@@ -123,12 +123,23 @@ func (args Args) Get(key string) []string {
123
123
return slice
124
124
}
125
125
126
+ // Get returns the list of children key/value pairs associated with the key
127
+ func (args Args ) GetPair (key string ) map [string ]bool {
128
+ values := args .fields [key ]
129
+ return values
130
+ }
131
+
126
132
// Add a new value to the set of values
127
- func (args Args ) Add (key , value string ) {
133
+ func (args Args ) Add (key , value string , isEqualOps ... bool ) {
134
+ isEqualOp := true
135
+ if len (isEqualOps ) > 0 {
136
+ isEqualOp = isEqualOps [0 ]
137
+ }
138
+
128
139
if _ , ok := args .fields [key ]; ok {
129
- args.fields [key ][value ] = true
140
+ args.fields [key ][value ] = isEqualOp
130
141
} else {
131
- args .fields [key ] = map [string ]bool {value : true }
142
+ args .fields [key ] = map [string ]bool {value : isEqualOp }
132
143
}
133
144
}
134
145
@@ -181,18 +192,7 @@ func (args Args) Match(field, source string) bool {
181
192
if args .ExactMatch (field , source ) {
182
193
return true
183
194
}
184
-
185
- fieldValues := args .fields [field ]
186
- for name2match := range fieldValues {
187
- match , err := regexp .MatchString (name2match , source )
188
- if err != nil {
189
- continue
190
- }
191
- if match {
192
- return true
193
- }
194
- }
195
- return false
195
+ return matchWithOperator (args .fields [field ], source , regexp .MatchString )
196
196
}
197
197
198
198
// GetBoolOrDefault returns a boolean value of the key if the key is present
@@ -226,7 +226,32 @@ func (args Args) ExactMatch(key, source string) bool {
226
226
}
227
227
228
228
// try to match full name value to avoid O(N) regular expression matching
229
- return fieldValues [source ]
229
+ return matchWithOperator (fieldValues , source , func (a , b string ) (bool , error ) {
230
+ return a == b , nil
231
+ })
232
+ }
233
+
234
+ func matchWithOperator (fieldValues map [string ]bool , source string , matcher func (string , string ) (bool , error )) bool {
235
+ hasExclusion := false
236
+ for pattern , isEqual := range fieldValues {
237
+ match , err := matcher (pattern , source )
238
+ if err != nil {
239
+ continue
240
+ }
241
+ if isEqual {
242
+ if match {
243
+ return true
244
+ }
245
+ } else {
246
+ hasExclusion = true
247
+ if match {
248
+ return false
249
+ }
250
+ }
251
+ }
252
+ return hasExclusion
253
+ // If exclusion filters(!=) exist and none match the source, include the container
254
+ // if only inclusion filters(=), and none match the source, exclude the container
230
255
}
231
256
232
257
// UniqueExactMatch returns true if there is only one value and the source
@@ -242,7 +267,9 @@ func (args Args) UniqueExactMatch(key, source string) bool {
242
267
}
243
268
244
269
// try to match full name value to avoid O(N) regular expression matching
245
- return fieldValues [source ]
270
+ return matchWithOperator (fieldValues , source , func (a , b string ) (bool , error ) {
271
+ return a == b , nil
272
+ })
246
273
}
247
274
248
275
// FuzzyMatch returns true if the source matches exactly one value, or the
@@ -253,12 +280,9 @@ func (args Args) FuzzyMatch(key, source string) bool {
253
280
}
254
281
255
282
fieldValues := args .fields [key ]
256
- for prefix := range fieldValues {
257
- if strings .HasPrefix (source , prefix ) {
258
- return true
259
- }
260
- }
261
- return false
283
+ return matchWithOperator (fieldValues , source , func (a , b string ) (bool , error ) {
284
+ return strings .HasPrefix (b , a ), nil
285
+ })
262
286
}
263
287
264
288
// Contains returns true if the key exists in the mapping
0 commit comments