mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-01-23 13:51:06 -05:00
Add role.FromString and more tolerance
This commit is contained in:
parent
f4af9c56f5
commit
75888e986e
@ -34,23 +34,14 @@ func (r *Role) UnmarshalJSON(b []byte) error {
|
||||
if err := json.Unmarshal(b, &roleString); err != nil {
|
||||
return err
|
||||
}
|
||||
switch strings.ToLower(roleString) {
|
||||
case "controlplane":
|
||||
*r = ControlPlane
|
||||
case "worker":
|
||||
*r = Worker
|
||||
case "admin":
|
||||
*r = Admin
|
||||
default:
|
||||
*r = Unknown
|
||||
}
|
||||
*r = FromString(roleString)
|
||||
return nil
|
||||
}
|
||||
|
||||
// FromString converts a string to a Role.
|
||||
// FromString returns the Role for the given string.
|
||||
func FromString(s string) Role {
|
||||
switch strings.ToLower(s) {
|
||||
case "controlplane":
|
||||
case "controlplane", "control-plane":
|
||||
return ControlPlane
|
||||
case "worker":
|
||||
return Worker
|
||||
|
@ -69,6 +69,10 @@ func TestUnmarshal(t *testing.T) {
|
||||
json: `"ControlPlane"`,
|
||||
wantRole: ControlPlane,
|
||||
},
|
||||
"dashed ControlPlane can be unmarshaled": {
|
||||
json: `"Control-Plane"`,
|
||||
wantRole: ControlPlane,
|
||||
},
|
||||
"lowercase controlPlane can be unmarshaled": {
|
||||
json: `"controlPlane"`,
|
||||
wantRole: ControlPlane,
|
||||
|
Loading…
Reference in New Issue
Block a user