fix map carry

This commit is contained in:
Alexander Wang 2023-05-09 17:17:18 -07:00
parent 0cd2f27d4f
commit 5520c0bca6
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 356 additions and 1 deletions

View file

@ -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

View file

@ -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
}
}
`,
},
}

View file

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