diff --git a/api/types/filters/parse.go b/api/types/filters/parse.go index a41e3d8d96..d8f19ae227 100644 --- a/api/types/filters/parse.go +++ b/api/types/filters/parse.go @@ -323,6 +323,22 @@ func (args Args) WalkValues(field string, op func(value string) error) error { return nil } +// Clone returns a copy of args. +func (args Args) Clone() (newArgs Args) { + newArgs.fields = make(map[string]map[string]bool, len(args.fields)) + for k, m := range args.fields { + var mm map[string]bool + if m != nil { + mm = make(map[string]bool, len(m)) + for kk, v := range m { + mm[kk] = v + } + } + newArgs.fields[k] = mm + } + return newArgs +} + func deprecatedArgs(d map[string][]string) map[string]map[string]bool { m := map[string]map[string]bool{} for k, v := range d { diff --git a/api/types/filters/parse_test.go b/api/types/filters/parse_test.go index e8345a1d5d..17feefe748 100644 --- a/api/types/filters/parse_test.go +++ b/api/types/filters/parse_test.go @@ -421,3 +421,11 @@ func TestFuzzyMatch(t *testing.T) { } } } + +func TestClone(t *testing.T) { + f := NewArgs() + f.Add("foo", "bar") + f2 := f.Clone() + f2.Add("baz", "qux") + assert.Check(t, is.Len(f.Get("baz"), 0)) +}