From ea9df0eedef3fe376952222fdc5638fd632d2899 Mon Sep 17 00:00:00 2001 From: Cory Cross Date: Tue, 4 Feb 2020 22:27:08 -0800 Subject: [PATCH] Fix path generation when merging file has period in key The program generates a path for every leaf node in the file-to-be-merged. It does not escape them if they contain a dot, as the path-expressions document mentions is necessary. Add in a test for this condition. Verified it fails without the fix. --- cmd/commands_test.go | 8 ++++---- examples/data2.yaml | 2 +- pkg/yqlib/lib.go | 10 +++++++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index f74d677e..872e4a22 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -1262,7 +1262,7 @@ c: toast: leave test: 1 tell: 1 - taco: cool + tasty.taco: cool ` filename := test.WriteTempYamlFile(content) defer test.RemoveTempYamlFile(filename) @@ -1277,7 +1277,7 @@ c: b: [3, 4] c: toast: leave - taco: cool + tasty.taco: cool ` test.AssertResult(t, expectedOutput, result.Output) } @@ -1443,7 +1443,7 @@ c: test: 1 toast: leave tell: 1 - taco: cool + tasty.taco: cool ` test.AssertResult(t, expectedOutput, result.Output) } @@ -1644,7 +1644,7 @@ c: test: 1 toast: leave tell: 1 - taco: cool + tasty.taco: cool ` test.AssertResult(t, expectedOutput, gotOutput) test.AssertResult(t, os.FileMode(int(0666)), info.Mode()) diff --git a/examples/data2.yaml b/examples/data2.yaml index 07088cc1..9efa4fc6 100644 --- a/examples/data2.yaml +++ b/examples/data2.yaml @@ -4,4 +4,4 @@ c: toast: leave test: 1 tell: 1 - taco: cool + tasty.taco: cool diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 196e3ddf..f4f3889c 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -51,7 +51,15 @@ func mergePathStackToString(pathStack []interface{}, appendArrays bool) string { } default: - sb.WriteString(fmt.Sprintf("%v", path)) + s := fmt.Sprintf("%v", path) + hasDot := strings.Contains(s, ".") + if hasDot { + sb.WriteString("[") + } + sb.WriteString(s) + if hasDot { + sb.WriteString("]") + } } if index < len(pathStack)-1 {