From 75888e986ef4ced807ec63e5473e315fe5836333 Mon Sep 17 00:00:00 2001 From: katexochen <49727155+katexochen@users.noreply.github.com> Date: Thu, 6 Oct 2022 11:57:35 +0200 Subject: [PATCH] Add role.FromString and more tolerance --- internal/role/role.go | 15 +++------------ internal/role/role_test.go | 4 ++++ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/internal/role/role.go b/internal/role/role.go index 5403d978e..a7deeaace 100644 --- a/internal/role/role.go +++ b/internal/role/role.go @@ -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 diff --git a/internal/role/role_test.go b/internal/role/role_test.go index 50a72b16d..210c9254c 100644 --- a/internal/role/role_test.go +++ b/internal/role/role_test.go @@ -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,