diff --git a/Upgrade Notes b/Upgrade Notes index 153ebf50..8567039b 100644 --- a/Upgrade Notes +++ b/Upgrade Notes @@ -1,3 +1,8 @@ +# Update doco / notes + - --autocreate=false to turn off default flags + - add comments to test yaml to ensure they are kept on updating. + - update built in command notes to includes quotes around path args. + # New Features - Keeps comments and formatting (e.g. inline arrays)! - Handles anchors! diff --git a/commands_test.go b/commands_test.go index 91b9cffe..6e4c5271 100644 --- a/commands_test.go +++ b/commands_test.go @@ -807,6 +807,18 @@ func TestNewCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestNewArrayCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "new b[0] 3") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `b: +- 3 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestNewCmd_Error(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "new b.c") @@ -1334,7 +1346,7 @@ func TestMergeOverwriteCmd(t *testing.T) { if result.Error != nil { t.Error(result.Error) } - expectedOutput := `a: other + expectedOutput := `a: other # better than the original b: [3, 4] c: test: 1 @@ -1348,7 +1360,7 @@ func TestMergeAppendCmd(t *testing.T) { if result.Error != nil { t.Error(result.Error) } - expectedOutput := `a: simple + expectedOutput := `a: simple # just the best b: [1, 2, 3, 4] c: test: 1 @@ -1366,26 +1378,27 @@ func TestMergeArraysCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } -func xTestMergeCmd_Multi(t *testing.T) { +func TestMergeCmd_Multi(t *testing.T) { cmd := getRootCommand() - result := test.RunCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data2.yaml") + result := test.RunCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data1.yaml") if result.Error != nil { t.Error(result.Error) } expectedOutput := `a: Easy! as one two three --- -a: other another: document: here +a: simple # just the best b: -- 3 -- 4 +- 1 +- 2 c: test: 1 --- - 1 -- 2` - test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n ")) +- 2 +` + test.AssertResult(t, expectedOutput, result.Output) } func xTestMergeYamlMultiAllCmd(t *testing.T) { diff --git a/examples/data1.yaml b/examples/data1.yaml index b0f017b1..6b87c47f 100644 --- a/examples/data1.yaml +++ b/examples/data1.yaml @@ -1,4 +1,4 @@ -a: simple +a: simple # just the best b: [1, 2] c: test: 1 diff --git a/examples/data2.yaml b/examples/data2.yaml index 93764e94..07088cc1 100644 --- a/examples/data2.yaml +++ b/examples/data2.yaml @@ -1,4 +1,4 @@ -a: other +a: other # better than the original b: [3, 4] c: toast: leave diff --git a/pkg/yqlib/data_navigator.go b/pkg/yqlib/data_navigator.go index 835c924b..86a89a21 100644 --- a/pkg/yqlib/data_navigator.go +++ b/pkg/yqlib/data_navigator.go @@ -234,7 +234,13 @@ func (n *navigator) recurseArray(value *yaml.Node, head string, tail []string, p if err != nil { return errors.Wrapf(err, "Error parsing array index '%v' for '%v'", head, PathStackToString(pathStack)) } + + for int64(len(value.Content)) <= index { + value.Content = append(value.Content, &yaml.Node{Kind: guessKind(head, tail, 0)}) + } + if index >= int64(len(value.Content)) { + log.Debug("index longer than array length, aborting!") return nil } value.Content[index] = n.getOrReplace(value.Content[index], guessKind(head, tail, value.Content[index].Kind))