UnmarshalJSON decodes from a JSON string: " : ".
(b []byte)
| 42 | |
| 43 | // UnmarshalJSON decodes from a JSON string: "<type>:<id>". |
| 44 | func (t *APIAllowListTarget) UnmarshalJSON(b []byte) error { |
| 45 | var s string |
| 46 | if err := json.Unmarshal(b, &s); err != nil { |
| 47 | return err |
| 48 | } |
| 49 | parts := strings.SplitN(strings.TrimSpace(s), ":", 2) |
| 50 | if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
| 51 | return xerrors.Errorf("invalid allow_list entry %q: want <type>:<id>", s) |
| 52 | } |
| 53 | |
| 54 | resource, id := RBACResource(parts[0]), parts[1] |
| 55 | |
| 56 | // Type |
| 57 | if resource != ResourceWildcard { |
| 58 | if _, ok := policy.RBACPermissions[string(resource)]; !ok { |
| 59 | return xerrors.Errorf("unknown resource type %q", resource) |
| 60 | } |
| 61 | } |
| 62 | t.Type = resource |
| 63 | |
| 64 | // ID |
| 65 | if id != policy.WildcardSymbol { |
| 66 | if _, err := uuid.Parse(id); err != nil { |
| 67 | return xerrors.Errorf("invalid %s ID (must be UUID): %q", resource, id) |
| 68 | } |
| 69 | } |
| 70 | t.ID = id |
| 71 | return nil |
| 72 | } |
| 73 | |
| 74 | // Implement encoding.TextMarshaler/Unmarshaler for broader compatibility |
| 75 |
no test coverage detected