diff --git a/d2oracle/edit.go b/d2oracle/edit.go index f1d56cb8f9..668b6d4a0d 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -2048,6 +2048,15 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err if !ok { return nil, nil } + + ignored := obj + for _, ch := range obj.ChildrenArray { + if ch.ID == obj.ID { + ignored = nil + break + } + } + for _, ch := range obj.ChildrenArray { // Record siblings as the unique key generated should not conflict with any siblings either var siblingsToBeHoisted []string @@ -2094,7 +2103,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err } if conflictingObj, ok := g.Root.HasChild(d2graph.Key(hoistedMK.Key)); (ok && conflictingObj != obj) || conflictsWithNewID { - newKey, _, err := generateUniqueKey(g, hoistedAbsID, obj, append(newIDs, siblingsToBeHoisted...)) + newKey, _, err := generateUniqueKey(g, hoistedAbsID, ignored, append(newIDs, siblingsToBeHoisted...)) if err != nil { return nil, err } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 6975264138..736fbcf69a 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -3526,6 +3526,29 @@ func TestDelete(t *testing.T) { key: `x`, exp: `x.y.z -> y.b +`, + }, + { + name: "duplicate_generated", + + text: `x +x 2 +x 3: { + x 3 + x 4 +} +x 4 +y +`, + key: `x 3`, + exp: `x +x 2 + +x 3 +x 5 + +x 4 +y `, }, { @@ -5503,6 +5526,24 @@ x.y.z.w.e.p.l -> x.y.z.1.2.3.4 exp: `{ "x.x": "x" +}`, + }, + { + name: "duplicate_generated", + + text: `x +x 2 +x 3: { + x 3 + x 4 +} +x 4 +y +`, + key: `x 3`, + exp: `{ + "x 3.x 3": "x 3", + "x 3.x 4": "x 5" }`, }, { diff --git a/testdata/d2oracle/TestDelete/duplicate_generated.exp.json b/testdata/d2oracle/TestDelete/duplicate_generated.exp.json new file mode 100644 index 0000000000..fe4d17adb0 --- /dev/null +++ b/testdata/d2oracle/TestDelete/duplicate_generated.exp.json @@ -0,0 +1,460 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,0:0:0-8:0:22", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,0:0:0-0:1:1", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,1:0:2-1:3:5", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,1:0:2-1:3:5", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,1:0:2-1:3:5", + "value": [ + { + "string": "x 2", + "raw_string": "x 2" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,3:0:7-3:3:10", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,3:0:7-3:3:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,3:0:7-3:3:10", + "value": [ + { + "string": "x 3", + "raw_string": "x 3" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,4:0:11-4:3:14", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,4:0:11-4:3:14", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,4:0:11-4:3:14", + "value": [ + { + "string": "x 5", + "raw_string": "x 5" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,6:0:16-6:3:19", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,6:0:16-6:3:19", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,6:0:16-6:3:19", + "value": [ + { + "string": "x 4", + "raw_string": "x 4" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,7:0:20-7:1:21", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,7:0:20-7:1:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,7:0:20-7:1:21", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "x", + "id_val": "x", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 2", + "id_val": "x 2", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,1:0:2-1:3:5", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,1:0:2-1:3:5", + "value": [ + { + "string": "x 2", + "raw_string": "x 2" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 2" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 3", + "id_val": "x 3", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,3:0:7-3:3:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,3:0:7-3:3:10", + "value": [ + { + "string": "x 3", + "raw_string": "x 3" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 3" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 5", + "id_val": "x 5", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,4:0:11-4:3:14", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,4:0:11-4:3:14", + "value": [ + { + "string": "x 5", + "raw_string": "x 5" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 5" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 4", + "id_val": "x 4", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,6:0:16-6:3:19", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,6:0:16-6:3:19", + "value": [ + { + "string": "x 4", + "raw_string": "x 4" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 4" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "y", + "id_val": "y", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,7:0:20-7:1:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/duplicate_generated.d2,7:0:20-7:1:21", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "y" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}