From 1ed8e7017eaff6c934c12c60f9182b0c87c09e1f Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 8 Aug 2017 17:04:30 +1000 Subject: [PATCH] Can create arrays --- yaml.go | 13 +++++++++++-- yaml_test.go | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/yaml.go b/yaml.go index 49784c0067e..2f1d26ecd97 100644 --- a/yaml.go +++ b/yaml.go @@ -160,9 +160,18 @@ func newYaml(args []string) interface{} { writeCommands[0] = yaml.MapItem{Key: args[0], Value: parseValue(args[1])} } - parsedData := make(yaml.MapSlice, 0) + var parsedData yaml.MapSlice + var prependCommand = "" + var isArray = strings.HasPrefix(writeCommands[0].Key.(string), "[") + if isArray { + item := yaml.MapItem{Key: "thing", Value: make(yaml.MapSlice, 0)} + parsedData = yaml.MapSlice{item} + prependCommand = "thing" + } else { + parsedData = make(yaml.MapSlice, 0) + } - return updateParsedData(parsedData, writeCommands, "") + return updateParsedData(parsedData, writeCommands, prependCommand) } func writeProperty(cmd *cobra.Command, args []string) { diff --git a/yaml_test.go b/yaml_test.go index a4a8e32d2b1..98ad44f1525 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -55,6 +55,14 @@ func TestNewYaml(t *testing.T) { formattedResult) } +func TestNewYamlArray(t *testing.T) { + result := newYaml([]string{"[0].cat", "meow"}) + formattedResult := fmt.Sprintf("%v", result) + assertResult(t, + "[[{cat meow}]]", + formattedResult) +} + func TestUpdateYaml(t *testing.T) { result := updateYaml([]string{"sample.yaml", "b.c", "3"}) formattedResult := fmt.Sprintf("%v", result)