storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/event/rules.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package event 18 19 import ( 20 "strings" 21 22 "storj.io/minio/pkg/wildcard" 23 ) 24 25 // NewPattern - create new pattern for prefix/suffix. 26 func NewPattern(prefix, suffix string) (pattern string) { 27 if prefix != "" { 28 if !strings.HasSuffix(prefix, "*") { 29 prefix += "*" 30 } 31 32 pattern = prefix 33 } 34 35 if suffix != "" { 36 if !strings.HasPrefix(suffix, "*") { 37 suffix = "*" + suffix 38 } 39 40 pattern += suffix 41 } 42 43 pattern = strings.Replace(pattern, "**", "*", -1) 44 45 return pattern 46 } 47 48 // Rules - event rules 49 type Rules map[string]TargetIDSet 50 51 // Add - adds pattern and target ID. 52 func (rules Rules) Add(pattern string, targetID TargetID) { 53 rules[pattern] = NewTargetIDSet(targetID).Union(rules[pattern]) 54 } 55 56 // MatchSimple - returns true one of the matching object name in rules. 57 func (rules Rules) MatchSimple(objectName string) bool { 58 for pattern := range rules { 59 if wildcard.MatchSimple(pattern, objectName) { 60 return true 61 } 62 } 63 return false 64 } 65 66 // Match - returns TargetIDSet matching object name in rules. 67 func (rules Rules) Match(objectName string) TargetIDSet { 68 targetIDs := NewTargetIDSet() 69 70 for pattern, targetIDSet := range rules { 71 if wildcard.MatchSimple(pattern, objectName) { 72 targetIDs = targetIDs.Union(targetIDSet) 73 } 74 } 75 76 return targetIDs 77 } 78 79 // Clone - returns copy of this rules. 80 func (rules Rules) Clone() Rules { 81 rulesCopy := make(Rules) 82 83 for pattern, targetIDSet := range rules { 84 rulesCopy[pattern] = targetIDSet.Clone() 85 } 86 87 return rulesCopy 88 } 89 90 // Union - returns union with given rules as new rules. 91 func (rules Rules) Union(rules2 Rules) Rules { 92 nrules := rules.Clone() 93 94 for pattern, targetIDSet := range rules2 { 95 nrules[pattern] = nrules[pattern].Union(targetIDSet) 96 } 97 98 return nrules 99 } 100 101 // Difference - returns diffrence with given rules as new rules. 102 func (rules Rules) Difference(rules2 Rules) Rules { 103 nrules := make(Rules) 104 105 for pattern, targetIDSet := range rules { 106 if nv := targetIDSet.Difference(rules2[pattern]); len(nv) > 0 { 107 nrules[pattern] = nv 108 } 109 } 110 111 return nrules 112 }