delete iddelta

This commit is contained in:
Alexander Wang 2023-06-26 14:25:29 -07:00
parent aa321c2066
commit e072296717
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
2 changed files with 71 additions and 12 deletions

View file

@ -2117,7 +2117,7 @@ func updateNear(prevG, g *d2graph.Graph, from, to *string, includeDescendants bo
tmpG, _ := recompile(prevG.AST)
appendMapKey(tmpG.AST, valueMK)
if to == nil {
deltas, err := DeleteIDDeltas(tmpG, *from)
deltas, err := DeleteIDDeltas(tmpG, nil, *from)
if err != nil {
return err
}
@ -2162,7 +2162,7 @@ func updateNear(prevG, g *d2graph.Graph, from, to *string, includeDescendants bo
tmpG, _ := recompile(prevG.AST)
appendMapKey(tmpG.AST, valueMK)
if to == nil {
deltas, err := DeleteIDDeltas(tmpG, *from)
deltas, err := DeleteIDDeltas(tmpG, nil, *from)
if err != nil {
return err
}
@ -2693,7 +2693,7 @@ func MoveIDDeltas(g *d2graph.Graph, key, newKey string, includeDescendants bool)
return deltas, nil
}
func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err error) {
func DeleteIDDeltas(g *d2graph.Graph, boardPath []string, key string) (deltas map[string]string, err error) {
defer xdefer.Errorf(&err, "failed to get deltas for deletion of %#v", key)
deltas = make(map[string]string)
@ -2703,7 +2703,16 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err
}
edgeTrimCommon(mk)
obj := g.Root
boardG := g
if len(boardPath) > 0 {
boardG = GetBoardGraph(g, boardPath)
if boardG == nil {
return nil, fmt.Errorf("board %v not found", boardPath)
}
}
obj := boardG.Root
conflictNewIDs := make(map[*d2graph.Object]string)
conflictOldIDs := make(map[*d2graph.Object]string)
var newIDs []string
@ -2715,7 +2724,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err
}
var ok bool
obj, ok = g.Root.HasChild(d2graph.Key(mk.Key))
obj, ok = boardG.Root.HasChild(d2graph.Key(mk.Key))
if !ok {
return nil, nil
}
@ -2742,7 +2751,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err
continue
}
hoistedAbsID := ch2.ID
if obj.Parent != g.Root {
if obj.Parent != boardG.Root {
hoistedAbsID = obj.Parent.AbsID() + "." + ch2.ID
}
siblingsToBeHoisted = append(siblingsToBeHoisted, hoistedAbsID)
@ -2757,7 +2766,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err
continue
}
hoistedAbsID := ch.ID
if obj.Parent != g.Root {
if obj.Parent != boardG.Root {
hoistedAbsID = obj.Parent.AbsID() + "." + ch.ID
}
hoistedMK, err := d2parser.ParseMapKey(hoistedAbsID)
@ -2773,8 +2782,8 @@ 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, ignored, append(newIDs, siblingsToBeHoisted...))
if conflictingObj, ok := boardG.Root.HasChild(d2graph.Key(hoistedMK.Key)); (ok && conflictingObj != obj) || conflictsWithNewID {
newKey, _, err := generateUniqueKey(boardG, hoistedAbsID, ignored, append(newIDs, siblingsToBeHoisted...))
if err != nil {
return nil, err
}

View file

@ -7730,6 +7730,7 @@ func TestDeleteIDDeltas(t *testing.T) {
testCases := []struct {
name string
boardPath []string
text string
key string
@ -7998,6 +7999,55 @@ Square
exp: `{
"x.Square 2": "Square 4",
"x.Square 3": "Square 3"
}`,
},
{
name: "scenarios-basic",
text: `x
scenarios: {
y: {
a
}
}
`,
boardPath: []string{"y"},
key: `a`,
exp: `{}`,
},
{
name: "scenarios-parent",
text: `x
scenarios: {
y: {
a.x
}
}
`,
boardPath: []string{"y"},
key: `a`,
exp: `{
"a.x": "x 2"
}`,
},
{
name: "layers-parent",
text: `x
layers: {
y: {
a.x
}
}
`,
boardPath: []string{"y"},
key: `a`,
exp: `{
"a.x": "x"
}`,
},
}
@ -8013,7 +8063,7 @@ Square
t.Fatal(err)
}
deltas, err := d2oracle.DeleteIDDeltas(g, tc.key)
deltas, err := d2oracle.DeleteIDDeltas(g, tc.boardPath, tc.key)
if tc.expErr != "" {
if err == nil {
t.Fatalf("expected error with: %q", tc.expErr)