d2oracle: update signature of UpdateImport

This commit is contained in:
Alexander Wang 2025-04-30 11:21:07 -06:00
parent e9040e88e6
commit f648622dcf
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
2 changed files with 14 additions and 97 deletions

View file

@ -3325,42 +3325,24 @@ func filterReservedPath(path []*d2ast.StringBox) (filtered []*d2ast.StringBox) {
return return
} }
func UpdateImport(g *d2graph.Graph, boardPath []string, path string, newPath *string) (_ *d2graph.Graph, err error) { func UpdateImport(dsl, path string, newPath *string) (_ string, err error) {
if newPath == nil { if newPath == nil {
defer xdefer.Errorf(&err, "failed to remove import %#v", path) defer xdefer.Errorf(&err, "failed to remove import %#v", path)
} else { } else {
defer xdefer.Errorf(&err, "failed to update import from %#v to %#v", path, *newPath) defer xdefer.Errorf(&err, "failed to update import from %#v to %#v", path, *newPath)
} }
boardG := g ast, err := d2parser.Parse("", strings.NewReader(dsl), nil)
baseAST := g.AST if err != nil {
return "", err
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)
}
// TODO beter name
baseAST = boardG.BaseAST
if baseAST == nil {
return nil, fmt.Errorf("board %v cannot be modified through this file", boardPath)
}
} }
_updateImport(boardG, baseAST, path, newPath) _updateImport(ast, path, newPath)
if len(boardPath) > 0 { return d2format.Format(ast), nil
replaced := ReplaceBoardNode(g.AST, baseAST, boardPath)
if !replaced {
return nil, fmt.Errorf("board %v AST not found", boardPath)
}
}
return recompile(g)
} }
func _updateImport(g *d2graph.Graph, m *d2ast.Map, oldPath string, newPath *string) { func _updateImport(m *d2ast.Map, oldPath string, newPath *string) {
for i := 0; i < len(m.Nodes); i++ { for i := 0; i < len(m.Nodes); i++ {
node := m.Nodes[i] node := m.Nodes[i]
@ -3417,7 +3399,7 @@ func _updateImport(g *d2graph.Graph, m *d2ast.Map, oldPath string, newPath *stri
} }
if node.MapKey.Value.Map != nil { if node.MapKey.Value.Map != nil {
_updateImport(g, node.MapKey.Value.Map, oldPath, newPath) _updateImport(node.MapKey.Value.Map, oldPath, newPath)
} }
} }
} }

View file

