Skip to content

Commit

Permalink
check whether slices are empty before popping
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Feb 12, 2025
1 parent b6aeff3 commit c8c0b74
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 50 deletions.
2 changes: 1 addition & 1 deletion go/tools/asthelpergen/integration/ast_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestWalkAllPartsOfAST(t *testing.T) {
}

var leafPaths []ASTPath
RewriteWithPaths(ast, func(c *Cursor) bool {
Rewrite(ast, func(c *Cursor) bool {
node := c.Node()
if !reflect.TypeOf(node).Comparable() {
return true
Expand Down
2 changes: 1 addition & 1 deletion go/tools/asthelpergen/integration/ast_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions go/tools/asthelpergen/integration/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,9 @@ func (c *Cursor) ReplaceAndRevisit(newNode AST) {

type replacerFunc func(newNode, parent AST)

// Rewrite is the api.
func Rewrite(node AST, pre, post ApplyFunc) AST {
outer := &struct{ AST }{node}

a := &application{
pre: pre,
post: post,
}

a.rewriteAST(outer, node, func(newNode, parent AST) {
outer.AST = newNode
})

return outer.AST
}

func RewriteWithPaths(node AST, pre, post ApplyFunc) AST {
outer := &struct{ AST }{node}

a := &application{
pre: pre,
post: post,
Expand Down
13 changes: 9 additions & 4 deletions go/tools/asthelpergen/rewrite_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,17 @@ func (r *rewriteGen) interfaceMethod(t types.Type, iface *types.Interface, spi g
}

func (r *rewriteGen) visitStructFields(t types.Type, strct *types.Struct, spi generatorSPI, fail bool) (stmts []jen.Code) {
fields := r.rewriteAllStructFields(t, strct, spi, fail)
fields, prevSliceField := r.rewriteAllStructFields(t, strct, spi, fail)
stmts = append(stmts, r.executePre())
stmts = append(stmts, fields...)

if len(fields) > 0 {
stmts = append(stmts, jen.If(jen.Id("a.collectPaths")).Block(jen.Id("a.cur.current.Pop").Params()))
ifCondition := jen.Id("a.collectPaths")
if prevSliceField != "" {
ifCondition = ifCondition.Op("&&").Len(jen.Id("node." + prevSliceField)).Op(">").Lit(0)
}

stmts = append(stmts, jen.If(ifCondition).Block(jen.Id("a.cur.current.Pop").Params()))
}
stmts = append(stmts, executePost(len(fields) > 0))
stmts = append(stmts, returnTrue())
Expand Down Expand Up @@ -284,7 +289,7 @@ func (r *rewriteGen) rewriteFunc(t types.Type, stmts []jen.Code, source string)
r.file.Add(code)
}

func (r *rewriteGen) rewriteAllStructFields(t types.Type, strct *types.Struct, spi generatorSPI, fail bool) []jen.Code {
func (r *rewriteGen) rewriteAllStructFields(t types.Type, strct *types.Struct, spi generatorSPI, fail bool) ([]jen.Code, string) {
/*
if errF := rewriteAST(node, node.ASTType, func(newNode, parent AST) {
err = vterrors.New(vtrpcpb.Code_INTERNAL, "[BUG] tried to replace '%s' on '%s'")
Expand Down Expand Up @@ -326,7 +331,7 @@ func (r *rewriteGen) rewriteAllStructFields(t types.Type, strct *types.Struct, s
fieldNumber++
}
}
return output
return output, prevSliceField
}

func failReplacer(t types.Type, f string) *jen.Statement {
Expand Down
56 changes: 28 additions & 28 deletions go/vt/sqlparser/ast_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3917,6 +3917,10 @@ func TestValid(t *testing.T) {
_ = Walk(func(node SQLNode) (bool, error) {
return true, nil
}, tree)

_ = RewriteWithPath(tree, func(cursor *Cursor) bool {
return true
}, nil)
})
}
}
Expand Down

0 comments on commit c8c0b74

Please sign in to comment.