d2format: Escape null correctly

This commit is contained in:
Anmol Sethi 2022-12-26 11:18:09 -08:00
parent 81b8606d8a
commit dd1de57200
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
4 changed files with 7 additions and 3 deletions

View file

@ -1656,7 +1656,7 @@ choo: {
text: `null text: `null
`, `,
assertions: func(t *testing.T, g *d2graph.Graph) { assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "null", g.Objects[0].ID) tassert.Equal(t, "'null'", g.Objects[0].ID)
tassert.Equal(t, "null", g.Objects[0].IDVal) tassert.Equal(t, "null", g.Objects[0].IDVal)
}, },
}, },

View file

@ -47,6 +47,10 @@ func escapeUnquotedValue(s string, inKey bool) string {
return `""` return `""`
} }
if strings.EqualFold(s, "null") {
return `'null'`
}
var b strings.Builder var b strings.Builder
for i, r := range s { for i, r := range s {
switch r { switch r {

View file

@ -170,7 +170,7 @@ func TestEscapeUnquoted(t *testing.T) {
{ {
name: "null", name: "null",
str: `null`, str: `null`,
exp: `\null`, exp: `'null'`,
}, },
{ {
name: "empty", name: "empty",

View file

@ -53,7 +53,7 @@
"edges": null, "edges": null,
"objects": [ "objects": [
{ {
"id": "null", "id": "'null'",
"id_val": "null", "id_val": "null",
"label_dimensions": { "label_dimensions": {
"width": 0, "width": 0,