From 99c4215548fdfde1ce22ccbd07e2edf33e386a52 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 2 Jan 2024 13:15:53 -0800 Subject: [PATCH] fix set with imports --- d2oracle/edit.go | 3 + d2oracle/edit_test.go | 34 ++ d2oracle/get.go | 10 + testdata/d2oracle/TestSet/import/4.exp.json | 187 ++++----- testdata/d2oracle/TestSet/import/5.exp.json | 346 ++++++++++++++++ testdata/d2oracle/TestSet/import/6.exp.json | 414 ++++++++++++++++++++ 6 files changed, 888 insertions(+), 106 deletions(-) create mode 100644 testdata/d2oracle/TestSet/import/5.exp.json create mode 100644 testdata/d2oracle/TestSet/import/6.exp.json diff --git a/d2oracle/edit.go b/d2oracle/edit.go index a2cccce2b..c58c7f62e 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -391,6 +391,9 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string) maybeNewScope = ref.MapKey.Value.Map } } + } else if IsImported(g, obj) { + appendMapKey(scope, mk) + return nil } else { maybeNewScope = obj.Map } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index b18ad3786..b89f4e823 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -2071,6 +2071,40 @@ a`, exp: `...@yo a b.style.fill: red +`, + }, + { + name: "import/5", + + text: `a +x: { + ...@yo +}`, + fsTexts: map[string]string{ + "yo.d2": `b`, + }, + key: `x.b.style.fill`, + value: go2.Pointer(`red`), + exp: `a +x: { + ...@yo + b.style.fill: red +} +`, + }, + { + name: "import/6", + + text: `a +x: @yo`, + fsTexts: map[string]string{ + "yo.d2": `b`, + }, + key: `x.b.style.fill`, + value: go2.Pointer(`red`), + exp: `a +x: @yo +x.b.style.fill: red `, }, } diff --git a/d2oracle/get.go b/d2oracle/get.go index 6f2074a08..b75ad12d1 100644 --- a/d2oracle/get.go +++ b/d2oracle/get.go @@ -140,6 +140,16 @@ func GetParentID(g *d2graph.Graph, boardPath []string, absID string) (string, er return obj.Parent.AbsID(), nil } +func IsImported(g *d2graph.Graph, obj *d2graph.Object) bool { + for _, ref := range obj.References { + if ref.MapKey.Range.Path == g.AST.Range.Path { + return false + } + } + + return true +} + func GetObj(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Object { g = GetBoardGraph(g, boardPath) if g == nil { diff --git a/testdata/d2oracle/TestSet/import/4.exp.json b/testdata/d2oracle/TestSet/import/4.exp.json index e0b66f2f8..19de4ef48 100644 --- a/testdata/d2oracle/TestSet/import/4.exp.json +++ b/testdata/d2oracle/TestSet/import/4.exp.json @@ -3,17 +3,17 @@ "name": "", "isFolderOnly": false, "ast": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,0:0:0-3:0:27", + "range": "index.d2,0:0:0-3:0:27", "nodes": [ { "import": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,0:0:0-0:6:6", + "range": "index.d2,0:0:0-0:6:6", "spread": true, "pre": "", "path": [ { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,0:4:4-0:6:6", + "range": "index.d2,0:4:4-0:6:6", "value": [ { "string": "yo", @@ -27,13 +27,13 @@ }, { "map_key": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,1:0:7-1:1:8", + "range": "index.d2,1:0:7-1:1:8", "key": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,1:0:7-1:1:8", + "range": "index.d2,1:0:7-1:1:8", "path": [ { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,1:0:7-1:1:8", + "range": "index.d2,1:0:7-1:1:8", "value": [ { "string": "a", @@ -50,13 +50,13 @@ }, { "map_key": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:0:9-2:17:26", + "range": "index.d2,2:0:9-2:17:26", "key": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:0:9-2:12:21", + "range": "index.d2,2:0:9-2:12:21", "path": [ { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:0:9-2:1:10", + "range": "index.d2,2:0:9-2:1:10", "value": [ { "string": "b", @@ -67,7 +67,7 @@ }, { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:2:11-2:7:16", + "range": "index.d2,2:2:11-2:7:16", "value": [ { "string": "style", @@ -78,7 +78,7 @@ }, { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:8:17-2:12:21", + "range": "index.d2,2:8:17-2:12:21", "value": [ { "string": "fill", @@ -92,7 +92,7 @@ "primary": {}, "value": { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:14:23-2:17:26", + "range": "index.d2,2:14:23-2:17:26", "value": [ { "string": "red", @@ -130,107 +130,37 @@ }, "edges": null, "objects": [ - { - "id": "bb", - "id_val": "bb", - "references": [ - { - "key": { - "range": "d2/testdata/d2oracle/TestSet/import/yo.d2,0:0:0-0:2:2", - "path": [ - { - "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/yo.d2,0:0:0-0:2:2", - "value": [ - { - "string": "bb", - "raw_string": "bb" - } - ] - } - } - ] - }, - "key_path_index": 0, - "map_key_edge_index": -1 - } - ], - "attributes": { - "label": { - "value": "bb" - }, - "labelDimensions": { - "width": 0, - "height": 0 - }, - "style": {}, - "near_key": null, - "shape": { - "value": "rectangle" - }, - "direction": { - "value": "" - }, - "constraint": null - }, - "zIndex": 0 - }, - { - "id": "a", - "id_val": "a", - "references": [ - { - "key": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,1:0:7-1:1:8", - "path": [ - { - "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,1:0:7-1:1:8", - "value": [ - { - "string": "a", - "raw_string": "a" - } - ] - } - } - ] - }, - "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 - }, { "id": "b", "id_val": "b", "references": [ { "key": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:0:9-2:12:21", + "range": "yo.d2,0:0:0-0:1:1", "path": [ { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:0:9-2:1:10", + "range": "yo.d2,0:0:0-0:1:1", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + }, + { + "key": { + "range": "index.d2,2:0:9-2:12:21", + "path": [ + { + "unquoted_string": { + "range": "index.d2,2:0:9-2:1:10", "value": [ { "string": "b", @@ -241,7 +171,7 @@ }, { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:2:11-2:7:16", + "range": "index.d2,2:2:11-2:7:16", "value": [ { "string": "style", @@ -252,7 +182,7 @@ }, { "unquoted_string": { - "range": "d2/testdata/d2oracle/TestSet/import/4.d2,2:8:17-2:12:21", + "range": "index.d2,2:8:17-2:12:21", "value": [ { "string": "fill", @@ -290,6 +220,51 @@ "constraint": null }, "zIndex": 0 + }, + { + "id": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "index.d2,1:0:7-1:1:8", + "path": [ + { + "unquoted_string": { + "range": "index.d2,1:0:7-1:1:8", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "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 } ] }, diff --git a/testdata/d2oracle/TestSet/import/5.exp.json b/testdata/d2oracle/TestSet/import/5.exp.json new file mode 100644 index 000000000..fe34e8801 --- /dev/null +++ b/testdata/d2oracle/TestSet/import/5.exp.json @@ -0,0 +1,346 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "index.d2,0:0:0-5:0:38", + "nodes": [ + { + "map_key": { + "range": "index.d2,0:0:0-0:1:1", + "key": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "index.d2,1:0:2-4:1:37", + "key": { + "range": "index.d2,1:0:2-1:1:3", + "path": [ + { + "unquoted_string": { + "range": "index.d2,1:0:2-1:1:3", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "index.d2,1:3:5-4:1:37", + "nodes": [ + { + "import": { + "range": "index.d2,2:2:9-2:8:15", + "spread": true, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "index.d2,2:6:13-2:8:15", + "value": [ + { + "string": "yo", + "raw_string": "yo" + } + ] + } + } + ] + } + }, + { + "map_key": { + "range": "index.d2,3:2:18-3:19:35", + "key": { + "range": "index.d2,3:2:18-3:14:30", + "path": [ + { + "unquoted_string": { + "range": "index.d2,3:2:18-3:3:19", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,3:4:20-3:9:25", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,3:10:26-3:14:30", + "value": [ + { + "string": "fill", + "raw_string": "fill" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "index.d2,3:16:32-3:19:35", + "value": [ + { + "string": "red", + "raw_string": "red" + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "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": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "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 + }, + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "yo.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "yo.d2,0:0:0-0:1:1", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + }, + { + "key": { + "range": "index.d2,3:2:18-3:14:30", + "path": [ + { + "unquoted_string": { + "range": "index.d2,3:2:18-3:3:19", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,3:4:20-3:9:25", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,3:10:26-3:14:30", + "value": [ + { + "string": "fill", + "raw_string": "fill" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": { + "fill": { + "value": "red" + } + }, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "x", + "id_val": "x", + "references": [ + { + "key": { + "range": "index.d2,1:0:2-1:1:3", + "path": [ + { + "unquoted_string": { + "range": "index.d2,1:0:2-1:1:3", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": "" +} diff --git a/testdata/d2oracle/TestSet/import/6.exp.json b/testdata/d2oracle/TestSet/import/6.exp.json new file mode 100644 index 000000000..7c2f2b3e6 --- /dev/null +++ b/testdata/d2oracle/TestSet/import/6.exp.json @@ -0,0 +1,414 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "index.d2,0:0:0-3:0:29", + "nodes": [ + { + "map_key": { + "range": "index.d2,0:0:0-0:1:1", + "key": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "index.d2,1:0:2-1:6:8", + "key": { + "range": "index.d2,1:0:2-1:1:3", + "path": [ + { + "unquoted_string": { + "range": "index.d2,1:0:2-1:1:3", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "import": { + "range": "index.d2,1:3:5-1:6:8", + "spread": false, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "index.d2,1:4:6-1:6:8", + "value": [ + { + "string": "yo", + "raw_string": "yo" + } + ] + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "index.d2,2:0:9-2:19:28", + "key": { + "range": "index.d2,2:0:9-2:14:23", + "path": [ + { + "unquoted_string": { + "range": "index.d2,2:0:9-2:1:10", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:2:11-2:3:12", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:4:13-2:9:18", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:10:19-2:14:23", + "value": [ + { + "string": "fill", + "raw_string": "fill" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "index.d2,2:16:25-2:19:28", + "value": [ + { + "string": "red", + "raw_string": "red" + } + ] + } + } + } + } + ] + }, + "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": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "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 + }, + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "yo.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "yo.d2,0:0:0-0:1:1", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + }, + { + "key": { + "range": "index.d2,2:0:9-2:14:23", + "path": [ + { + "unquoted_string": { + "range": "index.d2,2:0:9-2:1:10", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:2:11-2:3:12", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:4:13-2:9:18", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:10:19-2:14:23", + "value": [ + { + "string": "fill", + "raw_string": "fill" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": { + "fill": { + "value": "red" + } + }, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "x", + "id_val": "x", + "references": [ + { + "key": { + "range": "index.d2,1:0:2-1:1:3", + "path": [ + { + "unquoted_string": { + "range": "index.d2,1:0:2-1:1:3", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + }, + { + "key": { + "range": "index.d2,2:0:9-2:14:23", + "path": [ + { + "unquoted_string": { + "range": "index.d2,2:0:9-2:1:10", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:2:11-2:3:12", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:4:13-2:9:18", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,2:10:19-2:14:23", + "value": [ + { + "string": "fill", + "raw_string": "fill" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": "" +}