RoleNameFromString takes a formatted string ' [:org_id]'.
(input string)
| 71 | |
| 72 | // RoleNameFromString takes a formatted string '<role_name>[:org_id]'. |
| 73 | func RoleNameFromString(input string) (RoleIdentifier, error) { |
| 74 | var role RoleIdentifier |
| 75 | |
| 76 | arr := strings.Split(input, ":") |
| 77 | if len(arr) > 2 { |
| 78 | return role, xerrors.Errorf("too many colons in role name") |
| 79 | } |
| 80 | |
| 81 | if len(arr) == 0 { |
| 82 | return role, xerrors.Errorf("empty string not a valid role") |
| 83 | } |
| 84 | |
| 85 | if arr[0] == "" { |
| 86 | return role, xerrors.Errorf("role cannot be the empty string") |
| 87 | } |
| 88 | |
| 89 | role.Name = arr[0] |
| 90 | |
| 91 | if len(arr) == 2 { |
| 92 | orgID, err := uuid.Parse(arr[1]) |
| 93 | if err != nil { |
| 94 | return role, xerrors.Errorf("%q not a valid uuid: %w", arr[1], err) |
| 95 | } |
| 96 | role.OrganizationID = orgID |
| 97 | } |
| 98 | return role, nil |
| 99 | } |
| 100 | |
| 101 | func (r RoleIdentifier) String() string { |
| 102 | if r.OrganizationID != uuid.Nil { |
no test coverage detected