From 53a4a47ce306edead2dc9f059c488ec02f9d963d Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 15 Dec 2019 18:38:40 +1100 Subject: [PATCH] wip - prefix splat --- commands_test.go | 89 +++++++++++++++++-------------------- examples/data2.yaml | 2 + pkg/yqlib/data_navigator.go | 10 +++-- 3 files changed, 48 insertions(+), 53 deletions(-) diff --git a/commands_test.go b/commands_test.go index ed3fd562..8e09b81d 100644 --- a/commands_test.go +++ b/commands_test.go @@ -235,15 +235,14 @@ func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } -// func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) { -// cmd := getRootCommand() -// result := test.RunCmd(cmd, "read examples/array.yaml [*].roles[x]") -// if result.Error == nil { -// 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.Error.Error()) -// } +func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read examples/array.yaml [*].roles[x]") + expectedOutput := `- +- +` + test.AssertResult(t, expectedOutput, result.Output) +} func TestReadCmd_Error(t *testing.T) { cmd := getRootCommand() @@ -280,27 +279,26 @@ func TestReadCmd_ErrorUnreadableFile(t *testing.T) { test.AssertResult(t, expectedOutput, result.Error.Error()) } -// func TestReadCmd_ErrorBadPath(t *testing.T) { -// content := `b: -// d: -// e: -// - 3 -// - 4 -// f: -// - 1 -// - 2 -// ` -// filename := test.WriteTempYamlFile(content) -// defer test.RemoveTempYamlFile(filename) +func TestReadCmd_ErrorBadPath(t *testing.T) { + content := `b: + d: + e: + - 3 + - 4 + f: + - 1 + - 2 +` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) -// cmd := getRootCommand() -// result := test.RunCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename)) -// if result.Error == nil { -// 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.Error.Error()) -// } + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename)) + expectedOutput := `- +- +` + test.AssertResult(t, expectedOutput, result.Output) +} func TestReadCmd_Verbose(t *testing.T) { cmd := getRootCommand() @@ -878,35 +876,28 @@ b: } func TestDeleteSplatYaml(t *testing.T) { - content := `a: 2 -b: - hi: - c: things - d: something else - hello: - c: things2 - d: something else2 - there: - c: more things - d: more something else + content := `a: other +b: [3, 4] +c: + toast: leave + test: 1 + tell: 1 + taco: cool ` filename := test.WriteTempYamlFile(content) defer test.RemoveTempYamlFile(filename) 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 { t.Error(result.Error) } - expectedOutput := `a: 2 -b: - hi: - d: something else - hello: - d: something else2 - there: - d: more something else + expectedOutput := `a: other +b: [3, 4] +c: + toast: leave + taco: cool ` test.AssertResult(t, expectedOutput, result.Output) } diff --git a/examples/data2.yaml b/examples/data2.yaml index 7969f1ec..93764e94 100644 --- a/examples/data2.yaml +++ b/examples/data2.yaml @@ -1,5 +1,7 @@ a: other b: [3, 4] c: + toast: leave test: 1 tell: 1 + taco: cool diff --git a/pkg/yqlib/data_navigator.go b/pkg/yqlib/data_navigator.go index 438dfd9d..66ee359b 100644 --- a/pkg/yqlib/data_navigator.go +++ b/pkg/yqlib/data_navigator.go @@ -220,10 +220,12 @@ type mapVisitorFn func(int) error func (n *navigator) visitMatchingEntries(contents []*yaml.Node, key string, visit mapVisitorFn) (bool, error) { visited := false - for index, content := range contents { - // value.Content is a concatenated array of key, value, - // so keys are in the even indexes, values in odd. - if index%2 == 0 && (n.matchesKey(key, content.Value)) { + + // value.Content is a concatenated array of key, value, + // so keys are in the even indexes, values in odd. + for index := 0; index < len(contents); index = index + 2 { + content := contents[index] + if n.matchesKey(key, content.Value) { errorVisiting := visit(index) if errorVisiting != nil { return visited, errorVisiting