Skip to content

Commit 1311e4d

Browse files
committed
Add support for more field path pattern types, clarify comments
1 parent 1206de6 commit 1311e4d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

fieldpath/set.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package fieldpath
1818

1919
import (
2020
"fmt"
21+
"sigs.k8s.io/structured-merge-diff/v4/value"
2122
"sort"
2223
"strings"
2324

@@ -148,8 +149,14 @@ func MustPrefixPattern(parts ...interface{}) *SetPattern {
148149
}
149150

150151
// PrefixPattern creates a SetPattern that matches all field paths prefixed by the given list of pattern path parts.
151-
// The pattern parts may be PathPatterns, PathElements, strings (for field names) or ints (for array indices).
152-
// `MatchAnyPathElement()` may be used as a pattern path part to wildcard match a field path part.
152+
// The pattern parts may any of:
153+
//
154+
// - PathPattern - for wildcards, `MatchAnyPathElement()` can be used as well.
155+
// - PathElement - for any path element
156+
// - value.FieldList - for associative list keys
157+
// - value.Value - for scalar list elements
158+
// - string - For field names
159+
// - int - for array indices
153160
func PrefixPattern(parts ...interface{}) (*SetPattern, error) {
154161
current := MatchAnySet() // match all field patch suffixes
155162
for i := len(parts) - 1; i >= 0; i-- {
@@ -160,6 +167,13 @@ func PrefixPattern(parts ...interface{}) (*SetPattern, error) {
160167
pattern = t
161168
case PathElement:
162169
pattern = PathPattern{PathElement: t}
170+
case *value.FieldList:
171+
if len(*t) == 0 {
172+
return nil, fmt.Errorf("associative list key type path elements must have at least one key (got zero)")
173+
}
174+
pattern = PathPattern{PathElement: PathElement{Key: t}}
175+
case value.Value:
176+
pattern = PathPattern{PathElement: PathElement{Value: &t}}
163177
case string:
164178
pattern = PathPattern{PathElement: PathElement{FieldName: &t}}
165179
case int:
@@ -639,7 +653,7 @@ func (s *SetNodeMap) Leaves() *SetNodeMap {
639653
return out
640654
}
641655

642-
// Filter defines an interface for filtering a set.
656+
// Filter defines an interface for excluding field paths from a set.
643657
// NewExcludeFilter can be used to create a filter that removes
644658
// excluded field paths.
645659
// NewPatternFilter can be used to create a filter that removes all fields except

0 commit comments

Comments
 (0)