fix set with explicit label

This commit is contained in:
Alexander Wang 2024-11-13 20:05:41 -07:00
parent 7b9f2d151c
commit 51950f041a
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 228 additions and 1 deletions

View file

@ -491,7 +491,12 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
m = obj.Map
}
if (obj.Label.MapKey != nil && writeableLabelMK) && m == nil && (!found || reserved || len(mk.Edges) > 0) {
if (obj.Label.MapKey != nil && writeableLabelMK) && m == nil && (!found || reserved || len(mk.Edges) > 0) &&
// Label is not set like `hey.label: mylabel`
// This should only work when label is set like `hey: mylabel`
(obj.Label.MapKey.Key == nil ||
len(obj.Label.MapKey.Key.Path) == 0 ||
obj.Label.MapKey.Key.Path[len(obj.Label.MapKey.Key.Path)-1].Unbox().ScalarString() != "label") {
m2 := &d2ast.Map{
Range: d2ast.MakeRange(",1:0:0-1:0:0"),
}

View file

@ -1023,6 +1023,16 @@ square.style.opacity: 0.2
key: `square.top`,
value: go2.Pointer(`200`),
exp: `square: {top: 200}
`,
},
{
name: "labeled_set_position",
text: `hey.label: what
`,
key: `hey.top`,
value: go2.Pointer(`200`),
exp: `hey.label: what
hey.top: 200
`,
},
{

212
testdata/d2oracle/TestSet/labeled_set_position.exp.json generated vendored Normal file
View file

@ -0,0 +1,212 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:0:0-2:0:29",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:0:0-0:15:15",
"key": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:0:0-0:9:9",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:0:0-0:3:3",
"value": [
{
"string": "hey",
"raw_string": "hey"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:4:4-0:9:9",
"value": [
{
"string": "label",
"raw_string": "label"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:11:11-0:15:15",
"value": [
{
"string": "what",
"raw_string": "what"
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,1:0:16-1:12:28",
"key": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,1:0:16-1:7:23",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,1:0:16-1:3:19",
"value": [
{
"string": "hey",
"raw_string": "hey"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,1:4:20-1:7:23",
"value": [
{
"string": "top",
"raw_string": "top"
}
]
}
}
]
},
"primary": {},
"value": {
"number": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,1:9:25-1:12:28",
"raw": "200",
"value": "200"
}
}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"attributes": {
"label": {
"value": ""
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
"edges": null,
"objects": [
{
"id": "hey",
"id_val": "hey",
"references": [
{
"key": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:0:0-0:9:9",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:0:0-0:3:3",
"value": [
{
"string": "hey",
"raw_string": "hey"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,0:4:4-0:9:9",
"value": [
{
"string": "label",
"raw_string": "label"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
},
{
"key": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,1:0:16-1:7:23",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,1:0:16-1:3:19",
"value": [
{
"string": "hey",
"raw_string": "hey"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestSet/labeled_set_position.d2,1:4:20-1:7:23",
"value": [
{
"string": "top",
"raw_string": "top"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "what"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"top": {
"value": "200"
},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": "<nil>"
}