diff --git a/data_navigator.go b/data_navigator.go index 1d2e6e1d..e6db1a34 100644 --- a/data_navigator.go +++ b/data_navigator.go @@ -82,6 +82,9 @@ func writeArray(context interface{}, paths []string, value interface{}) []interf if err != nil { die("Error accessing array: %v", err) } + for index >= int64(len(array)) { + array = append(array, nil) + } currentChild := array[index] log.Debugf("\tcurrentChild %v\n", currentChild) diff --git a/data_navigator_test.go b/data_navigator_test.go index 1ec64038..ba54fc4c 100644 --- a/data_navigator_test.go +++ b/data_navigator_test.go @@ -205,6 +205,21 @@ b: assertResult(t, "4", readMap(updated, "b", []string{"d", "0"})) } +func TestWrite_add_to_array(t *testing.T) { + var data = parseData(` +b: + - aa +`) + + var expected = `b: +- aa +- bb` + + updated := writeMap(data, []string{"b", "1"}, "bb") + + assertResult(t, expected, yamlToString(updated)) +} + func TestWrite_with_no_tail(t *testing.T) { var data = parseData(` b: