mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
client: Filters: add Clone method
This method returns a deep-copy of the filter, which can be used in situations where the original filter must not be mutated, but additional filters need to be added for a specific request. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -30,6 +30,19 @@ func (f Filters) Add(term string, values ...string) Filters {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone returns a deep copy of f.
|
||||||
|
func (f Filters) Clone() Filters {
|
||||||
|
out := make(Filters, len(f))
|
||||||
|
for term, values := range f {
|
||||||
|
inner := make(map[string]bool, len(values))
|
||||||
|
for v, ok := range values {
|
||||||
|
inner[v] = ok
|
||||||
|
}
|
||||||
|
out[term] = inner
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// updateURLValues sets the "filters" key in values to the marshalled value of
|
// 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
|
// f, replacing any existing values. When f is empty, any existing "filters" key
|
||||||
// is removed.
|
// is removed.
|
||||||
|
|||||||
@@ -46,3 +46,26 @@ func TestFilters_UpdateURLValues(t *testing.T) {
|
|||||||
Filters{"foo": map[string]bool{"bar": true}}.updateURLValues(v)
|
Filters{"foo": map[string]bool{"bar": true}}.updateURLValues(v)
|
||||||
assert.Check(t, is.DeepEqual(v, url.Values{"filters": []string{`{"foo":{"bar":true}}`}}))
|
assert.Check(t, is.DeepEqual(v, url.Values{"filters": []string{`{"foo":{"bar":true}}`}}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilters_Clone(t *testing.T) {
|
||||||
|
f1 := make(Filters).Add("foo", "one")
|
||||||
|
f2 := f1.Clone()
|
||||||
|
|
||||||
|
f2.Add("foo", "f2-extra").Add("f2", "f2-value")
|
||||||
|
assert.Check(t, is.DeepEqual(f1, Filters{"foo": {"one": true}}))
|
||||||
|
assert.Check(t, is.DeepEqual(f2, Filters{
|
||||||
|
"foo": {"one": true, "f2-extra": true},
|
||||||
|
"f2": {"f2-value": true},
|
||||||
|
}))
|
||||||
|
|
||||||
|
f1.Add("foo", "f1-extra").Add("f1", "f1-value")
|
||||||
|
assert.Check(t, is.DeepEqual(f1, Filters{
|
||||||
|
"foo": {"one": true, "f1-extra": true},
|
||||||
|
"f1": {"f1-value": true},
|
||||||
|
}))
|
||||||
|
|
||||||
|
assert.Check(t, is.DeepEqual(f2, Filters{
|
||||||
|
"foo": {"one": true, "f2-extra": true},
|
||||||
|
"f2": {"f2-value": true},
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|||||||
13
vendor/github.com/moby/moby/client/filters.go
generated
vendored
13
vendor/github.com/moby/moby/client/filters.go
generated
vendored
@@ -30,6 +30,19 @@ func (f Filters) Add(term string, values ...string) Filters {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone returns a deep copy of f.
|
||||||
|
func (f Filters) Clone() Filters {
|
||||||
|
out := make(Filters, len(f))
|
||||||
|
for term, values := range f {
|
||||||
|
inner := make(map[string]bool, len(values))
|
||||||
|
for v, ok := range values {
|
||||||
|
inner[v] = ok
|
||||||
|
}
|
||||||
|
out[term] = inner
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// updateURLValues sets the "filters" key in values to the marshalled value of
|
// 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
|
// f, replacing any existing values. When f is empty, any existing "filters" key
|
||||||
// is removed.
|
// is removed.
|
||||||
|
|||||||
Reference in New Issue
Block a user