This commit is contained in:
Alexander Wang 2023-06-25 18:08:16 -07:00
parent 3d69eefc68
commit d5b58f9371
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
2 changed files with 59 additions and 11 deletions

View file

@ -2225,7 +2225,7 @@ func ReparentIDDelta(g *d2graph.Graph, key, parentKey string) (string, error) {
return id, nil return id, nil
} }
func ReconnectEdgeIDDeltas(g *d2graph.Graph, edgeKey string, srcKey, dstKey *string) (deltas map[string]string, err error) { func ReconnectEdgeIDDeltas(g *d2graph.Graph, boardPath []string, edgeKey string, srcKey, dstKey *string) (deltas map[string]string, err error) {
defer xdefer.Errorf(&err, "failed to get deltas for reconnect edge %#v", edgeKey) defer xdefer.Errorf(&err, "failed to get deltas for reconnect edge %#v", edgeKey)
deltas = make(map[string]string) deltas = make(map[string]string)
// Reconnection: nothing is created or destroyed, the edge just gets a new ID // Reconnection: nothing is created or destroyed, the edge just gets a new ID
@ -2251,10 +2251,21 @@ func ReconnectEdgeIDDeltas(g *d2graph.Graph, edgeKey string, srcKey, dstKey *str
} }
edgeTrimCommon(mk) edgeTrimCommon(mk)
obj := g.Root
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 nil, fmt.Errorf("board %v not found", boardPath)
}
}
obj := boardG.Root
if mk.Key != nil { if mk.Key != nil {
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, errors.New("edge not found") return nil, errors.New("edge not found")
} }
@ -2289,7 +2300,7 @@ func ReconnectEdgeIDDeltas(g *d2graph.Graph, edgeKey string, srcKey, dstKey *str
if err != nil { if err != nil {
return nil, err return nil, err
} }
src, ok = g.Root.HasChild(d2graph.Key(srcmk.Key)) src, ok = boardG.Root.HasChild(d2graph.Key(srcmk.Key))
if !ok { if !ok {
return nil, errors.New("newSrc not found") return nil, errors.New("newSrc not found")
} }
@ -2300,7 +2311,7 @@ func ReconnectEdgeIDDeltas(g *d2graph.Graph, edgeKey string, srcKey, dstKey *str
if err != nil { if err != nil {
return nil, err return nil, err
} }
dst, ok = g.Root.HasChild(d2graph.Key(dstmk.Key)) dst, ok = boardG.Root.HasChild(d2graph.Key(dstmk.Key))
if !ok { if !ok {
return nil, errors.New("newDst not found") return nil, errors.New("newDst not found")
} }
@ -2313,7 +2324,7 @@ func ReconnectEdgeIDDeltas(g *d2graph.Graph, edgeKey string, srcKey, dstKey *str
newIndex := 0 newIndex := 0
// For the edge's own delta, it just needs to know how many edges came before it with the same src and dst // For the edge's own delta, it just needs to know how many edges came before it with the same src and dst
for _, otherEdge := range g.Edges { for _, otherEdge := range boardG.Edges {
if otherEdge.Src == newSrc && otherEdge.Dst == newDst { if otherEdge.Src == newSrc && otherEdge.Dst == newDst {
firstRef := otherEdge.References[0] firstRef := otherEdge.References[0]
if firstRef.MapKey.Range.Start.Line <= line { if firstRef.MapKey.Range.Start.Line <= line {

View file

@ -7021,10 +7021,11 @@ func TestReconnectEdgeIDDeltas(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
text string boardPath []string
edge string text string
newSrc string edge string
newDst string newSrc string
newDst string
exp string exp string
expErr string expErr string
@ -7181,6 +7182,42 @@ b
exp: `{ exp: `{
"(a -> c)[0]": "(a -> b)[0]", "(a -> c)[0]": "(a -> b)[0]",
"(a -> c)[1]": "(a -> c)[0]" "(a -> c)[1]": "(a -> c)[0]"
}`,
},
{
name: "scenarios-outer-scope",
text: `a
scenarios: {
x: {
d -> b
}
}
`,
boardPath: []string{"x"},
edge: `(d -> b)[0]`,
newDst: "a",
exp: `{
"(d -> b)[0]": "(d -> a)[0]"
}`,
},
{
name: "scenarios-second",
text: `g
a -> b
d
scenarios: {
x: {
d -> b
}
}
`,
boardPath: []string{"x"},
edge: `(d -> b)[0]`,
newSrc: "a",
exp: `{
"(d -> b)[0]": "(a -> b)[1]"
}`, }`,
}, },
} }
@ -7205,7 +7242,7 @@ b
newDst = &tc.newDst newDst = &tc.newDst
} }
deltas, err := d2oracle.ReconnectEdgeIDDeltas(g, tc.edge, newSrc, newDst) deltas, err := d2oracle.ReconnectEdgeIDDeltas(g, tc.boardPath, tc.edge, newSrc, newDst)
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)