updated matchelementlist

This commit is contained in:
Natasha Sarkar
2020-11-05 16:52:36 -08:00
parent 8f80a898b6
commit e785bab474
2 changed files with 15 additions and 9 deletions

View File

@@ -281,7 +281,6 @@ func GetElementByKey(key string) ElementMatcher {
// ElementMatcher returns the first element from a Sequence matching the // ElementMatcher returns the first element from a Sequence matching the
// specified key-value pairs. If there's no match, and no configuration error, // specified key-value pairs. If there's no match, and no configuration error,
// the matcher returns nil, nil. // the matcher returns nil, nil.
type ElementMatcher struct { type ElementMatcher struct {
Kind string `yaml:"kind,omitempty"` Kind string `yaml:"kind,omitempty"`
@@ -301,6 +300,13 @@ type ElementMatcher struct {
} }
func (e ElementMatcher) Filter(rn *RNode) (*RNode, error) { func (e ElementMatcher) Filter(rn *RNode) (*RNode, error) {
if len(e.Keys) == 0 {
e.Keys = append(e.Keys, "")
}
if len(e.Values) == 0 {
e.Values = append(e.Values, "")
}
if err := ErrorIfInvalid(rn, yaml.SequenceNode); err != nil { if err := ErrorIfInvalid(rn, yaml.SequenceNode); err != nil {
return nil, err return nil, err
} }

View File

@@ -370,7 +370,7 @@ func TestElementMatcherWithNoValue(t *testing.T) {
assert.Equal(t, "b: \"\"\n", assertNoErrorString(t)(rn.String())) assert.Equal(t, "b: \"\"\n", assertNoErrorString(t)(rn.String()))
rn, err = node.Pipe(ElementMatcher{Keys: []string{"a"}}) rn, err = node.Pipe(ElementMatcher{Keys: []string{"a"}})
assert.Error(t, err) assert.NoError(t, err)
assert.Nil(t, rn) assert.Nil(t, rn)
rn, err = node.Pipe(ElementMatcher{Keys: []string{"a"}, MatchAnyValue: true}) rn, err = node.Pipe(ElementMatcher{Keys: []string{"a"}, MatchAnyValue: true})
@@ -378,7 +378,7 @@ func TestElementMatcherWithNoValue(t *testing.T) {
assert.Equal(t, "a: c\n", assertNoErrorString(t)(rn.String())) assert.Equal(t, "a: c\n", assertNoErrorString(t)(rn.String()))
_, err = node.Pipe(ElementMatcher{Keys: []string{"a"}, Values: []string{"c"}, MatchAnyValue: true}) _, err = node.Pipe(ElementMatcher{Keys: []string{"a"}, Values: []string{"c"}, MatchAnyValue: true})
assert.Errorf(t, err, "FieldValue must be empty when NoValue is set to true") assert.Errorf(t, err, "Values must be empty when MatchAnyValue is set to true")
} }
func TestElementMatcherMultipleKeys(t *testing.T) { func TestElementMatcherMultipleKeys(t *testing.T) {