From bda634ef20dbc5f02fd6acb73919d70bf091b642 Mon Sep 17 00:00:00 2001 From: Joe Betz Date: Thu, 31 Oct 2024 13:40:44 -0400 Subject: [PATCH] Apply feedback --- fieldpath/set.go | 10 ++++-- fieldpath/set_test.go | 78 ++++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/fieldpath/set.go b/fieldpath/set.go index e07d0f3..77ae251 100644 --- a/fieldpath/set.go +++ b/fieldpath/set.go @@ -153,7 +153,7 @@ func MakePrefixMatcherOrDie(parts ...interface{}) *SetMatcher { // // - PathElementMatcher - for wildcards, `MatchAnyPathElement()` can be used as well. // - PathElement - for any path element -// - value.FieldList - for associative list keys +// - value.FieldList - for listMap keys // - value.Value - for scalar list elements // - string - For field names // - int - for array indices @@ -164,19 +164,25 @@ func PrefixMatcher(parts ...interface{}) (*SetMatcher, error) { var pattern PathElementMatcher switch t := part.(type) { case PathElementMatcher: + // any path matcher, including wildcard pattern = t case PathElement: + // any path element pattern = PathElementMatcher{PathElement: t} case *value.FieldList: + // a listMap key if len(*t) == 0 { return nil, fmt.Errorf("associative list key type path elements must have at least one key (got zero)") } pattern = PathElementMatcher{PathElement: PathElement{Key: t}} case value.Value: + // a scalar or set-type list element pattern = PathElementMatcher{PathElement: PathElement{Value: &t}} case string: + // a plain field name pattern = PathElementMatcher{PathElement: PathElement{FieldName: &t}} case int: + // a plain list index pattern = PathElementMatcher{PathElement: PathElement{Index: &t}} default: return nil, fmt.Errorf("unexpected type %T", t) @@ -270,7 +276,7 @@ type SetMemberMatcher struct { Child *SetMatcher } -// PathElementMatcher defined a match matcher for a PathElement. +// PathElementMatcher defined a path matcher for a PathElement. type PathElementMatcher struct { // Wildcard indicates that all PathElements are matched by this matcher. // If set, PathElement is ignored. diff --git a/fieldpath/set_test.go b/fieldpath/set_test.go index ef6aff1..7efb374 100644 --- a/fieldpath/set_test.go +++ b/fieldpath/set_test.go @@ -769,16 +769,20 @@ func TestFilterByPattern(t *testing.T) { input: NewSet( MakePathOrDie("spec"), MakePathOrDie("spec", "containers"), + MakePathOrDie("spec", "containers", 0), MakePathOrDie("spec", "containers", 0, "resources"), MakePathOrDie("spec", "containers", 0, "resources", "limits"), MakePathOrDie("spec", "containers", 0, "resources", "limits", "cpu"), MakePathOrDie("spec", "containers", 0, "resources", "requests"), MakePathOrDie("spec", "containers", 0, "resources", "requests", "cpu"), MakePathOrDie("spec", "containers", 0, "resources", "claims"), + MakePathOrDie("spec", "containers", 0, "resources", "claims", 0), MakePathOrDie("spec", "containers", 0, "resources", "claims", 0, "name"), MakePathOrDie("spec", "containers", 0, "resources", "claims", 0, "request"), + MakePathOrDie("spec", "containers", 0, "resources", "claims", 1), MakePathOrDie("spec", "containers", 0, "resources", "claims", 1, "name"), MakePathOrDie("spec", "containers", 0, "resources", "claims", 1, "request"), + MakePathOrDie("spec", "containers", 1), MakePathOrDie("spec", "containers", 1, "resources"), MakePathOrDie("spec", "containers", 1, "resources", "limits"), MakePathOrDie("spec", "containers", 1, "resources", "limits", "cpu"), @@ -787,16 +791,20 @@ func TestFilterByPattern(t *testing.T) { expect: NewSet( MakePathOrDie("spec"), MakePathOrDie("spec", "containers"), + MakePathOrDie("spec", "containers", 0), MakePathOrDie("spec", "containers", 0, "resources"), MakePathOrDie("spec", "containers", 0, "resources", "limits"), MakePathOrDie("spec", "containers", 0, "resources", "limits", "cpu"), MakePathOrDie("spec", "containers", 0, "resources", "requests"), MakePathOrDie("spec", "containers", 0, "resources", "requests", "cpu"), MakePathOrDie("spec", "containers", 0, "resources", "claims"), + MakePathOrDie("spec", "containers", 0, "resources", "claims", 0), MakePathOrDie("spec", "containers", 0, "resources", "claims", 0, "name"), MakePathOrDie("spec", "containers", 0, "resources", "claims", 0, "request"), + MakePathOrDie("spec", "containers", 0, "resources", "claims", 1), MakePathOrDie("spec", "containers", 0, "resources", "claims", 1, "name"), MakePathOrDie("spec", "containers", 0, "resources", "claims", 1, "request"), + MakePathOrDie("spec", "containers", 1), MakePathOrDie("spec", "containers", 1, "resources"), MakePathOrDie("spec", "containers", 1, "resources", "limits"), MakePathOrDie("spec", "containers", 1, "resources", "limits", "cpu"), @@ -831,6 +839,7 @@ func TestFilterByPattern(t *testing.T) { input: NewSet( MakePathOrDie("spec"), MakePathOrDie("spec", "containers"), + MakePathOrDie("spec", "containers", 0), MakePathOrDie("spec", "containers", 0, "image"), MakePathOrDie("spec", "containers", 0, "workingDir"), MakePathOrDie("spec", "containers", 0, "resources"), @@ -839,6 +848,7 @@ func TestFilterByPattern(t *testing.T) { expect: NewSet( MakePathOrDie("spec"), MakePathOrDie("spec", "containers"), + MakePathOrDie("spec", "containers", 0), MakePathOrDie("spec", "containers", 0, "resources"), ), }, @@ -880,44 +890,6 @@ func TestFilterByPattern(t *testing.T) { }, "field"), ), }, - { - name: "filter listMap key", - input: NewSet( - MakePathOrDie("spec"), - MakePathOrDie("spec", "listMap", - &value.FieldList{ - {Name: "key1", Value: value.NewValueInterface("value1")}, - {Name: "key2", Value: value.NewValueInterface("value2")}, - }), - MakePathOrDie("spec", "listMap", - &value.FieldList{ - {Name: "key1", Value: value.NewValueInterface("value1")}, - {Name: "key2", Value: value.NewValueInterface("value2")}, - }, "field"), - MakePathOrDie("spec", "listMap", - &value.FieldList{ - {Name: "key1", Value: value.NewValueInterface("valueX")}, - {Name: "key2", Value: value.NewValueInterface("valueY")}, - }, "field"), - ), - filter: NewIncludeMatcherFilter(MakePrefixMatcherOrDie("spec", "listMap", &value.FieldList{ - {Name: "key1", Value: value.NewValueInterface("value1")}, - {Name: "key2", Value: value.NewValueInterface("value2")}, - })), - expect: NewSet( - MakePathOrDie("spec"), - MakePathOrDie("spec", "listMap", - &value.FieldList{ - {Name: "key1", Value: value.NewValueInterface("value1")}, - {Name: "key2", Value: value.NewValueInterface("value2")}, - }), - MakePathOrDie("spec", "listMap", - &value.FieldList{ - {Name: "key1", Value: value.NewValueInterface("value1")}, - {Name: "key2", Value: value.NewValueInterface("value2")}, - }, "field"), - ), - }, { name: "filter value", input: NewSet( @@ -935,12 +907,17 @@ func TestFilterByPattern(t *testing.T) { name: "filter by index", input: NewSet( MakePathOrDie("spec"), + MakePathOrDie("spec", "list"), + MakePathOrDie("spec", "list", 0), MakePathOrDie("spec", "list", 0, "value"), + MakePathOrDie("spec", "list", 1), MakePathOrDie("spec", "list", 1, "value"), ), filter: NewIncludeMatcherFilter(MakePrefixMatcherOrDie("spec", "list", 1, "value")), expect: NewSet( MakePathOrDie("spec"), + MakePathOrDie("spec", "list"), + MakePathOrDie("spec", "list", 1), MakePathOrDie("spec", "list", 1, "value"), ), }, @@ -948,8 +925,13 @@ func TestFilterByPattern(t *testing.T) { name: "multiple index matchers", input: NewSet( MakePathOrDie("spec"), + MakePathOrDie("spec", "list"), + MakePathOrDie("spec", "list", 0), MakePathOrDie("spec", "list", 0, "value"), + MakePathOrDie("spec", "list", 1), MakePathOrDie("spec", "list", 1, "value"), + MakePathOrDie("spec", "list", 2), + MakePathOrDie("spec", "list", 2, "value"), ), filter: NewIncludeMatcherFilter( MakePrefixMatcherOrDie("spec", "list", 0, "value"), @@ -957,15 +939,22 @@ func TestFilterByPattern(t *testing.T) { ), expect: NewSet( MakePathOrDie("spec"), + MakePathOrDie("spec", "list"), + MakePathOrDie("spec", "list", 0), MakePathOrDie("spec", "list", 0, "value"), + MakePathOrDie("spec", "list", 1), MakePathOrDie("spec", "list", 1, "value"), ), }, { name: "multiple field matchers", input: NewSet( + MakePathOrDie("spec"), + MakePathOrDie("spec", "f1"), MakePathOrDie("spec", "f1", "f11"), + MakePathOrDie("spec", "f2"), MakePathOrDie("spec", "f2", "f21"), + MakePathOrDie("spec", "f3"), MakePathOrDie("spec", "f3", "f31"), ), filter: NewIncludeMatcherFilter( @@ -973,17 +962,25 @@ func TestFilterByPattern(t *testing.T) { MakePrefixMatcherOrDie("spec", "f3"), ), expect: NewSet( + MakePathOrDie("spec"), + MakePathOrDie("spec", "f1"), MakePathOrDie("spec", "f1", "f11"), + MakePathOrDie("spec", "f3"), MakePathOrDie("spec", "f3", "f31"), ), }, { name: "wildcard takes precedence", input: NewSet( + MakePathOrDie("spec"), + MakePathOrDie("spec", "list"), + MakePathOrDie("spec", "list", 0), MakePathOrDie("spec", "list", 0, "f1"), MakePathOrDie("spec", "list", 0, "f2"), + MakePathOrDie("spec", "list", 1), MakePathOrDie("spec", "list", 1, "f1"), MakePathOrDie("spec", "list", 1, "f2"), + MakePathOrDie("spec", "list", 2), MakePathOrDie("spec", "list", 2, "f1"), MakePathOrDie("spec", "list", 2, "f2"), ), @@ -992,8 +989,13 @@ func TestFilterByPattern(t *testing.T) { MakePrefixMatcherOrDie("spec", "list", 1, "f2"), // ignored ), expect: NewSet( + MakePathOrDie("spec"), + MakePathOrDie("spec", "list"), + MakePathOrDie("spec", "list", 0), MakePathOrDie("spec", "list", 0, "f1"), + MakePathOrDie("spec", "list", 1), MakePathOrDie("spec", "list", 1, "f1"), + MakePathOrDie("spec", "list", 2), MakePathOrDie("spec", "list", 2, "f1"), ), },