Merge pull request #829 from alixander/delete-underscore

d2oracle: fix delete and move with underscores
This commit is contained in:
Alexander Wang 2023-02-15 14:32:34 -08:00 committed by GitHub
commit 92ee929f21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 773 additions and 7 deletions

View file

@ -773,13 +773,14 @@ func deleteObject(g *d2graph.Graph, key *d2ast.KeyPath, obj *d2graph.Object) (*d
if len(ref.MapKey.Edges) == 0 { if len(ref.MapKey.Edges) == 0 {
isSuffix := ref.KeyPathIndex == len(ref.Key.Path)-1 isSuffix := ref.KeyPathIndex == len(ref.Key.Path)-1
ref.Key.Path = append(ref.Key.Path[:ref.KeyPathIndex], ref.Key.Path[ref.KeyPathIndex+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 { withoutSpecial := go2.Filter(ref.Key.Path, func(x *d2ast.StringBox) bool {
_, ok := d2graph.ReservedKeywords[x.Unbox().ScalarString()] _, isReserved := d2graph.ReservedKeywords[x.Unbox().ScalarString()]
return !ok isSpecial := isReserved || x.Unbox().ScalarString() == "_"
return !isSpecial
}) })
if obj.Attributes.Shape.Value == d2target.ShapeSQLTable || obj.Attributes.Shape.Value == d2target.ShapeClass { if obj.Attributes.Shape.Value == d2target.ShapeSQLTable || obj.Attributes.Shape.Value == d2target.ShapeClass {
ref.MapKey.Value.Map = nil ref.MapKey.Value.Map = nil
} else if len(withoutReserved) == 0 { } else if len(withoutSpecial) == 0 {
hoistRefChildren(g, key, ref) hoistRefChildren(g, key, ref)
deleteFromMap(ref.Scope, ref.MapKey) deleteFromMap(ref.Scope, ref.MapKey)
} else if ref.MapKey.Value.Unbox() == nil && } else if ref.MapKey.Value.Unbox() == nil &&
@ -1167,7 +1168,7 @@ func move(g *d2graph.Graph, key, newKey string) (*d2graph.Graph, error) {
} }
ida := d2graph.Key(ref.Key) ida := d2graph.Key(ref.Key)
resolvedObj, resolvedIDA, err := d2graph.ResolveUnderscoreKey(ida, obj) resolvedObj, resolvedIDA, err := d2graph.ResolveUnderscoreKey(ida, ref.ScopeObj)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2086,11 +2087,11 @@ func getMostNestedRefs(obj *d2graph.Object) []d2graph.Reference {
if err != nil { if err != nil {
mostKey = &d2ast.KeyPath{} mostKey = &d2ast.KeyPath{}
} }
_, resolvedScopeKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(scopeKey), obj) _, resolvedScopeKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(scopeKey), ref.ScopeObj)
if err != nil { if err != nil {
continue continue
} }
_, resolvedMostKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(mostKey), obj) _, resolvedMostKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(mostKey), ref.ScopeObj)
if err != nil { if err != nil {
continue continue
} }

View file

@ -2025,6 +2025,24 @@ c: {
d d
b b
} }
`,
},
{
name: "nested-underscore-move-out",
text: `guitar: {
books: {
_._.pipe
}
}
`,
key: `pipe`,
newKey: `guitar.pipe`,
exp: `guitar: {
books
pipe
}
`, `,
}, },
{ {
@ -3189,6 +3207,41 @@ c -> d
exp: `books: { exp: `books: {
_.pipe _.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
`, `,
}, },
{ {
@ -4743,6 +4796,23 @@ x.y.z.w.e.p.l -> x.y.z.1.2.3.4
"x.x": "x" "x.x": "x"
}`, }`,
}, },
{
name: "only-reserved",
text: `guitar: {
books: {
_._.pipe: {
a
}
}
}
`,
key: `pipe`,
exp: `{
"pipe.a": "a"
}`,
},
{ {
name: "delete_container_with_conflicts", name: "delete_container_with_conflicts",

View file

@ -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": "<nil>"
}

185
testdata/d2oracle/TestDelete/only-underscore.exp.json generated vendored Normal file
View file

@ -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": "<nil>"
}

View file

@ -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": "<nil>"
}