mirror of
https://github.com/mikefarah/yq.git
synced 2026-10-12 02:19:06 +00:00
fix(toml): quote empty keys (#2870)
This commit is contained in:
@@ -73,6 +73,9 @@ func (te *tomlEncoder) CanHandleAliases() bool {
|
||||
// in a TOML bare key. TOML bare keys may only contain ASCII letters, ASCII
|
||||
// digits, underscores, and dashes.
|
||||
func tomlKey(key string) string {
|
||||
if key == "" {
|
||||
return `""`
|
||||
}
|
||||
for _, r := range key {
|
||||
if (r < 'A' || r > 'Z') && (r < 'a' || r > 'z') && (r < '0' || r > '9') && r != '_' && r != '-' {
|
||||
return fmt.Sprintf("%q", key)
|
||||
|
||||
@@ -558,6 +558,38 @@ var tomlScenarios = []formatScenario{
|
||||
scenarioType: "decode",
|
||||
},
|
||||
// Roundtrip scenarios
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Roundtrip: empty root key",
|
||||
input: "\"\" = \"empty\"\nplain = \"value\"\n",
|
||||
expression: ".",
|
||||
expected: "\"\" = \"empty\"\nplain = \"value\"\n",
|
||||
scenarioType: "roundtrip",
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Roundtrip: empty nested table key",
|
||||
input: "[outer.\"\"]\nvalue = 1\n",
|
||||
expression: ".",
|
||||
expected: "[outer.\"\"]\nvalue = 1\n",
|
||||
scenarioType: "roundtrip",
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Roundtrip: empty inline table key",
|
||||
input: "entry = { \"\" = \"value\" }\n",
|
||||
expression: ".",
|
||||
expected: "entry = { \"\" = \"value\" }\n",
|
||||
scenarioType: "roundtrip",
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Roundtrip: empty array of tables key",
|
||||
input: "[[\"\"]]\nname = \"value\"\n",
|
||||
expression: ".",
|
||||
expected: "[[\"\"]]\nname = \"value\"\n",
|
||||
scenarioType: "roundtrip",
|
||||
},
|
||||
{
|
||||
description: "Roundtrip: inline table attribute",
|
||||
input: rtInlineTableAttr,
|
||||
|
||||
Reference in New Issue
Block a user