From 28f61ad4e62774acf49a41f156e75652fd1cf5ad Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 15 Feb 2023 14:09:56 -0800 Subject: [PATCH 1/3] delete only underscore --- d2oracle/edit.go | 9 +- d2oracle/edit_test.go | 35 +++ .../only-underscore-nested.exp.json | 255 ++++++++++++++++++ .../TestDelete/only-underscore.exp.json | 185 +++++++++++++ 4 files changed, 480 insertions(+), 4 deletions(-) create mode 100644 testdata/d2oracle/TestDelete/only-underscore-nested.exp.json create mode 100644 testdata/d2oracle/TestDelete/only-underscore.exp.json diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 9fe1c5389..52dfd029f 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -773,13 +773,14 @@ func deleteObject(g *d2graph.Graph, key *d2ast.KeyPath, obj *d2graph.Object) (*d if len(ref.MapKey.Edges) == 0 { isSuffix := ref.KeyPathIndex == len(ref.Key.Path)-1 ref.Key.Path = append(ref.Key.Path[:ref.KeyPathIndex], ref.Key.Path[ref.KeyPathIndex+1:]...) - withoutReserved := go2.Filter(ref.Key.Path, func(x *d2ast.StringBox) bool { - _, ok := d2graph.ReservedKeywords[x.Unbox().ScalarString()] - return !ok + withoutSpecial := go2.Filter(ref.Key.Path, func(x *d2ast.StringBox) bool { + _, isReserved := d2graph.ReservedKeywords[x.Unbox().ScalarString()] + isSpecial := isReserved || x.Unbox().ScalarString() == "_" + return !isSpecial }) if obj.Attributes.Shape.Value == d2target.ShapeSQLTable || obj.Attributes.Shape.Value == d2target.ShapeClass { ref.MapKey.Value.Map = nil - } else if len(withoutReserved) == 0 { + } else if len(withoutSpecial) == 0 { hoistRefChildren(g, key, ref) deleteFromMap(ref.Scope, ref.MapKey) } else if ref.MapKey.Value.Unbox() == nil && diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 4c6a16cae..1de9246dd 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -3189,6 +3189,41 @@ c -> d exp: `books: { _.pipe } +`, + }, + { + name: "only-underscore", + + text: `guitar: { + books: { + _._.pipe + } +} +`, + key: `pipe`, + + exp: `guitar: { + books +} +`, + }, + { + name: "only-underscore-nested", + + text: `guitar: { + books: { + _._.pipe: { + a + } + } +} +`, + key: `pipe`, + + exp: `guitar: { + books +} +a `, }, { diff --git a/testdata/d2oracle/TestDelete/only-underscore-nested.exp.json b/testdata/d2oracle/TestDelete/only-underscore-nested.exp.json new file mode 100644 index 000000000..483e50180 --- /dev/null +++ b/testdata/d2oracle/TestDelete/only-underscore-nested.exp.json @@ -0,0 +1,255 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-4:0:22", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-2:1:19", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:8:8-2:0:18", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "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": "guitar", + "id_val": "guitar", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "guitar" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "books", + "id_val": "books", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "books" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "a", + "id_val": "a", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +} diff --git a/testdata/d2oracle/TestDelete/only-underscore.exp.json b/testdata/d2oracle/TestDelete/only-underscore.exp.json new file mode 100644 index 000000000..ce90b0614 --- /dev/null +++ b/testdata/d2oracle/TestDelete/only-underscore.exp.json @@ -0,0 +1,185 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-3:0:20", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-2:1:19", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:8:8-2:0:18", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "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": "guitar", + "id_val": "guitar", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "guitar" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "books", + "id_val": "books", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "books" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +} From 30905b6e622d11c131475e51274d50c3acea011d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 15 Feb 2023 14:13:31 -0800 Subject: [PATCH 2/3] add test for delete id deltas --- d2oracle/edit_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 1de9246dd..26c200c17 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -4778,6 +4778,23 @@ x.y.z.w.e.p.l -> x.y.z.1.2.3.4 "x.x": "x" }`, }, + + { + name: "only-reserved", + text: `guitar: { + books: { + _._.pipe: { + a + } + } +} +`, + key: `pipe`, + + exp: `{ + "pipe.a": "a" +}`, + }, { name: "delete_container_with_conflicts", From ba7ef0b6614eaa2a4c4e098e7853574263f965bf Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 15 Feb 2023 14:26:52 -0800 Subject: [PATCH 3/3] fix underscore move --- d2oracle/edit.go | 6 +- d2oracle/edit_test.go | 18 ++ .../nested-underscore-move-out.exp.json | 255 ++++++++++++++++++ 3 files changed, 276 insertions(+), 3 deletions(-) create mode 100644 testdata/d2oracle/TestMove/nested-underscore-move-out.exp.json diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 52dfd029f..3537677b3 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -1168,7 +1168,7 @@ func move(g *d2graph.Graph, key, newKey string) (*d2graph.Graph, error) { } ida := d2graph.Key(ref.Key) - resolvedObj, resolvedIDA, err := d2graph.ResolveUnderscoreKey(ida, obj) + resolvedObj, resolvedIDA, err := d2graph.ResolveUnderscoreKey(ida, ref.ScopeObj) if err != nil { return nil, err } @@ -2087,11 +2087,11 @@ func getMostNestedRefs(obj *d2graph.Object) []d2graph.Reference { if err != nil { mostKey = &d2ast.KeyPath{} } - _, resolvedScopeKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(scopeKey), obj) + _, resolvedScopeKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(scopeKey), ref.ScopeObj) if err != nil { continue } - _, resolvedMostKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(mostKey), obj) + _, resolvedMostKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(mostKey), ref.ScopeObj) if err != nil { continue } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 26c200c17..489f3384b 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -2025,6 +2025,24 @@ c: { d b } +`, + }, + + { + name: "nested-underscore-move-out", + text: `guitar: { + books: { + _._.pipe + } +} +`, + key: `pipe`, + newKey: `guitar.pipe`, + + exp: `guitar: { + books + pipe +} `, }, { diff --git a/testdata/d2oracle/TestMove/nested-underscore-move-out.exp.json b/testdata/d2oracle/TestMove/nested-underscore-move-out.exp.json new file mode 100644 index 000000000..79df7f9c5 --- /dev/null +++ b/testdata/d2oracle/TestMove/nested-underscore-move-out.exp.json @@ -0,0 +1,255 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-4:0:27", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-3:1:26", + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:8:8-3:0:25", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "value": [ + { + "string": "pipe", + "raw_string": "pipe" + } + ] + } + } + ] + }, + "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": "guitar", + "id_val": "guitar", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "guitar" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "books", + "id_val": "books", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "books" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "pipe", + "id_val": "pipe", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "value": [ + { + "string": "pipe", + "raw_string": "pipe" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "pipe" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}