Files
moby/client/filters.go
Cory Snider 7ea066c8d1 client: add Filters type
Add a new type to use for building filter predicates for API requests,
replacing "./api/types/filters".Args in the client. Remove the now
unused api/types/filters package.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2025-10-08 12:06:31 -04:00

47 lines
1.2 KiB
Go

package client
import (
"encoding/json"
"net/url"
)
// Filters describes a predicate for an API request.
//
// Each entry in the map is a filter term.
// Each term is evaluated against the set of values.
// A filter term is satisfied if any one of the values in the set is a match.
// An item matches the filters when all terms are satisfied.
//
// Like all other map types in Go, the zero value is empty and read-only.
type Filters map[string]map[string]bool
// Add appends values to the value-set of term.
//
// The receiver f is returned for chaining.
//
// f := make(Filters).Add("name", "foo", "bar").Add("status", "exited")
func (f Filters) Add(term string, values ...string) Filters {
if _, ok := f[term]; !ok {
f[term] = make(map[string]bool)
}
for _, v := range values {
f[term][v] = true
}
return f
}
// updateURLValues sets the "filters" key in values to the marshalled value of
// f, replacing any existing values. When f is empty, any existing "filters" key
// is removed.
func (f Filters) updateURLValues(values url.Values) {
if len(f) > 0 {
b, err := json.Marshal(f)
if err != nil {
panic(err) // Marshaling builtin types should never fail
}
values.Set("filters", string(b))
} else {
values.Del("filters")
}
}