Can create arrays

This commit is contained in:
Mike Farah 2017-08-08 17:04:30 +10:00
parent 5514d2300b
commit 1ed8e7017e
2 changed files with 19 additions and 2 deletions

13
yaml.go
View File

@ -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) {

View File

@ -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)