Merge arrays!

This commit is contained in:
Mike Farah 2020-01-05 17:28:24 +13:00
parent 1aa5ec1d40
commit 1f7f1b0def
5 changed files with 29 additions and 17 deletions

10
Upgrade Notes Normal file
View File

@ -0,0 +1,10 @@
# New Features
- Keeps comments and formatting (e.g. inline arrays)!
- Handles anchors!
- Can specify yaml tags (e.g. !!int)
# Update scripts file format has changed
# Merge command
- autocreates missing entries in target by default, new flag to turn that off.

View File

@ -1342,34 +1342,26 @@ c:
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
func xTestMergeAppendCmd(t *testing.T) { func TestMergeAppendCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --append examples/data1.yaml examples/data2.yaml") result := test.RunCmd(cmd, "merge --autocreate=false --append examples/data1.yaml examples/data2.yaml")
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
expectedOutput := `a: simple expectedOutput := `a: simple
b: b: [1, 2, 3, 4]
- 1
- 2
- 3
- 4
c: c:
test: 1 test: 1
` `
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
func xTestMergeArraysCmd(t *testing.T) { func TestMergeArraysCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "merge --append examples/sample_array.yaml examples/sample_array_2.yaml") result := test.RunCmd(cmd, "merge --append examples/sample_array.yaml examples/sample_array_2.yaml")
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
expectedOutput := `- 1 expectedOutput := `[1, 2, 3, 4, 5]
- 2
- 3
- 4
- 5
` `
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }

View File

@ -1 +1,2 @@
[4,5] - 4
- 5

View File

@ -34,11 +34,20 @@ func DebugNode(value *yaml.Node) {
} }
func PathStackToString(pathStack []interface{}) string { func PathStackToString(pathStack []interface{}) string {
return MergePathStackToString(pathStack, false)
}
func MergePathStackToString(pathStack []interface{}, appendArrays bool) string {
var sb strings.Builder var sb strings.Builder
for index, path := range pathStack { for index, path := range pathStack {
switch path.(type) { switch path.(type) {
case int: case int:
sb.WriteString(fmt.Sprintf("[%v]", path)) if appendArrays {
sb.WriteString("[+]")
} else {
sb.WriteString(fmt.Sprintf("[%v]", path))
}
default: default:
sb.WriteString(fmt.Sprintf("%v", path)) sb.WriteString(fmt.Sprintf("%v", path))
} }

4
yq.go
View File

@ -238,7 +238,7 @@ Note that if you set both flags only overwrite will take effect.
cmdMerge.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace") cmdMerge.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace")
cmdMerge.PersistentFlags().BoolVarP(&overwriteFlag, "overwrite", "x", false, "update the yaml file by overwriting existing values") cmdMerge.PersistentFlags().BoolVarP(&overwriteFlag, "overwrite", "x", false, "update the yaml file by overwriting existing values")
cmdMerge.PersistentFlags().BoolVarP(&autoCreateFlag, "autocreate", "c", true, "automatically create any missing entries") cmdMerge.PersistentFlags().BoolVarP(&autoCreateFlag, "autocreate", "c", true, "automatically create any missing entries")
// cmdMerge.PersistentFlags().BoolVarP(&appendFlag, "append", "a", false, "update the yaml file by appending array values") cmdMerge.PersistentFlags().BoolVarP(&appendFlag, "append", "a", false, "update the yaml file by appending array values")
// cmdMerge.PersistentFlags().BoolVarP(&allowEmptyFlag, "allow-empty", "e", false, "allow empty yaml files") // cmdMerge.PersistentFlags().BoolVarP(&allowEmptyFlag, "allow-empty", "e", false, "allow empty yaml files")
cmdMerge.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)") cmdMerge.PersistentFlags().StringVarP(&docIndex, "doc", "d", "0", "process document index number (0 based, * for all documents)")
return cmdMerge return cmdMerge
@ -435,7 +435,7 @@ func mergeProperties(cmd *cobra.Command, args []string) error {
return errorProcessingFile return errorProcessingFile
} }
for _, matchingNode := range matchingNodes { for _, matchingNode := range matchingNodes {
mergePath := yqlib.PathStackToString(matchingNode.PathStack) mergePath := yqlib.MergePathStackToString(matchingNode.PathStack, appendFlag)
updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag}) updateCommands = append(updateCommands, yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag})
} }
} }