From 5520c0bca60ad433e1274ca71317cfd5f0459b1d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 9 May 2023 17:17:18 -0700 Subject: [PATCH] fix map carry --- d2oracle/edit.go | 5 +- d2oracle/edit_test.go | 20 ++ .../include_descendants_move_out.exp.json | 332 ++++++++++++++++++ 3 files changed, 356 insertions(+), 1 deletion(-) create mode 100644 testdata/d2oracle/TestMove/include_descendants_move_out.exp.json diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 29d365962..47c620acd 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -1613,7 +1613,10 @@ func move(g *d2graph.Graph, key, newKey string, includeDescendants bool) (*d2gra } else { detachedMK.Key.Path = []*d2ast.StringBox{ref.Key.Path[ref.KeyPathIndex]} } - if includeDescendants || ref.KeyPathIndex == len(filterReservedPath(ref.Key.Path))-1 { + if includeDescendants { + detachedMK.Value = ref.MapKey.Value + ref.MapKey.Value = d2ast.ValueBox{} + } else if ref.KeyPathIndex == len(filterReservedPath(ref.Key.Path))-1 { withReserved, withoutReserved := filterReserved(ref.MapKey.Value) detachedMK.Value = withReserved ref.MapKey.Value = withoutReserved diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 0cc62106e..aa689f696 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -4306,6 +4306,26 @@ C.shape: circle exp: `A C.C.shape: circle +`, + }, + { + name: "include_descendants_move_out", + text: `a.b: { + c: { + d + } +} +`, + key: `a.b`, + newKey: `b`, + includeDescendants: true, + + exp: `a +b: { + c: { + d + } +} `, }, } diff --git a/testdata/d2oracle/TestMove/include_descendants_move_out.exp.json b/testdata/d2oracle/TestMove/include_descendants_move_out.exp.json new file mode 100644 index 000000000..db22e48e9 --- /dev/null +++ b/testdata/d2oracle/TestMove/include_descendants_move_out.exp.json @@ -0,0 +1,332 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,0:0:0-6:0:26", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,0:0:0-0:1:1", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,1:0:2-5:1:25", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,1:0:2-1:1:3", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,1:0:2-1:1:3", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,1:3:5-5:0:24", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,2:2:9-4:3:23", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,2:2:9-2:3:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,2:2:9-2:3:10", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,2:5:12-4:2:22", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,3:4:18-3:5:19", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,3:4:18-3:5:19", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,3:4:18-3:5:19", + "value": [ + { + "string": "d", + "raw_string": "d" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.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": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,1:0:2-1:1:3", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,1:0:2-1:1:3", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "c", + "id_val": "c", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,2:2:9-2:3:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,2:2:9-2:3:10", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "c" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "d", + "id_val": "d", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,3:4:18-3:5:19", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_move_out.d2,3:4:18-3:5:19", + "value": [ + { + "string": "d", + "raw_string": "d" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "d" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}