From 4b63f38404d2a5aa06f022678eb2a93c5049a590 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 20 Mar 2023 14:24:16 -0700 Subject: [PATCH] fix id delta --- d2oracle/edit.go | 21 ++++++++++++++++++++- d2oracle/edit_test.go | 6 +++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/d2oracle/edit.go b/d2oracle/edit.go index dd878fef1..1132ec2ac 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -2032,6 +2032,25 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err return nil, nil } for _, ch := range obj.ChildrenArray { + // Record siblings as the unique key generated should not conflict with any siblings either + var siblingsToBeHoisted []string + for _, ch2 := range obj.ChildrenArray { + if ch2 != ch { + chMK, err := d2parser.ParseMapKey(ch2.AbsID()) + if err != nil { + return nil, err + } + ida := d2graph.Key(chMK.Key) + if ida[len(ida)-1] == ida[len(ida)-2] { + continue + } + hoistedAbsID := ch2.ID + if obj.Parent != g.Root { + hoistedAbsID = obj.Parent.AbsID() + "." + ch2.ID + } + siblingsToBeHoisted = append(siblingsToBeHoisted, hoistedAbsID) + } + } chMK, err := d2parser.ParseMapKey(ch.AbsID()) if err != nil { return nil, err @@ -2058,7 +2077,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err } if conflictingObj, ok := g.Root.HasChild(d2graph.Key(hoistedMK.Key)); (ok && conflictingObj != obj) || conflictsWithNewID { - newKey, _, err := generateUniqueKey(g, hoistedAbsID, obj, newIDs) + newKey, _, err := generateUniqueKey(g, hoistedAbsID, obj, append(newIDs, siblingsToBeHoisted...)) if err != nil { return nil, err } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 020b93484..a04413679 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -5616,9 +5616,9 @@ Square: { key: "Square", exp: `{ - "Square.Text": "Text 2", - "Square.Text 4": "Text", - "Square.Text 4.Text 2": "Text.Text 2" + "Square.Text": "Text", + "Square.Text 4": "Text 2", + "Square.Text 4.Text 2": "Text 2.Text 2" }`, }, {