diff --git a/d2ast/d2ast.go b/d2ast/d2ast.go index 282c3509f..63a7d07f9 100644 --- a/d2ast/d2ast.go +++ b/d2ast/d2ast.go @@ -611,9 +611,13 @@ func (mk1 *Key) Equals(mk2 *Key) bool { return false } if (mk1.Value.Map == nil) != (mk2.Value.Map == nil) { - return false - } - if (mk1.Value.Unbox() == nil) != (mk2.Value.Unbox() == nil) { + if mk1.Value.Map != nil && len(mk1.Value.Map.Nodes) > 0 { + return false + } + if mk2.Value.Map != nil && len(mk2.Value.Map.Nodes) > 0 { + return false + } + } else if (mk1.Value.Unbox() == nil) != (mk2.Value.Unbox() == nil) { return false } @@ -638,7 +642,7 @@ func (mk1 *Key) Equals(mk2 *Key) bool { } } - if mk1.Value.Map != nil { + if mk1.Value.Map != nil && len(mk1.Value.Map.Nodes) > 0 { if len(mk1.Value.Map.Nodes) != len(mk2.Value.Map.Nodes) { return false } diff --git a/d2oracle/edit.go b/d2oracle/edit.go index fc0a5b784..29d365962 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -1726,7 +1726,7 @@ func move(g *d2graph.Graph, key, newKey string, includeDescendants bool) (*d2gra } if includeDescendants { - ref.Key.Path = append(ref.Key.Path[:go2.Max(0, ref.KeyPathIndex-len(newPath))], append(newPath, ref.Key.Path[go2.Min(len(ref.Key.Path), ref.KeyPathIndex+len(newPath)):]...)...) + ref.Key.Path = append(newPath, ref.Key.Path[go2.Min(len(ref.Key.Path), ref.KeyPathIndex+len(newPath)):]...) } else { ref.Key.Path = newPath } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index a4b1cc500..0cc62106e 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -4157,6 +4157,30 @@ y.b y.b: { z } +`, + }, + { + name: "include_descendants_underscore", + text: `github.code -> local.dev + +github: { + _.local.dev -> _.aws.workflows + _.aws: { + workflows + } +} +`, + key: `aws.workflows`, + newKey: `github.workflows`, + includeDescendants: true, + + exp: `github.code -> local.dev + +github: { + _.local.dev -> workflows + _.aws + workflows +} `, }, { diff --git a/testdata/d2oracle/TestMove/include_descendants_underscore.exp.json b/testdata/d2oracle/TestMove/include_descendants_underscore.exp.json new file mode 100644 index 000000000..32dbb24e5 --- /dev/null +++ b/testdata/d2oracle/TestMove/include_descendants_underscore.exp.json @@ -0,0 +1,788 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-7:0:85", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-0:24:24", + "edges": [ + { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-0:24:24", + "src": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-0:11:11", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-0:6:6", + "value": [ + { + "string": "github", + "raw_string": "github" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:7:7-0:11:11", + "value": [ + { + "string": "code", + "raw_string": "code" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:15:15-0:24:24", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:15:15-0:20:20", + "value": [ + { + "string": "local", + "raw_string": "local" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:21:21-0:24:24", + "value": [ + { + "string": "dev", + "raw_string": "dev" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,2:0:26-6:1:84", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,2:0:26-2:6:32", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,2:0:26-2:6:32", + "value": [ + { + "string": "github", + "raw_string": "github" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,2:8:34-6:0:83", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:2:38-3:26:62", + "edges": [ + { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:2:38-3:26:62", + "src": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:2:38-3:13:49", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:2:38-3:3:39", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:4:40-3:9:45", + "value": [ + { + "string": "local", + "raw_string": "local" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:10:46-3:13:49", + "value": [ + { + "string": "dev", + "raw_string": "dev" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:17:53-3:26:62", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:17:53-3:26:62", + "value": [ + { + "string": "workflows", + "raw_string": "workflows" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,4:2:65-4:7:70", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,4:2:65-4:7:70", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,4:2:65-4:3:66", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,4:4:67-4:7:70", + "value": [ + { + "string": "aws", + "raw_string": "aws" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,5:2:73-5:11:82", + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,5:2:73-5:11:82", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,5:2:73-5:11:82", + "value": [ + { + "string": "workflows", + "raw_string": "workflows" + } + ] + } + } + ] + }, + "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": [ + { + "index": 0, + "isCurve": false, + "src_arrow": false, + "dst_arrow": true, + "references": [ + { + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "index": 0, + "isCurve": false, + "src_arrow": false, + "dst_arrow": true, + "references": [ + { + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ], + "objects": [ + { + "id": "github", + "id_val": "github", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-0:11:11", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-0:6:6", + "value": [ + { + "string": "github", + "raw_string": "github" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:7:7-0:11:11", + "value": [ + { + "string": "code", + "raw_string": "code" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + }, + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,2:0:26-2:6:32", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,2:0:26-2:6:32", + "value": [ + { + "string": "github", + "raw_string": "github" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "github" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "code", + "id_val": "code", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-0:11:11", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:0:0-0:6:6", + "value": [ + { + "string": "github", + "raw_string": "github" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:7:7-0:11:11", + "value": [ + { + "string": "code", + "raw_string": "code" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "code" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "local", + "id_val": "local", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:15:15-0:24:24", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:15:15-0:20:20", + "value": [ + { + "string": "local", + "raw_string": "local" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:21:21-0:24:24", + "value": [ + { + "string": "dev", + "raw_string": "dev" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + }, + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:2:38-3:13:49", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:2:38-3:3:39", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:4:40-3:9:45", + "value": [ + { + "string": "local", + "raw_string": "local" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:10:46-3:13:49", + "value": [ + { + "string": "dev", + "raw_string": "dev" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "local" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "dev", + "id_val": "dev", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:15:15-0:24:24", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:15:15-0:20:20", + "value": [ + { + "string": "local", + "raw_string": "local" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,0:21:21-0:24:24", + "value": [ + { + "string": "dev", + "raw_string": "dev" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": 0 + }, + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:2:38-3:13:49", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:2:38-3:3:39", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:4:40-3:9:45", + "value": [ + { + "string": "local", + "raw_string": "local" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:10:46-3:13:49", + "value": [ + { + "string": "dev", + "raw_string": "dev" + } + ] + } + } + ] + }, + "key_path_index": 2, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "dev" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "workflows", + "id_val": "workflows", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:17:53-3:26:62", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,3:17:53-3:26:62", + "value": [ + { + "string": "workflows", + "raw_string": "workflows" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + }, + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,5:2:73-5:11:82", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,5:2:73-5:11:82", + "value": [ + { + "string": "workflows", + "raw_string": "workflows" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "workflows" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "aws", + "id_val": "aws", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,4:2:65-4:7:70", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,4:2:65-4:3:66", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/include_descendants_underscore.d2,4:4:67-4:7:70", + "value": [ + { + "string": "aws", + "raw_string": "aws" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "aws" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}