fix(toml): quote empty keys (#2870)

This commit is contained in:
Jake Wang
2026-09-24 12:23:18 +10:00
committed by GitHub
parent a58568d965
commit 2b91505c10
2 changed files with 35 additions and 0 deletions
+3
View File
@@ -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)
+32
View File
@@ -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,