mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Refactored merge - will allow more sophisticated mergin
This commit is contained in:
16
cmd/merge.go
16
cmd/merge.go
@@ -43,7 +43,7 @@ If append flag is set then existing arrays will be merged with the arrays from e
|
||||
*/
|
||||
func createReadFunctionForMerge() func(*yaml.Node) ([]*yqlib.NodeContext, error) {
|
||||
return func(dataBucket *yaml.Node) ([]*yqlib.NodeContext, error) {
|
||||
return lib.Get(dataBucket, "**", !appendFlag)
|
||||
return lib.GetForMerge(dataBucket, "**", !appendFlag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,9 +63,21 @@ func mergeProperties(cmd *cobra.Command, args []string) error {
|
||||
if errorProcessingFile != nil {
|
||||
return errorProcessingFile
|
||||
}
|
||||
log.Debugf("finished reading for merge!")
|
||||
for _, matchingNode := range matchingNodes {
|
||||
log.Debugf("matched node %v", lib.PathStackToString(matchingNode.PathStack))
|
||||
yqlib.DebugNode(matchingNode.Node)
|
||||
}
|
||||
for _, matchingNode := range matchingNodes {
|
||||
mergePath := lib.MergePathStackToString(matchingNode.PathStack, appendFlag)
|
||||
updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag})
|
||||
updateCommands = append(updateCommands, yqlib.UpdateCommand{
|
||||
Command: "merge",
|
||||
Path: mergePath,
|
||||
Value: matchingNode.Node,
|
||||
Overwrite: overwriteFlag,
|
||||
// dont update the content for nodes midway, only leaf nodes
|
||||
DontUpdateNodeContent: matchingNode.IsMiddleNode,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +129,11 @@ func TestMergeArraysCmd(t *testing.T) {
|
||||
if result.Error != nil {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `[1, 2, 3, 4, 5]
|
||||
expectedOutput := `- 1
|
||||
- 2
|
||||
- 3
|
||||
- 4
|
||||
- 5
|
||||
`
|
||||
test.AssertResult(t, expectedOutput, result.Output)
|
||||
}
|
||||
@@ -145,9 +149,7 @@ func TestMergeCmd_Multi(t *testing.T) {
|
||||
another:
|
||||
document: here
|
||||
a: simple # just the best
|
||||
b:
|
||||
- 1
|
||||
- 2
|
||||
b: [1, 2]
|
||||
c:
|
||||
test: 1
|
||||
---
|
||||
@@ -316,9 +318,7 @@ func TestMergeAllowEmptyTargetCmd(t *testing.T) {
|
||||
t.Error(result.Error)
|
||||
}
|
||||
expectedOutput := `a: simple # just the best
|
||||
b:
|
||||
- 1
|
||||
- 2
|
||||
b: [1, 2]
|
||||
c:
|
||||
test: 1
|
||||
`
|
||||
|
||||
@@ -17,7 +17,7 @@ type readDataFn func(dataBucket *yaml.Node) ([]*yqlib.NodeContext, error)
|
||||
|
||||
func createReadFunction(path string) func(*yaml.Node) ([]*yqlib.NodeContext, error) {
|
||||
return func(dataBucket *yaml.Node) ([]*yqlib.NodeContext, error) {
|
||||
return lib.Get(dataBucket, path, true)
|
||||
return lib.Get(dataBucket, path)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user