forked from minio/minio
/
rulesmap.go
78 lines (66 loc) · 2.24 KB
/
rulesmap.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
/*
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package event
// RulesMap - map of rules for every event name.
type RulesMap map[Name]Rules
// add - adds event names, prefixes, suffixes and target ID to rules map.
func (rulesMap RulesMap) add(eventNames []Name, pattern string, targetID TargetID) {
rules := make(Rules)
rules.Add(pattern, targetID)
for _, eventName := range eventNames {
for _, name := range eventName.Expand() {
rulesMap[name] = rulesMap[name].Union(rules)
}
}
}
// Clone - returns copy of this rules map.
func (rulesMap RulesMap) Clone() RulesMap {
rulesMapCopy := make(RulesMap)
for eventName, rules := range rulesMap {
rulesMapCopy[eventName] = rules.Clone()
}
return rulesMapCopy
}
// Add - adds given rules map.
func (rulesMap RulesMap) Add(rulesMap2 RulesMap) {
for eventName, rules := range rulesMap2 {
rulesMap[eventName] = rules.Union(rulesMap[eventName])
}
}
// Remove - removes given rules map.
func (rulesMap RulesMap) Remove(rulesMap2 RulesMap) {
for eventName, rules := range rulesMap {
if nr := rules.Difference(rulesMap2[eventName]); len(nr) != 0 {
rulesMap[eventName] = nr
} else {
delete(rulesMap, eventName)
}
}
}
// Match - returns TargetIDSet matching object name and event name in rules map.
func (rulesMap RulesMap) Match(eventName Name, objectName string) TargetIDSet {
return rulesMap[eventName].Match(objectName)
}
// NewRulesMap - creates new rules map with given values.
func NewRulesMap(eventNames []Name, pattern string, targetID TargetID) RulesMap {
// If pattern is empty, add '*' wildcard to match all.
if pattern == "" {
pattern = "*"
}
rulesMap := make(RulesMap)
rulesMap.add(eventNames, pattern, targetID)
return rulesMap
}