From e785bab47493fbd33d9877cf410d360843355386 Mon Sep 17 00:00:00 2001 From: Natasha Sarkar Date: Thu, 5 Nov 2020 16:52:36 -0800 Subject: [PATCH] updated matchelementlist --- kyaml/yaml/fns.go | 8 +++++++- kyaml/yaml/fns_test.go | 16 ++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/kyaml/yaml/fns.go b/kyaml/yaml/fns.go index 72e44c6b6..fa1ad609b 100644 --- a/kyaml/yaml/fns.go +++ b/kyaml/yaml/fns.go @@ -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 } diff --git a/kyaml/yaml/fns_test.go b/kyaml/yaml/fns_test.go index febdd9923..9f976883f 100644 --- a/kyaml/yaml/fns_test.go +++ b/kyaml/yaml/fns_test.go @@ -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) {