Fixed special characters in path for merging

This commit is contained in:
Mike Farah 2020-02-18 20:18:49 +11:00
parent f5c3beb159
commit a46386e093
2 changed files with 33 additions and 5 deletions

View File

@ -1999,6 +1999,27 @@ apples: red
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
func TestMergeSpecialCharacterKeysCmd(t *testing.T) {
content := ``
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
mergeContent := `key[bracket]: value
key.bracket: value
key"value": value
key'value': value
`
mergeFilename := test.WriteTempYamlFile(mergeContent)
defer test.RemoveTempYamlFile(mergeFilename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("merge %s %s", filename, mergeFilename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, mergeContent, result.Output)
}
func TestMergeYamlMultiAllOverwriteCmd(t *testing.T) { func TestMergeYamlMultiAllOverwriteCmd(t *testing.T) {
content := `b: content := `b:
c: 3 c: 3

View File

@ -71,13 +71,20 @@ func mergePathStackToString(pathStack []interface{}, appendArrays bool) string {
s := fmt.Sprintf("%v", path) s := fmt.Sprintf("%v", path)
var _, errParsingInt = strconv.ParseInt(s, 10, 64) // nolint var _, errParsingInt = strconv.ParseInt(s, 10, 64) // nolint
hasDot := strings.Contains(s, ".") hasSpecial := strings.Contains(s, ".") || strings.Contains(s, "[") || strings.Contains(s, "]") || strings.Contains(s, "\"")
if hasDot || errParsingInt == nil { hasDoubleQuotes := strings.Contains(s, "\"")
sb.WriteString("\"") wrappingCharacterStart := "\""
wrappingCharacterEnd := "\""
if hasDoubleQuotes {
wrappingCharacterStart = "("
wrappingCharacterEnd = ")"
}
if hasSpecial || errParsingInt == nil {
sb.WriteString(wrappingCharacterStart)
} }
sb.WriteString(s) sb.WriteString(s)
if hasDot || errParsingInt == nil { if hasSpecial || errParsingInt == nil {
sb.WriteString("\"") sb.WriteString(wrappingCharacterEnd)
} }
} }