Merge pull request #1447 from alixander/d2oracle-iddeltas

DeleteIDDeltas with boardPath
This commit is contained in:
Alexander Wang 2023-06-26 14:37:57 -07:00 committed by GitHub
commit a5b3ad96da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 16 deletions

View file

@ -2117,7 +2117,7 @@ func updateNear(prevG, g *d2graph.Graph, from, to *string, includeDescendants bo
tmpG, _ := recompile(prevG.AST) tmpG, _ := recompile(prevG.AST)
appendMapKey(tmpG.AST, valueMK) appendMapKey(tmpG.AST, valueMK)
if to == nil { if to == nil {
deltas, err := DeleteIDDeltas(tmpG, *from) deltas, err := DeleteIDDeltas(tmpG, nil, *from)
if err != nil { if err != nil {
return err return err
} }
@ -2162,7 +2162,7 @@ func updateNear(prevG, g *d2graph.Graph, from, to *string, includeDescendants bo
tmpG, _ := recompile(prevG.AST) tmpG, _ := recompile(prevG.AST)
appendMapKey(tmpG.AST, valueMK) appendMapKey(tmpG.AST, valueMK)
if to == nil { if to == nil {
deltas, err := DeleteIDDeltas(tmpG, *from) deltas, err := DeleteIDDeltas(tmpG, nil, *from)
if err != nil { if err != nil {
return err return err
} }
@ -2196,23 +2196,34 @@ func deleteFromMap(m *d2ast.Map, mk *d2ast.Key) bool {
return false return false
} }
func ReparentIDDelta(g *d2graph.Graph, key, parentKey string) (string, error) { func ReparentIDDelta(g *d2graph.Graph, boardPath []string, key, parentKey string) (string, error) {
mk, err := d2parser.ParseMapKey(key) mk, err := d2parser.ParseMapKey(key)
if err != nil { if err != nil {
return "", err return "", err
} }
obj, ok := g.Root.HasChild(d2graph.Key(mk.Key))
boardG := g
if len(boardPath) > 0 {
// When compiling a nested board, we can read from boardG but only write to baseBoardG
boardG = GetBoardGraph(g, boardPath)
if boardG == nil {
return "", fmt.Errorf("board %v not found", boardPath)
}
}
obj, ok := boardG.Root.HasChild(d2graph.Key(mk.Key))
if !ok { if !ok {
return "", errors.New("not found") return "", errors.New("not found")
} }
parent := g.Root parent := boardG.Root
if parentKey != "" { if parentKey != "" {
mk2, err := d2parser.ParseMapKey(parentKey) mk2, err := d2parser.ParseMapKey(parentKey)
if err != nil { if err != nil {
return "", err return "", err
} }
parent, ok = g.Root.HasChild(d2graph.Key(mk2.Key)) parent, ok = boardG.Root.HasChild(d2graph.Key(mk2.Key))
if !ok { if !ok {
return "", errors.New("not found") return "", errors.New("not found")
} }
@ -2693,7 +2704,7 @@ func MoveIDDeltas(g *d2graph.Graph, key, newKey string, includeDescendants bool)
return deltas, nil 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) defer xdefer.Errorf(&err, "failed to get deltas for deletion of %#v", key)
deltas = make(map[string]string) deltas = make(map[string]string)
@ -2703,7 +2714,16 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err
} }
edgeTrimCommon(mk) 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) conflictNewIDs := make(map[*d2graph.Object]string)
conflictOldIDs := make(map[*d2graph.Object]string) conflictOldIDs := make(map[*d2graph.Object]string)
var newIDs []string var newIDs []string
@ -2715,7 +2735,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err
} }
var ok bool var ok bool
obj, ok = g.Root.HasChild(d2graph.Key(mk.Key)) obj, ok = boardG.Root.HasChild(d2graph.Key(mk.Key))
if !ok { if !ok {
return nil, nil return nil, nil
} }
@ -2742,7 +2762,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err
continue continue
} }
hoistedAbsID := ch2.ID hoistedAbsID := ch2.ID
if obj.Parent != g.Root { if obj.Parent != boardG.Root {
hoistedAbsID = obj.Parent.AbsID() + "." + ch2.ID hoistedAbsID = obj.Parent.AbsID() + "." + ch2.ID
} }
siblingsToBeHoisted = append(siblingsToBeHoisted, hoistedAbsID) siblingsToBeHoisted = append(siblingsToBeHoisted, hoistedAbsID)
@ -2757,7 +2777,7 @@ func DeleteIDDeltas(g *d2graph.Graph, key string) (deltas map[string]string, err
continue continue
} }
hoistedAbsID := ch.ID hoistedAbsID := ch.ID
if obj.Parent != g.Root { if obj.Parent != boardG.Root {
hoistedAbsID = obj.Parent.AbsID() + "." + ch.ID hoistedAbsID = obj.Parent.AbsID() + "." + ch.ID
} }
hoistedMK, err := d2parser.ParseMapKey(hoistedAbsID) hoistedMK, err := d2parser.ParseMapKey(hoistedAbsID)
@ -2773,8 +2793,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 { if conflictingObj, ok := boardG.Root.HasChild(d2graph.Key(hoistedMK.Key)); (ok && conflictingObj != obj) || conflictsWithNewID {
newKey, _, err := generateUniqueKey(g, hoistedAbsID, ignored, append(newIDs, siblingsToBeHoisted...)) newKey, _, err := generateUniqueKey(boardG, hoistedAbsID, ignored, append(newIDs, siblingsToBeHoisted...))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -7730,8 +7730,9 @@ func TestDeleteIDDeltas(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
text string boardPath []string
key string text string
key string
exp string exp string
expErr string expErr string
@ -7998,6 +7999,55 @@ Square
exp: `{ exp: `{
"x.Square 2": "Square 4", "x.Square 2": "Square 4",
"x.Square 3": "Square 3" "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) t.Fatal(err)
} }
deltas, err := d2oracle.DeleteIDDeltas(g, tc.key) deltas, err := d2oracle.DeleteIDDeltas(g, tc.boardPath, tc.key)
if tc.expErr != "" { if tc.expErr != "" {
if err == nil { if err == nil {
t.Fatalf("expected error with: %q", tc.expErr) t.Fatalf("expected error with: %q", tc.expErr)