From 949df0342d5dd49665b48c2512a65416eedd0992 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Thu, 22 Dec 2022 19:29:13 -0800 Subject: [PATCH] fix null key --- d2compiler/compile_test.go | 10 ++ d2format/escape.go | 4 - go.mod | 2 +- testdata/d2compiler/TestCompile/null.exp.json | 102 ++++++++++++++++++ 4 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 testdata/d2compiler/TestCompile/null.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 5923293fc..311dd2267 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1650,6 +1650,16 @@ choo: { } }, }, + { + name: "null", + + 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].IDVal) + }, + }, } for _, tc := range testCases { diff --git a/d2format/escape.go b/d2format/escape.go index b6267ea80..8fad2892c 100644 --- a/d2format/escape.go +++ b/d2format/escape.go @@ -47,10 +47,6 @@ 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/go.mod b/go.mod index 15acbb70b..097cfe190 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( cdr.dev/slog v1.4.2-0.20221206192828-e4803b10ae17 github.com/PuerkitoBio/goquery v1.8.0 github.com/alecthomas/chroma v0.10.0 + github.com/davecgh/go-spew v1.1.1 github.com/dop251/goja v0.0.0-20221118162653-d4bf6fde1b86 github.com/fsnotify/fsnotify v1.6.0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 @@ -28,7 +29,6 @@ require ( cloud.google.com/go/compute v1.7.0 // indirect github.com/andybalholm/cascadia v1.3.1 // indirect github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/dlclark/regexp2 v1.7.0 // indirect github.com/fatih/color v1.13.0 // indirect github.com/gin-gonic/gin v1.7.7 // indirect diff --git a/testdata/d2compiler/TestCompile/null.exp.json b/testdata/d2compiler/TestCompile/null.exp.json new file mode 100644 index 000000000..220260a45 --- /dev/null +++ b/testdata/d2compiler/TestCompile/null.exp.json @@ -0,0 +1,102 @@ +{ + "graph": { + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-1:0:5", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "value": [ + { + "string": "null", + "raw_string": "null" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "null", + "id_val": "null", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "value": [ + { + "string": "null", + "raw_string": "null" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "null" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": null +}