mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
refactor: single function to visit mapping node content
Refactor mapping node content traversal so that all code paths execute through the same root function.
This commit is contained in:
@@ -197,36 +197,37 @@ func (c FieldClearer) Filter(rn *RNode) (*RNode, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(rn.Content()); i += 2 {
|
||||
// if name matches, remove these 2 elements from the list because
|
||||
// they are treated as a fieldName/fieldValue pair.
|
||||
if rn.Content()[i].Value == c.Name {
|
||||
if c.IfEmpty {
|
||||
if len(rn.Content()[i+1].Content) > 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// save the item we are about to remove
|
||||
removed := NewRNode(rn.Content()[i+1])
|
||||
if len(rn.YNode().Content) > i+2 {
|
||||
l := len(rn.YNode().Content)
|
||||
// remove from the middle of the list
|
||||
rn.YNode().Content = rn.Content()[:i]
|
||||
rn.YNode().Content = append(
|
||||
rn.YNode().Content,
|
||||
rn.Content()[i+2:l]...)
|
||||
} else {
|
||||
// remove from the end of the list
|
||||
rn.YNode().Content = rn.Content()[:i]
|
||||
}
|
||||
|
||||
// return the removed field name and value
|
||||
return removed, nil
|
||||
var removed *RNode
|
||||
visitFieldsWhileTrue(rn.Content(), func(key, value *yaml.Node, keyIndex int) bool {
|
||||
if key.Value != c.Name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
// nothing removed
|
||||
return nil, nil
|
||||
|
||||
// the name matches: remove these 2 elements from the list because
|
||||
// they are treated as a fieldName/fieldValue pair.
|
||||
if c.IfEmpty {
|
||||
if len(value.Content) > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// save the item we are about to remove
|
||||
removed = NewRNode(value)
|
||||
if len(rn.YNode().Content) > keyIndex+2 {
|
||||
l := len(rn.YNode().Content)
|
||||
// remove from the middle of the list
|
||||
rn.YNode().Content = rn.Content()[:keyIndex]
|
||||
rn.YNode().Content = append(
|
||||
rn.YNode().Content,
|
||||
rn.Content()[keyIndex+2:l]...)
|
||||
} else {
|
||||
// remove from the end of the list
|
||||
rn.YNode().Content = rn.Content()[:keyIndex]
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return removed, nil
|
||||
}
|
||||
|
||||
func MatchElement(field, value string) ElementMatcher {
|
||||
@@ -402,14 +403,15 @@ func (f FieldMatcher) Filter(rn *RNode) (*RNode, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(rn.Content()); i = IncrementFieldIndex(i) {
|
||||
isMatchingField := rn.Content()[i].Value == f.Name
|
||||
if isMatchingField {
|
||||
requireMatchFieldValue := f.Value != nil
|
||||
if !requireMatchFieldValue || rn.Content()[i+1].Value == f.Value.YNode().Value {
|
||||
return NewRNode(rn.Content()[i+1]), nil
|
||||
}
|
||||
var returnNode *RNode
|
||||
requireMatchFieldValue := f.Value != nil
|
||||
visitMappingNodeFields(rn.Content(), func(key, value *yaml.Node) {
|
||||
if !requireMatchFieldValue || value.Value == f.Value.YNode().Value {
|
||||
returnNode = NewRNode(value)
|
||||
}
|
||||
}, f.Name)
|
||||
if returnNode != nil {
|
||||
return returnNode, nil
|
||||
}
|
||||
|
||||
if f.Create != nil {
|
||||
@@ -643,13 +645,19 @@ func (s MapEntrySetter) Filter(rn *RNode) (*RNode, error) {
|
||||
if s.Name == "" {
|
||||
s.Name = GetValue(s.Key)
|
||||
}
|
||||
for i := 0; i < len(rn.Content()); i = IncrementFieldIndex(i) {
|
||||
isMatchingField := rn.Content()[i].Value == s.Name
|
||||
if isMatchingField {
|
||||
rn.Content()[i] = s.Key.YNode()
|
||||
rn.Content()[i+1] = s.Value.YNode()
|
||||
return rn, nil
|
||||
|
||||
content := rn.Content()
|
||||
stillMissing := true
|
||||
visitFieldsWhileTrue(content, func(key, value *yaml.Node, keyIndex int) bool {
|
||||
if key.Value == s.Name {
|
||||
content[keyIndex] = s.Key.YNode()
|
||||
content[keyIndex+1] = s.Value.YNode()
|
||||
stillMissing = false
|
||||
}
|
||||
return stillMissing
|
||||
})
|
||||
if !stillMissing {
|
||||
return rn, nil
|
||||
}
|
||||
|
||||
// create the field
|
||||
@@ -868,9 +876,3 @@ func SplitIndexNameValue(p string) (string, string, error) {
|
||||
}
|
||||
return parts[0], parts[1], nil
|
||||
}
|
||||
|
||||
// IncrementFieldIndex increments i to point to the next field name element in
|
||||
// a slice of Contents.
|
||||
func IncrementFieldIndex(i int) int {
|
||||
return i + 2
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user