-
Notifications
You must be signed in to change notification settings - Fork 10
/
entity_uid.go
102 lines (85 loc) · 2.68 KB
/
entity_uid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package types
import (
"encoding/json"
"hash/fnv"
"strconv"
"github.com/cedar-policy/cedar-go/internal/mapset"
)
// Path is a series of idents separated by ::
type Path string
// EntityType is the type portion of an EntityUID
type EntityType Path
// An EntityUID is the identifier for a principal, action, or resource.
type EntityUID struct {
Type EntityType
ID String
}
// NewEntityUID returns an EntityUID given an EntityType and identifier
func NewEntityUID(typ EntityType, id String) EntityUID {
return EntityUID{
Type: typ,
ID: id,
}
}
// IsZero returns true if the EntityUID has an empty Type and ID.
func (a EntityUID) IsZero() bool {
return a.Type == "" && a.ID == ""
}
func (a EntityUID) Equal(bi Value) bool {
b, ok := bi.(EntityUID)
return ok && a == b
}
// String produces a string representation of the EntityUID, e.g. `Type::"id"`.
func (v EntityUID) String() string { return string(v.Type) + "::" + strconv.Quote(string(v.ID)) }
// MarshalCedar produces a valid MarshalCedar language representation of the EntityUID, e.g. `Type::"id"`.
func (v EntityUID) MarshalCedar() []byte {
return []byte(v.String())
}
func (v *EntityUID) UnmarshalJSON(b []byte) error {
// TODO: review after adding support for schemas
var res entityValueJSON
if err := json.Unmarshal(b, &res); err != nil {
return err
}
if res.Entity != nil {
v.Type = EntityType(res.Entity.Type)
v.ID = String(res.Entity.ID)
return nil
} else if res.Type != nil && res.ID != nil { // require both Type and ID to parse "implicit" JSON
v.Type = EntityType(*res.Type)
v.ID = String(*res.ID)
return nil
}
return errJSONEntityNotFound
}
// MarshalJSON marshals the EntityUID into JSON using the explicit form.
func (v EntityUID) MarshalJSON() ([]byte, error) {
return json.Marshal(entityValueJSON{
Entity: &extEntity{
Type: string(v.Type),
ID: string(v.ID),
},
})
}
func (v EntityUID) hash() uint64 {
h := fnv.New64()
_, _ = h.Write([]byte(v.Type))
_, _ = h.Write([]byte(v.ID))
return h.Sum64()
}
// ImplicitlyMarshaledEntityUID exists to allow the marshaling of the EntityUID into JSON using the implicit form. Users
// can opt in to this form if they know that this EntityUID will be serialized to a place where its type will be
// unambiguous.
type ImplicitlyMarshaledEntityUID EntityUID
func (i ImplicitlyMarshaledEntityUID) MarshalJSON() ([]byte, error) {
s := struct {
Type EntityType `json:"type"`
ID String `json:"id"`
}{i.Type, i.ID}
return json.Marshal(s)
}
type EntityUIDSet = mapset.ImmutableMapSet[EntityUID]
// NewEntityUIDSet returns an immutable EntityUIDSet ready for use.
func NewEntityUIDSet(args ...EntityUID) EntityUIDSet {
return mapset.Immutable[EntityUID](args...)
}