mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
changed handling of empty values
This commit is contained in:
@@ -48,6 +48,7 @@ func (a ElementAppender) Filter(rn *RNode) (*RNode, error) {
|
|||||||
// not designed for this purpose. To append an element, please use ElementAppender.
|
// not designed for this purpose. To append an element, please use ElementAppender.
|
||||||
// To replace, set the key-value pair and a non-nil Element.
|
// To replace, set the key-value pair and a non-nil Element.
|
||||||
// To delete, set the key-value pair and leave the Element as nil.
|
// To delete, set the key-value pair and leave the Element as nil.
|
||||||
|
// Every key must have a corresponding value.
|
||||||
type ElementSetter struct {
|
type ElementSetter struct {
|
||||||
Kind string `yaml:"kind,omitempty"`
|
Kind string `yaml:"kind,omitempty"`
|
||||||
|
|
||||||
@@ -78,9 +79,6 @@ func (e ElementSetter) Filter(rn *RNode) (*RNode, error) {
|
|||||||
if len(e.Keys) == 0 {
|
if len(e.Keys) == 0 {
|
||||||
e.Keys = append(e.Keys, "")
|
e.Keys = append(e.Keys, "")
|
||||||
}
|
}
|
||||||
if len(e.Values) == 0 {
|
|
||||||
e.Values = append(e.Values, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ErrorIfInvalid(rn, SequenceNode); err != nil {
|
if err := ErrorIfInvalid(rn, SequenceNode); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -103,16 +101,16 @@ func (e ElementSetter) Filter(rn *RNode) (*RNode, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(e.Keys) > len(e.Values) {
|
|
||||||
return nil, fmt.Errorf("cannot have more keys than values")
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if this is the element we are matching
|
// check if this is the element we are matching
|
||||||
var val *RNode
|
var val *RNode
|
||||||
var err error
|
var err error
|
||||||
found := true
|
found := true
|
||||||
for j := range e.Keys {
|
for j := range e.Keys {
|
||||||
val, err = newNode.Pipe(FieldMatcher{Name: e.Keys[j], StringValue: e.Values[j]})
|
if j >= len(e.Values) {
|
||||||
|
val, err = newNode.Pipe(FieldMatcher{Name: e.Keys[j], StringValue: ""})
|
||||||
|
} else {
|
||||||
|
val, err = newNode.Pipe(FieldMatcher{Name: e.Keys[j], StringValue: e.Values[j]})
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -123,7 +121,9 @@ func (e ElementSetter) Filter(rn *RNode) (*RNode, error) {
|
|||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
// not the element we are looking for, keep it in the Content
|
// not the element we are looking for, keep it in the Content
|
||||||
newContent = append(newContent, elem)
|
if len(e.Values) > 0 {
|
||||||
|
newContent = append(newContent, elem)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
matchingElementFound = true
|
matchingElementFound = true
|
||||||
|
|||||||
@@ -132,22 +132,6 @@ func TestElementSetter(t *testing.T) {
|
|||||||
_, err = node.Pipe(ElementSetter{Keys: []string{"a"}})
|
_, err = node.Pipe(ElementSetter{Keys: []string{"a"}})
|
||||||
assert.EqualError(t, err, "wrong Node Kind for expected: MappingNode was ScalarNode: value: {scalarValue}")
|
assert.EqualError(t, err, "wrong Node Kind for expected: MappingNode was ScalarNode: value: {scalarValue}")
|
||||||
|
|
||||||
node = MustParse(`
|
|
||||||
- a: b
|
|
||||||
- c: d
|
|
||||||
`)
|
|
||||||
// {a: b} is removed since the value is omitted and only key is used
|
|
||||||
// to match and no Element specified.
|
|
||||||
rn, err = node.Pipe(ElementSetter{Keys: []string{"a"}})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Nil(t, rn)
|
|
||||||
assert.Equal(t, `- c: d
|
|
||||||
`, assertNoErrorString(t)(node.String()))
|
|
||||||
|
|
||||||
node = MustParse(`
|
|
||||||
- a: b
|
|
||||||
- c: d
|
|
||||||
`)
|
|
||||||
// Return error because ElementSetter will assume all elements are scalar when
|
// Return error because ElementSetter will assume all elements are scalar when
|
||||||
// there is only value provided.
|
// there is only value provided.
|
||||||
_, err = node.Pipe(ElementSetter{Values: []string{"b"}})
|
_, err = node.Pipe(ElementSetter{Values: []string{"b"}})
|
||||||
@@ -342,20 +326,6 @@ func TestElementSetterMultipleKeys(t *testing.T) {
|
|||||||
- e: f
|
- e: f
|
||||||
- g: h
|
- g: h
|
||||||
`, assertNoErrorString(t)(node.String()))
|
`, assertNoErrorString(t)(node.String()))
|
||||||
|
|
||||||
node = orig.Copy()
|
|
||||||
// Should return an error
|
|
||||||
// keys and values are not the same length
|
|
||||||
newElement = NewMapRNode(&map[string]string{
|
|
||||||
"g": "h",
|
|
||||||
})
|
|
||||||
rn, err = node.Pipe(ElementSetter{
|
|
||||||
Keys: []string{"a", "c"},
|
|
||||||
Values: []string{"b"},
|
|
||||||
Element: newElement.YNode(),
|
|
||||||
})
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.Nil(t, rn)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestElementMatcherWithNoValue(t *testing.T) {
|
func TestElementMatcherWithNoValue(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user