wip - prefix splat

This commit is contained in:
Mike Farah 2019-12-15 18:38:40 +11:00
parent 5988d0cffa
commit 53a4a47ce3
3 changed files with 48 additions and 53 deletions

View File

@ -235,15 +235,14 @@ func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
// func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) { func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) {
// cmd := getRootCommand() cmd := getRootCommand()
// result := test.RunCmd(cmd, "read examples/array.yaml [*].roles[x]") result := test.RunCmd(cmd, "read examples/array.yaml [*].roles[x]")
// if result.Error == nil { expectedOutput := `-
// t.Error("Expected command to fail due to invalid path") -
// } `
// expectedOutput := `Error reading path in document index 0: error accessing array: strconv.ParseInt: parsing "x": invalid syntax` test.AssertResult(t, expectedOutput, result.Output)
// test.AssertResult(t, expectedOutput, result.Error.Error()) }
// }
func TestReadCmd_Error(t *testing.T) { func TestReadCmd_Error(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
@ -280,27 +279,26 @@ func TestReadCmd_ErrorUnreadableFile(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Error.Error()) test.AssertResult(t, expectedOutput, result.Error.Error())
} }
// func TestReadCmd_ErrorBadPath(t *testing.T) { func TestReadCmd_ErrorBadPath(t *testing.T) {
// content := `b: content := `b:
// d: d:
// e: e:
// - 3 - 3
// - 4 - 4
// f: f:
// - 1 - 1
// - 2 - 2
// ` `
// filename := test.WriteTempYamlFile(content) filename := test.WriteTempYamlFile(content)
// defer test.RemoveTempYamlFile(filename) defer test.RemoveTempYamlFile(filename)
// cmd := getRootCommand() cmd := getRootCommand()
// result := test.RunCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename)) result := test.RunCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename))
// if result.Error == nil { expectedOutput := `-
// t.Fatal("Expected command to fail due to invalid path") -
// } `
// expectedOutput := `Error reading path in document index 0: error accessing array: strconv.ParseInt: parsing "x": invalid syntax` test.AssertResult(t, expectedOutput, result.Output)
// test.AssertResult(t, expectedOutput, result.Error.Error()) }
// }
func TestReadCmd_Verbose(t *testing.T) { func TestReadCmd_Verbose(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
@ -878,35 +876,28 @@ b:
} }
func TestDeleteSplatYaml(t *testing.T) { func TestDeleteSplatYaml(t *testing.T) {
content := `a: 2 content := `a: other
b: b: [3, 4]
hi: c:
c: things toast: leave
d: something else test: 1
hello: tell: 1
c: things2 taco: cool
d: something else2
there:
c: more things
d: more something else
` `
filename := test.WriteTempYamlFile(content) filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename) defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("delete -v %s b.*.c", filename)) result := test.RunCmd(cmd, fmt.Sprintf("delete -v %s c.te*", filename))
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
expectedOutput := `a: 2 expectedOutput := `a: other
b: b: [3, 4]
hi: c:
d: something else toast: leave
hello: taco: cool
d: something else2
there:
d: more something else
` `
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }

View File

@ -1,5 +1,7 @@
a: other a: other
b: [3, 4] b: [3, 4]
c: c:
toast: leave
test: 1 test: 1
tell: 1 tell: 1
taco: cool

View File

@ -220,10 +220,12 @@ type mapVisitorFn func(int) error
func (n *navigator) visitMatchingEntries(contents []*yaml.Node, key string, visit mapVisitorFn) (bool, error) { func (n *navigator) visitMatchingEntries(contents []*yaml.Node, key string, visit mapVisitorFn) (bool, error) {
visited := false visited := false
for index, content := range contents {
// value.Content is a concatenated array of key, value, // value.Content is a concatenated array of key, value,
// so keys are in the even indexes, values in odd. // so keys are in the even indexes, values in odd.
if index%2 == 0 && (n.matchesKey(key, content.Value)) { for index := 0; index < len(contents); index = index + 2 {
content := contents[index]
if n.matchesKey(key, content.Value) {
errorVisiting := visit(index) errorVisiting := visit(index)
if errorVisiting != nil { if errorVisiting != nil {
return visited, errorVisiting return visited, errorVisiting