Skip to content

Commit 5df60fe

Browse files
Support '!=' operator in filters
Introduced matchWithOperator to determine inclusionary (=) or exclusionary (!=) filtering based on CLI input, per changes in [this PR](docker/cli#6035). Integrated it into Match, ExactMatch, UniqueExactMatch, and FuzzyMatch functions Also created GetPair Function and updated the Add function for use in the CLI update in the above PR Signed-off-by: Mohammed Aminu Futa <[email protected]>
1 parent 62bc597 commit 5df60fe

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

api/types/filters/parse.go

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,23 @@ func (args Args) Get(key string) []string {
123123
return slice
124124
}
125125

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+
126132
// 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+
128139
if _, ok := args.fields[key]; ok {
129-
args.fields[key][value] = true
140+
args.fields[key][value] = isEqualOp
130141
} else {
131-
args.fields[key] = map[string]bool{value: true}
142+
args.fields[key] = map[string]bool{value: isEqualOp}
132143
}
133144
}
134145

@@ -181,18 +192,7 @@ func (args Args) Match(field, source string) bool {
181192
if args.ExactMatch(field, source) {
182193
return true
183194
}
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)
196196
}
197197

198198
// 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 {
226226
}
227227

228228
// 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
230255
}
231256

232257
// UniqueExactMatch returns true if there is only one value and the source
@@ -242,7 +267,9 @@ func (args Args) UniqueExactMatch(key, source string) bool {
242267
}
243268

244269
// 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+
})
246273
}
247274

248275
// FuzzyMatch returns true if the source matches exactly one value, or the
@@ -253,12 +280,9 @@ func (args Args) FuzzyMatch(key, source string) bool {
253280
}
254281

255282
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+
})
262286
}
263287

264288
// Contains returns true if the key exists in the mapping

0 commit comments

Comments
 (0)