-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprincipal.go
59 lines (48 loc) · 1.4 KB
/
principal.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
package CloudPolicy
import "encoding/json"
// PrincipalType is a string that represents the type of Principal. it should be like "AWS", "Federated", "Service", "CanonicalUser", "RAM", "qcs" .. or any other string.
type PrincipalType = string
const ()
// Principal is a map of PrincipalType and Value. or It will be "*"
type Principal struct {
value map[PrincipalType]*Value
all bool
}
var _ json.Marshaler = (*Principal)(nil)
var _ json.Unmarshaler = (*Principal)(nil)
func (t *Principal) UnmarshalJSON(data []byte) error {
if string(data) == "\"*\"" {
t.all = true
return nil
}
t.all = false
return json.Unmarshal(data, &t.value)
}
func (t *Principal) MarshalJSON() ([]byte, error) {
if t.all {
return json.Marshal("*")
}
return json.Marshal(t.value)
}
// SetAllowAllPrincipal sets the Principal to "*"
func (t *Principal) SetAllowAllPrincipal() {
t.all = true
}
// SetPrincipal sets the Principal to a single PrincipalType and Value
func (t *Principal) SetPrincipal(a PrincipalType, b *Value) {
if t.value == nil {
t.value = make(map[PrincipalType]*Value)
}
t.value = map[PrincipalType]*Value{a: b}
}
// SetPrincipals sets the Principal directly to a map of PrincipalType and Value
func (t *Principal) SetPrincipals(a map[PrincipalType]*Value) {
t.value = a
}
// Value returns the value of the Principal object.
func (t *Principal) Value() any {
if t.all {
return "*"
}
return t.value
}