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
// specified key-value pairs. If there's no match, and no configuration error,
// the matcher returns nil, nil.
type ElementMatcher struct {
Kind string `yaml:"kind,omitempty"`
@@ -301,6 +300,13 @@ type ElementMatcher struct {
}
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 {
return nil, err
}

View File

@@ -171,8 +171,8 @@ func TestElementSetter(t *testing.T) {
"e": "f",
})
rn, err = node.Pipe(ElementSetterList{
Keys: []string{"a"},
Values: []string{"b"},
Keys: []string{"a"},
Values: []string{"b"},
Element: newElement.YNode(),
})
assert.NoError(t, err)
@@ -186,8 +186,8 @@ func TestElementSetter(t *testing.T) {
// Set an element with scalar, {"a": "b"} to "foo"
newElement = NewScalarRNode("foo")
rn, err = node.Pipe(ElementSetterList{
Keys: []string{"a"},
Values: []string{"b"},
Keys: []string{"a"},
Values: []string{"b"},
Element: newElement.YNode(),
})
assert.NoError(t, err)
@@ -204,8 +204,8 @@ func TestElementSetter(t *testing.T) {
"e": "f",
})
rn, err = node.Pipe(ElementSetterList{
Keys: []string{"x"},
Values: []string{"y"},
Keys: []string{"x"},
Values: []string{"y"},
Element: newElement.YNode(),
})
assert.NoError(t, err)
@@ -370,7 +370,7 @@ func TestElementMatcherWithNoValue(t *testing.T) {
assert.Equal(t, "b: \"\"\n", assertNoErrorString(t)(rn.String()))
rn, err = node.Pipe(ElementMatcher{Keys: []string{"a"}})
assert.Error(t, err)
assert.NoError(t, err)
assert.Nil(t, rn)
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()))
_, 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) {