-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcondition.go
187 lines (145 loc) · 4.42 KB
/
condition.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package restrict
import (
"encoding/json"
"gopkg.in/yaml.v3"
)
// Condition - additional requirement that needs to be satisfied
// to grant given permission.
type Condition interface {
// Type - returns Condition's type.
Type() string
// Check - returns true if Condition is satisfied by
// given AccessRequest, false otherwise.
Check(request *AccessRequest) error
}
// Conditions - alias type for Conditions array.
type Conditions []Condition
// AppendCondition - adds new Condition to Conditions slice.
func (cs *Conditions) appendCondition(condition Condition) {
if cs == nil {
cs = &Conditions{}
}
*cs = append(*cs, condition)
}
// jsonMarshalableCondition - helper type for handling marshaling/unmarshaling
// of JSON structures.
type jsonMarshalableCondition struct {
Type string `json:"type"`
Options json.RawMessage `json:"options,omitempty"`
}
// yamlMarshalableCondition - helper type for handling marshaling/unmarshaling
// of YAML structures.
type yamlMarshalableCondition struct {
Type string `yaml:"type"`
Options yaml.Node `yaml:"options,omitempty"`
}
// MarshalJSON - marshals a map of Conditions to JSON data.
func (cs Conditions) MarshalJSON() ([]byte, error) {
result := []*jsonMarshalableCondition{}
for _, condition := range cs {
options, err := json.Marshal(condition)
if err != nil {
return nil, err
}
result = append(result, &jsonMarshalableCondition{
Type: condition.Type(),
Options: json.RawMessage(options),
})
}
return json.Marshal(result)
}
// MarshalYAML - marshals a map of Conditions to YAML data.
func (cs Conditions) MarshalYAML() (interface{}, error) {
result := []*yamlMarshalableCondition{}
for _, condition := range cs {
options := yaml.Node{}
if err := options.Encode(condition); err != nil {
return nil, err
}
result = append(result, &yamlMarshalableCondition{
Type: condition.Type(),
Options: options,
})
}
output := yaml.Node{}
if err := output.Encode(result); err != nil {
return nil, err
}
return output, nil
}
// UnmarshalJSON - unmarshals a JSON-coded map of Conditions.
func (cs *Conditions) UnmarshalJSON(jsonData []byte) error {
var jsonValue []jsonMarshalableCondition
if err := json.Unmarshal(jsonData, &jsonValue); err != nil {
return err
}
for _, jsonCondition := range jsonValue {
factory := ConditionFactories[jsonCondition.Type]
if factory == nil {
return newConditionFactoryNotFoundError(jsonCondition.Type)
}
condition := factory()
if len(jsonCondition.Options) > 0 {
if err := json.Unmarshal(jsonCondition.Options, condition); err != nil {
return err
}
}
cs.appendCondition(condition)
}
return nil
}
// UnmarshalYAML - unmarshals a YAML-coded map of Conditions.
func (cs *Conditions) UnmarshalYAML(value *yaml.Node) error {
var yamlValue []yamlMarshalableCondition
if err := value.Decode(&yamlValue); err != nil {
return err
}
for _, yamlCondition := range yamlValue {
// Guard for conditions maps being empty - YAML will still
// create a Nodes from them.
if yamlCondition.Type == "" {
continue
}
factory := ConditionFactories[yamlCondition.Type]
if factory == nil {
return newConditionFactoryNotFoundError(yamlCondition.Type)
}
condition := factory()
if len(yamlCondition.Options.Content) > 0 {
if err := yamlCondition.Options.Decode(condition); err != nil {
return err
}
}
cs.appendCondition(condition)
}
return nil
}
// ConditionFactory - factory function for Condition.
type ConditionFactory func() Condition
// ConditionFactoriesMap - map of Condition factories.
type ConditionFactoriesMap = map[string]ConditionFactory
// ConditionFactories - stores a map of functions responsible for
// creating new Conditions, based on their names.
var ConditionFactories = ConditionFactoriesMap{
EqualConditionType: func() Condition {
return new(EqualCondition)
},
NotEqualConditionType: func() Condition {
return new(NotEqualCondition)
},
EmptyConditionType: func() Condition {
return new(EmptyCondition)
},
NotEmptyConditionType: func() Condition {
return new(NotEmptyCondition)
},
}
// RegisterConditionFactory - adds a new ConditionFactory under given name. If given name
// is already taken, an error is returned.
func RegisterConditionFactory(name string, factory ConditionFactory) error {
if ConditionFactories[name] != nil {
return newConditionFactoryAlreadyExistsError(name)
}
ConditionFactories[name] = factory
return nil
}