MCPcopy Index your code
hub / github.com/coder/coder / UnmarshalJSON

Method UnmarshalJSON

codersdk/allowlist.go:44–72  ·  view source on GitHub ↗

UnmarshalJSON decodes from a JSON string: " : ".

(b []byte)

Source from the content-addressed store, hash-verified

42
43// UnmarshalJSON decodes from a JSON string: "<type>:<id>".
44func (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

Callers 1

UnmarshalTextMethod · 0.95

Calls 4

RBACResourceTypeAlias · 0.85
ParseMethod · 0.65
UnmarshalMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected