Skip to content

Commit

Permalink
fix: correct visitMappingNodeFields
Browse files Browse the repository at this point in the history
This commit adjusts visitMappingNodeFields so that it no longer assumes
the mapping node has unique keys.
  • Loading branch information
ephesused committed Dec 22, 2022
1 parent 20b0d3c commit a0e94c1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
8 changes: 4 additions & 4 deletions kyaml/yaml/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,16 +647,16 @@ func (s MapEntrySetter) Filter(rn *RNode) (*RNode, error) {
}

content := rn.Content()
stillMissing := true
fieldStillNotFound := 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
fieldStillNotFound = false
}
return stillMissing
return fieldStillNotFound
})
if !stillMissing {
if !fieldStillNotFound {
return rn, nil
}

Expand Down
30 changes: 21 additions & 9 deletions kyaml/yaml/rnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func (rn *RNode) getMapFromMeta(fName string, fields ...string) map[string]strin
// fName is found in metadata; create the map from its content
expectedSize := len(fields)
if expectedSize == 0 {
expectedSize = len(fNameValue.Content) / 2
expectedSize = len(fNameValue.Content) / 2 //nolint: gomnd
}
result = make(map[string]string, expectedSize)

Expand Down Expand Up @@ -772,27 +772,39 @@ func (rn *RNode) VisitFields(fn func(node *MapNode) error) error {
// visitMappingNodeFields calls fn for fields in the content, in content order.
// The caller is responsible to ensure the node is a mapping node. If fieldNames
// are specified, then fn is called only for the fields that match the given
// fieldNames. fieldNames must contain unique values.
// fieldNames.
func visitMappingNodeFields(content []*yaml.Node, fn func(key, value *yaml.Node), fieldNames ...string) {
switch len(fieldNames) {
case 0: // visit all fields
visitFieldsWhileTrue(content, func(key, value *yaml.Node, _ int) bool {
fn(key, value)
return true
})
default: // visit specified fields
// assumption: fields in content have unique names
found := 0
case 1: // visit single field
visitFieldsWhileTrue(content, func(key, value *yaml.Node, _ int) bool {
if key == nil {
return true
}
if !sliceutil.Contains(fieldNames, key.Value) {
if fieldNames[0] == key.Value {
fn(key, value)
return false
}
return true
})
default: // visit specified fields
fieldsStillToVisit := make(map[string]bool, len(fieldNames))
for _, fieldName := range fieldNames {
fieldsStillToVisit[fieldName] = true
}
visitFieldsWhileTrue(content, func(key, value *yaml.Node, _ int) bool {
if key == nil {
return true
}
fn(key, value)
found++
return found < len(fieldNames)
if fieldsStillToVisit[key.Value] {
fn(key, value)
delete(fieldsStillToVisit, key.Value)
}
return len(fieldsStillToVisit) > 0
})
}
}
Expand Down

0 comments on commit a0e94c1

Please # to comment.