@ -9618,9 +9618,6 @@ func TestUpdateImport(t *testing.T) {
text: `x: @meow text: `x: @meow
y y
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
},
path: "meow", path: "meow",
newPath: nil, newPath: nil,
exp: `x exp: `x
@ -9632,9 +9629,6 @@ y
text: `x text: `x
...@meow ...@meow
y`, y`,
fsTexts: map[string]string{
"meow.d2": "k",
},
path: "meow", path: "meow",
newPath: nil, newPath: nil,
exp: `x exp: `x
@ -9647,10 +9641,6 @@ y
text: `x: @meow text: `x: @meow
y y
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
"woof.d2": "k",
},
path: "meow", path: "meow",
newPath: go2.Pointer("woof"), newPath: go2.Pointer("woof"),
exp: `x: @woof exp: `x: @woof
@ -9662,10 +9652,6 @@ y
text: `x: @foo/meow text: `x: @foo/meow
y y
`, `,
fsTexts: map[string]string{
"foo/meow.d2": "k",
"bar/woof.d2": "k",
},
path: "foo/meow", path: "foo/meow",
newPath: go2.Pointer("bar/woof"), newPath: go2.Pointer("bar/woof"),
exp: `x: @bar/woof exp: `x: @bar/woof
@ -9678,10 +9664,6 @@ y
...@meow ...@meow
y y
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
"woof.d2": "k",
},
path: "meow", path: "meow",
newPath: go2.Pointer("woof"), newPath: go2.Pointer("woof"),
exp: `x exp: `x
@ -9694,9 +9676,6 @@ y
text: `x: @cat text: `x: @cat
y y
`, `,
fsTexts: map[string]string{
"cat.d2": "k",
},
path: "meow", path: "meow",
newPath: go2.Pointer("woof"), newPath: go2.Pointer("woof"),
exp: `x: @cat exp: `x: @cat
@ -9710,10 +9689,6 @@ y
y y
} }
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
"woof.d2": "k",
},
path: "meow", path: "meow",
newPath: go2.Pointer("woof"), newPath: go2.Pointer("woof"),
exp: `container: { exp: `container: {
@ -9729,9 +9704,6 @@ y
y y
} }
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
},
path: "meow", path: "meow",
newPath: nil, newPath: nil,
exp: `container: { exp: `container: {
@ -9746,10 +9718,6 @@ y
y: @meow y: @meow
z z
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
"woof.d2": "k",
},
path: "meow", path: "meow",
newPath: go2.Pointer("woof"), newPath: go2.Pointer("woof"),
exp: `x: @woof exp: `x: @woof
@ -9764,10 +9732,6 @@ y
...@meow ...@meow
z z
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
"woof.d2": "k",
},
path: "meow", path: "meow",
newPath: go2.Pointer("woof"), newPath: go2.Pointer("woof"),
exp: `x: @woof exp: `x: @woof
@ -9786,10 +9750,6 @@ layers: {
} }
} }
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
"woof.d2": "k",
},
path: "meow", path: "meow",
newPath: go2.Pointer("woof"), newPath: go2.Pointer("woof"),
exp: `x exp: `x
@ -9811,10 +9771,6 @@ layers: {
} }
} }
`, `,
fsTexts: map[string]string{
"meow.d2": "k",
"woof.d2": "k",
},
path: "meow", path: "meow",
newPath: go2.Pointer("woof"), newPath: go2.Pointer("woof"),
exp: `x exp: `x
@ -9832,12 +9788,6 @@ layers: {
y: @foo/baz y: @foo/baz
z z
`, `,
fsTexts: map[string]string{
"foo/bar.d2": "k",
"foo/baz.d2": "k",
"woof/bar.d2": "k",
"woof/baz.d2": "k",
},
path: "foo/", path: "foo/",
newPath: go2.Pointer("woof/"), newPath: go2.Pointer("woof/"),
exp: `x: @woof/bar exp: `x: @woof/bar
@ -9851,10 +9801,6 @@ z
y: @foo/baz y: @foo/baz
z z
`, `,
fsTexts: map[string]string{
"foo/bar.d2": "k",
"foo/baz.d2": "k",
},
path: "foo/", path: "foo/",
newPath: nil, newPath: nil,
exp: `x exp: `x
@ -9868,12 +9814,6 @@ z
y: @foo/qux/quux y: @foo/qux/quux
z z
`, `,
fsTexts: map[string]string{
"foo/bar/baz.d2": "k",
"foo/qux/quux.d2": "k",
"woof/bar/baz.d2": "k",
"woof/qux/quux.d2": "k",
},
path: "foo/", path: "foo/",
newPath: go2.Pointer("woof/"), newPath: go2.Pointer("woof/"),
exp: `x: @woof/bar/baz exp: `x: @woof/bar/baz
@ -9888,18 +9828,13 @@ z
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
t.Parallel() t.Parallel()
et := editTest{ got, err := d2oracle.UpdateImport(tc.text, tc.path, tc.newPath)
text: tc.text, if err != nil {
fsTexts: tc.fsTexts, t.Fatal(err)
testFunc: func(g *d2graph.Graph) (*d2graph.Graph, error) { }
return d2oracle.UpdateImport(g, tc.boardPath, tc.path, tc.newPath) if got != tc.exp {
}, t.Fatalf("tc.exp != newText:\n%s", got)
exp: tc.exp,
expErr: tc.expErr,
assertions: tc.assertions,
} }
et.run(t)
}) })
} }
} }