Merge pull request #1236 from alixander/gen-unique-num
fix generate unique number id
This commit is contained in:
commit
f29699a445
3 changed files with 213 additions and 5 deletions
|
|
@ -1791,11 +1791,13 @@ func generateUniqueKey(g *d2graph.Graph, prefix string, ignored *d2graph.Object,
|
||||||
} else if obj, ok := g.Root.HasChild(d2graph.Key(mk.Key)); ok && obj != ignored {
|
} else if obj, ok := g.Root.HasChild(d2graph.Key(mk.Key)); ok && obj != ignored {
|
||||||
// The key may already have an index, e.g. "x 2"
|
// The key may already have an index, e.g. "x 2"
|
||||||
spaced := strings.Split(prefix, " ")
|
spaced := strings.Split(prefix, " ")
|
||||||
if _, err := strconv.Atoi(spaced[len(spaced)-1]); err == nil {
|
if len(spaced) > 1 {
|
||||||
withoutIndex := strings.Join(spaced[:len(spaced)-1], " ")
|
if _, err := strconv.Atoi(spaced[len(spaced)-1]); err == nil {
|
||||||
mk, err = d2parser.ParseMapKey(withoutIndex)
|
withoutIndex := strings.Join(spaced[:len(spaced)-1], " ")
|
||||||
if err != nil {
|
mk, err = d2parser.ParseMapKey(withoutIndex)
|
||||||
return "", false, err
|
if err != nil {
|
||||||
|
return "", false, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1595,6 +1595,19 @@ y
|
||||||
|
|
||||||
exp: `"a.b"
|
exp: `"a.b"
|
||||||
"a.b 2"
|
"a.b 2"
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "conflict_with_numbers",
|
||||||
|
|
||||||
|
text: `1
|
||||||
|
Square
|
||||||
|
`,
|
||||||
|
key: `Square`,
|
||||||
|
newName: `1`,
|
||||||
|
|
||||||
|
exp: `1
|
||||||
|
1 2
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -5935,6 +5948,19 @@ y
|
||||||
|
|
||||||
exp: `{
|
exp: `{
|
||||||
"y": "\"a.b 2\""
|
"y": "\"a.b 2\""
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rename_conflict_with_numbers",
|
||||||
|
|
||||||
|
text: `1
|
||||||
|
Square
|
||||||
|
`,
|
||||||
|
key: `Square`,
|
||||||
|
newName: `1`,
|
||||||
|
|
||||||
|
exp: `{
|
||||||
|
"Square": "1 2"
|
||||||
}`,
|
}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
180
testdata/d2oracle/TestRename/conflict_with_numbers.exp.json
generated
vendored
Normal file
180
testdata/d2oracle/TestRename/conflict_with_numbers.exp.json
generated
vendored
Normal file
|
|
@ -0,0 +1,180 @@
|
||||||
|
{
|
||||||
|
"graph": {
|
||||||
|
"name": "",
|
||||||
|
"isFolderOnly": false,
|
||||||
|
"ast": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,0:0:0-2:0:6",
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"map_key": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,0:0:0-0:1:1",
|
||||||
|
"key": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,0:0:0-0:1:1",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,0:0:0-0:1:1",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "1",
|
||||||
|
"raw_string": "1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"primary": {},
|
||||||
|
"value": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"map_key": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,1:0:2-1:3:5",
|
||||||
|
"key": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,1:0:2-1:3:5",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,1:0:2-1:3:5",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "1 2",
|
||||||
|
"raw_string": "1 2"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"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": "1",
|
||||||
|
"id_val": "1",
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"key": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,0:0:0-0:1:1",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,0:0:0-0:1:1",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "1",
|
||||||
|
"raw_string": "1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"key_path_index": 0,
|
||||||
|
"map_key_edge_index": -1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"attributes": {
|
||||||
|
"label": {
|
||||||
|
"value": "1"
|
||||||
|
},
|
||||||
|
"labelDimensions": {
|
||||||
|
"width": 0,
|
||||||
|
"height": 0
|
||||||
|
},
|
||||||
|
"style": {},
|
||||||
|
"near_key": null,
|
||||||
|
"shape": {
|
||||||
|
"value": "rectangle"
|
||||||
|
},
|
||||||
|
"direction": {
|
||||||
|
"value": ""
|
||||||
|
},
|
||||||
|
"constraint": {
|
||||||
|
"value": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"zIndex": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "1 2",
|
||||||
|
"id_val": "1 2",
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"key": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,1:0:2-1:3:5",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2oracle/TestRename/conflict_with_numbers.d2,1:0:2-1:3:5",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "1 2",
|
||||||
|
"raw_string": "1 2"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"key_path_index": 0,
|
||||||
|
"map_key_edge_index": -1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"attributes": {
|
||||||
|
"label": {
|
||||||
|
"value": "1 2"
|
||||||
|
},
|
||||||
|
"labelDimensions": {
|
||||||
|
"width": 0,
|
||||||
|
"height": 0
|
||||||
|
},
|
||||||
|
"style": {},
|
||||||
|
"near_key": null,
|
||||||
|
"shape": {
|
||||||
|
"value": "rectangle"
|
||||||
|
},
|
||||||
|
"direction": {
|
||||||
|
"value": ""
|
||||||
|
},
|
||||||
|
"constraint": {
|
||||||
|
"value": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"zIndex": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"err": "<nil>"
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue