fixed tests for write array splat

This commit is contained in:
Mike Farah 2019-05-13 09:13:45 +10:00
parent 53289366a5
commit 323089eb64
4 changed files with 90 additions and 37 deletions

View File

@ -806,6 +806,46 @@ func TestWriteCmd_SplatArray(t *testing.T) {
assertResult(t, expectedOutput, result.Output)
}
func TestWriteCmd_SplatMap(t *testing.T) {
content := `b:
c: thing
d: another thing
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("write -v %s b.* new", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: new
d: new
`
assertResult(t, expectedOutput, result.Output)
}
func TestWriteCmd_SplatMapEmpty(t *testing.T) {
content := `b:
c: thing
d: another thing
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("write -v %s b.c.* new", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
c: thing
d: another thing
`
assertResult(t, expectedOutput, result.Output)
}
func TestDeleteYaml(t *testing.T) {
content := `a: 2
b:

View File

@ -8,14 +8,15 @@ import (
yaml "gopkg.in/mikefarah/yaml.v2"
)
func entryInSlice(context yaml.MapSlice, key interface{}) *yaml.MapItem {
func entriesInSlice(context yaml.MapSlice, key interface{}) []*yaml.MapItem {
var matches = make([]*yaml.MapItem, 0)
for idx := range context {
var entry = &context[idx]
if fmt.Sprintf("%v", entry.Key) == key {
return entry
if key == "*" || fmt.Sprintf("%v", entry.Key) == key {
matches = append(matches, entry)
}
}
return nil
return matches
}
func getMapSlice(context interface{}) yaml.MapSlice {
@ -41,27 +42,33 @@ func getArray(context interface{}) (array []interface{}, ok bool) {
return
}
func writeMap(context interface{}, paths []string, value interface{}) yaml.MapSlice {
func writeMap(context interface{}, paths []string, value interface{}) interface{} {
log.Debugf("writeMap with path %v for %v to set value %v\n", paths, context, value)
mapSlice := getMapSlice(context)
if len(paths) == 0 {
return mapSlice
return context
}
child := entryInSlice(mapSlice, paths[0])
if child == nil {
children := entriesInSlice(mapSlice, paths[0])
if len(children) == 0 && paths[0] == "*" {
log.Debugf("\tNo matches, return map as is")
return context
}
if len(children) == 0 {
newChild := yaml.MapItem{Key: paths[0]}
mapSlice = append(mapSlice, newChild)
child = entryInSlice(mapSlice, paths[0])
children = entriesInSlice(mapSlice, paths[0])
log.Debugf("\tAppended child at %v for mapSlice %v\n", paths[0], mapSlice)
}
log.Debugf("\tchild.Value %v\n", child.Value)
remainingPaths := paths[1:]
child.Value = updatedChildValue(child.Value, remainingPaths, value)
for _, child := range children {
child.Value = updatedChildValue(child.Value, remainingPaths, value)
}
log.Debugf("\tReturning mapSlice %v\n", mapSlice)
return mapSlice
}
@ -131,13 +138,24 @@ func readMap(context yaml.MapSlice, head string, tail []string) (interface{}, er
if head == "*" {
return readMapSplat(context, tail)
}
var value interface{}
entry := entryInSlice(context, head)
if entry != nil {
value = entry.Value
entries := entriesInSlice(context, head)
if len(entries) == 1 {
return calculateValue(entries[0].Value, tail)
} else if len(entries) == 0 {
return nil, nil
}
return calculateValue(value, tail)
var errInIdx error
values := make([]interface{}, len(entries))
for idx, entry := range entries {
values[idx], errInIdx = calculateValue(entry.Value, tail)
if errInIdx != nil {
log.Errorf("Error updating index %v in %v", idx, context)
return nil, errInIdx
}
}
return values, nil
}
func readMapSplat(context yaml.MapSlice, tail []string) (interface{}, error) {

View File

@ -4,8 +4,6 @@ import (
"fmt"
"sort"
"testing"
yaml "gopkg.in/mikefarah/yaml.v2"
)
func TestReadMap_simple(t *testing.T) {
@ -189,8 +187,7 @@ func TestWrite_really_simple(t *testing.T) {
`)
updated := writeMap(data, []string{"b"}, "4")
b := entryInSlice(updated, "b").Value
assertResult(t, "4", b)
assertResult(t, "[{b 4}]", fmt.Sprintf("%v", updated))
}
func TestWrite_simple(t *testing.T) {
@ -200,9 +197,7 @@ b:
`)
updated := writeMap(data, []string{"b", "c"}, "4")
b := entryInSlice(updated, "b").Value.(yaml.MapSlice)
c := entryInSlice(b, "c").Value
assertResult(t, "4", c)
assertResult(t, "[{b [{c 4}]}]", fmt.Sprintf("%v", updated))
}
func TestWrite_new(t *testing.T) {
@ -212,9 +207,7 @@ b:
`)
updated := writeMap(data, []string{"b", "d"}, "4")
b := entryInSlice(updated, "b").Value.(yaml.MapSlice)
d := entryInSlice(b, "d").Value
assertResult(t, "4", d)
assertResult(t, "[{b [{c 2} {d 4}]}]", fmt.Sprintf("%v", updated))
}
func TestWrite_new_deep(t *testing.T) {
@ -224,8 +217,7 @@ b:
`)
updated := writeMap(data, []string{"b", "d", "f"}, "4")
got, _ := readMap(updated, "b", []string{"d", "f"})
assertResult(t, "4", got)
assertResult(t, "[{b [{c 2} {d [{f 4}]}]}]", fmt.Sprintf("%v", updated))
}
func TestWrite_array(t *testing.T) {
@ -236,8 +228,7 @@ b:
updated := writeMap(data, []string{"b", "0"}, "bb")
b := entryInSlice(updated, "b").Value.([]interface{})
assertResult(t, "bb", b[0].(string))
assertResult(t, "[{b [bb]}]", fmt.Sprintf("%v", updated))
}
func TestWrite_new_array(t *testing.T) {
@ -247,8 +238,7 @@ b:
`)
updated := writeMap(data, []string{"b", "0"}, "4")
got, _ := readMap(updated, "b", []string{"0"})
assertResult(t, "4", got)
assertResult(t, "[{b [{c 2} {0 4}]}]", fmt.Sprintf("%v", updated))
}
func TestWrite_new_array_deep(t *testing.T) {
@ -270,10 +260,14 @@ func TestWrite_new_map_array_deep(t *testing.T) {
b:
c: 2
`)
var expected = `b:
c: 2
d:
- "4"`
updated := writeMap(data, []string{"b", "d", "0"}, "4")
got, _ := readMap(updated, "b", []string{"d", "0"})
assertResult(t, "4", got)
got, _ := yamlToString(updated)
assertResult(t, expected, got)
}
func TestWrite_add_to_array(t *testing.T) {
@ -298,8 +292,7 @@ b:
`)
updated := writeMap(data, []string{"b"}, "4")
b := entryInSlice(updated, "b").Value
assertResult(t, "4", fmt.Sprintf("%v", b))
assertResult(t, "[{b 4}]", fmt.Sprintf("%v", updated))
}
func TestWriteMap_no_paths(t *testing.T) {

View File

@ -0,0 +1,2 @@
5:
6: camel!