From 949df0342d5dd49665b48c2512a65416eedd0992 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Thu, 22 Dec 2022 19:29:13 -0800 Subject: [PATCH 1/4] 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 +} From 81b8606d8a271ee6bb79cc41a250baff1c9bec1a Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Thu, 22 Dec 2022 19:32:51 -0800 Subject: [PATCH 2/4] changelog --- ci/release/changelogs/next.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index af4479824..a4c14441b 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -13,3 +13,4 @@ - Fixed an issue with elk layouts accounting for edge labels as if they were placed on the side of the edge. [#483](https://github.com/terrastruct/d2/pull/483) - Fixed an issue where dagre layouts may not have enough spacing for all edge labels. [#484](https://github.com/terrastruct/d2/pull/484) - Fixed connections being clipped if they were at the very top or left edges of the diagram. [#493](https://github.com/terrastruct/d2/pull/493) +- Fixed edge case where the key `null` was compiling wrongly. [#507](https://github.com/terrastruct/d2/issues/507) From dd1de57200b7b146aaa42e298d60389fc0ee5e0f Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Mon, 26 Dec 2022 11:18:09 -0800 Subject: [PATCH 3/4] 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, From a7ad0f4e24425472582287f43857b7ec6ea87287 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Mon, 26 Dec 2022 11:18:43 -0800 Subject: [PATCH 4/4] go.mod: tidy --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 097cfe190..15acbb70b 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ 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 @@ -29,6 +28,7 @@ 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