mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Fixed special characters in path for merging
This commit is contained in:
parent
f5c3beb159
commit
a46386e093
@ -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
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user