From dd1de57200b7b146aaa42e298d60389fc0ee5e0f Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Mon, 26 Dec 2022 11:18:09 -0800 Subject: [PATCH] d2format: Escape null correctly --- d2compiler/compile_test.go | 2 +- d2format/escape.go | 4 ++++ d2format/escape_test.go | 2 +- testdata/d2compiler/TestCompile/null.exp.json | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 311dd2267..f72894721 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1656,7 +1656,7 @@ choo: { text: `null `, 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) }, }, diff --git a/d2format/escape.go b/d2format/escape.go index 8fad2892c..9789a9c2d 100644 --- a/d2format/escape.go +++ b/d2format/escape.go @@ -47,6 +47,10 @@ func escapeUnquotedValue(s string, inKey bool) string { return `""` } + if strings.EqualFold(s, "null") { + return `'null'` + } + var b strings.Builder for i, r := range s { switch r { diff --git a/d2format/escape_test.go b/d2format/escape_test.go index c0090c03f..b7e67837c 100644 --- a/d2format/escape_test.go +++ b/d2format/escape_test.go @@ -170,7 +170,7 @@ func TestEscapeUnquoted(t *testing.T) { { name: "null", str: `null`, - exp: `\null`, + exp: `'null'`, }, { name: "empty", diff --git a/testdata/d2compiler/TestCompile/null.exp.json b/testdata/d2compiler/TestCompile/null.exp.json index 220260a45..f38e28de6 100644 --- a/testdata/d2compiler/TestCompile/null.exp.json +++ b/testdata/d2compiler/TestCompile/null.exp.json @@ -53,7 +53,7 @@ "edges": null, "objects": [ { - "id": "null", + "id": "'null'", "id_val": "null", "label_dimensions": { "width": 0,