From 0749d4fb41d1341d5a0edfb74b0e79ab3b5de2d5 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 26 Jun 2023 12:15:24 -0700 Subject: [PATCH] attribute --- d2compiler/compile_test.go | 20 +- d2ir/d2ir.go | 15 ++ .../nulls/basic/attribute.exp.json | 205 ++++++++++++++++++ 3 files changed, 230 insertions(+), 10 deletions(-) create mode 100644 testdata/d2compiler/TestCompile2/nulls/basic/attribute.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index db89c2319..a283166be 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2956,16 +2956,16 @@ a -> b assert.Equal(t, 0, len(g.Edges)) }, }, - // { - // name: "attribute", - // run: func(t *testing.T) { - // g := assertCompile(t, ` - // a.style.opacity: 0.2 - // a.style.opacity: null - // `, "") - // assert.Equal(t, "0.2", g.Objects[0].Attributes.Style.Opacity.Value) - // }, - // }, + { + name: "attribute", + run: func(t *testing.T) { + g := assertCompile(t, ` +a.style.opacity: 0.2 +a.style.opacity: null + `, "") + assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[0].Attributes.Style.Opacity) + }, + }, } for _, tc := range tca { diff --git a/d2ir/d2ir.go b/d2ir/d2ir.go index bb5ef5f8d..dc239975a 100644 --- a/d2ir/d2ir.go +++ b/d2ir/d2ir.go @@ -777,6 +777,21 @@ func (m *Map) DeleteField(ida ...string) *Field { } } m.Fields = append(m.Fields[:i], m.Fields[i+1:]...) + + // If a field was deleted from a keyword-holder keyword and that holder is empty, + // then that holder becomes meaningless and should be deleted too + parent := ParentField(f) + for keyword := range d2graph.ReservedKeywordHolders { + if parent != nil && parent.Name == keyword && len(parent.Map().Fields) == 0 { + styleParentMap := ParentMap(parent) + for i, f := range styleParentMap.Fields { + if f.Name == keyword { + styleParentMap.Fields = append(styleParentMap.Fields[:i], styleParentMap.Fields[i+1:]...) + break + } + } + } + } return f } if f.Map() != nil { diff --git a/testdata/d2compiler/TestCompile2/nulls/basic/attribute.exp.json b/testdata/d2compiler/TestCompile2/nulls/basic/attribute.exp.json new file mode 100644 index 000000000..8b29a9497 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/nulls/basic/attribute.exp.json @@ -0,0 +1,205 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,0:0:0-3:3:47", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:0:1-1:20:21", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:0:1-1:15:16", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:0:1-1:1:2", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:2:3-1:7:8", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:8:9-1:15:16", + "value": [ + { + "string": "opacity", + "raw_string": "opacity" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "number": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:17:18-1:20:21", + "raw": "0.2", + "value": "1/5" + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,2:0:22-2:21:43", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,2:0:22-2:15:37", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,2:0:22-2:1:23", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,2:2:24-2:7:29", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,2:8:30-2:15:37", + "value": [ + { + "string": "opacity", + "raw_string": "opacity" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "null": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,2:17:39-2:21:43" + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:0:1-1:15:16", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:0:1-1:1:2", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:2:3-1:7:8", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/attribute.d2,1:8:9-1:15:16", + "value": [ + { + "string": "opacity", + "raw_string": "opacity" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": null +}