-
Notifications
You must be signed in to change notification settings - Fork 77
/
container.go
104 lines (88 loc) · 2.6 KB
/
container.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
103
104
package manifest
// This file contains types and helper methods for wildcard containers.
// A wildcard container can contain either a finite set of elements or
// every possible element, in which case it is named `wildcard`.
import (
"bytes"
"encoding/json"
)
// WildStrings represents a string set which can be a wildcard.
type WildStrings struct {
Value []string
}
// WildPermissionDescs represents a PermissionDescriptor set which can be a wildcard.
type WildPermissionDescs struct {
Value []PermissionDesc
}
// Contains checks if v is in the container.
func (c *WildStrings) Contains(v string) bool {
if c.IsWildcard() {
return true
}
for _, s := range c.Value {
if v == s {
return true
}
}
return false
}
// Contains checks if v is in the container.
func (c *WildPermissionDescs) Contains(v PermissionDesc) bool {
if c.IsWildcard() {
return true
}
for _, u := range c.Value {
if u.Equals(v) {
return true
}
}
return false
}
// IsWildcard returns true iff the container is a wildcard.
func (c *WildStrings) IsWildcard() bool { return c.Value == nil }
// IsWildcard returns true iff the container is a wildcard.
func (c *WildPermissionDescs) IsWildcard() bool { return c.Value == nil }
// Restrict transforms the container into an empty one.
func (c *WildStrings) Restrict() { c.Value = []string{} }
// Restrict transforms the container into an empty one.
func (c *WildPermissionDescs) Restrict() { c.Value = []PermissionDesc{} }
// Add adds v to the container.
func (c *WildStrings) Add(v string) { c.Value = append(c.Value, v) }
// Add adds v to the container.
func (c *WildPermissionDescs) Add(v PermissionDesc) { c.Value = append(c.Value, v) }
// MarshalJSON implements the json.Marshaler interface.
func (c WildStrings) MarshalJSON() ([]byte, error) {
if c.IsWildcard() {
return []byte(`"*"`), nil
}
return json.Marshal(c.Value)
}
// MarshalJSON implements the json.Marshaler interface.
func (c WildPermissionDescs) MarshalJSON() ([]byte, error) {
if c.IsWildcard() {
return []byte(`"*"`), nil
}
return json.Marshal(c.Value)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (c *WildStrings) UnmarshalJSON(data []byte) error {
if !bytes.Equal(data, []byte(`"*"`)) {
ss := []string{}
if err := json.Unmarshal(data, &ss); err != nil {
return err
}
c.Value = ss
}
return nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (c *WildPermissionDescs) UnmarshalJSON(data []byte) error {
if !bytes.Equal(data, []byte(`"*"`)) {
us := []PermissionDesc{}
if err := json.Unmarshal(data, &us); err != nil {
return err
}
c.Value = us
}
return nil
}