@@ -38,17 +38,14 @@ func (op *Operation) Perform(doc *yaml.Node) error {
3838 return fmt .Errorf ("%s operation does not apply: doc is missing path: %s" , op .Op , op .Path )
3939 }
4040
41- var f func (parent * yaml.Node , match * yaml.Node )
41+ // function that will actually perform the patch operation
42+ var opFunc func (parent * yaml.Node , match * yaml.Node )
4243
4344 switch op .Op {
4445 case opAdd :
45- f = op .add
46-
47- var pathMatches bool
46+ opFunc = op .add
4847
4948 if len (matches ) > 0 {
50- pathMatches = true
51-
5249 if matches [0 ].Kind == yaml .MappingNode || matches [0 ].Kind == yaml .SequenceNode {
5350 break
5451 }
@@ -61,17 +58,17 @@ func (op *Operation) Perform(doc *yaml.Node) error {
6158 return fmt .Errorf ("could not add using path: %s" , op .Path )
6259 }
6360
64- if len (matches ) > 0 && pathMatches {
61+ if len (matches ) > 0 && len ( originalMatches ) > 0 {
6562 if matches [0 ].Kind == yaml .SequenceNode {
6663 matches = originalMatches
6764 break
68- } else {
69- // we are trying to overwrite an existing key in a map, don't do that!
70- return fmt .Errorf (
71- "attempting add operation for non array/object path '%s' which already exists" ,
72- op .Path ,
73- )
7465 }
66+
67+ // we are trying to overwrite an existing key in a map, don't do that!
68+ return fmt .Errorf (
69+ "attempting add operation for non array/object path '%s' which already exists" ,
70+ op .Path ,
71+ )
7572 }
7673
7774 parentPath := op .Path .getParentPath ()
@@ -83,17 +80,17 @@ func (op *Operation) Perform(doc *yaml.Node) error {
8380 op .Path = OpPath (parentPath )
8481
8582 case opRemove :
86- f = op .remove
83+ opFunc = op .remove
8784 case opReplace :
88- f = op .replace
85+ opFunc = op .replace
8986 default :
9087 return fmt .Errorf ("unexpected op: %s" , op .Op )
9188 }
9289
9390 for _ , match := range matches {
9491 parent := find (doc , containsChild (match ))
9592
96- f (parent , match )
93+ opFunc (parent , match )
9794 }
9895
9996 return nil
0 commit comments