Skip to content

Commit

Permalink
feat: fix implementation of GetNodeFromPath and rewrite api
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
GuptaManan100 committed Feb 11, 2025
1 parent 59c422f commit 179b70f
Show file tree
Hide file tree
Showing 13 changed files with 1,953 additions and 132 deletions.
23 changes: 23 additions & 0 deletions go/tools/asthelpergen/integration/ast_clone.go

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

27 changes: 27 additions & 0 deletions go/tools/asthelpergen/integration/ast_equals.go

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

57 changes: 57 additions & 0 deletions go/tools/asthelpergen/integration/ast_path.go

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

20 changes: 17 additions & 3 deletions go/tools/asthelpergen/integration/ast_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestWalkAllPartsOfAST(t *testing.T) {
Expand All @@ -31,21 +33,33 @@ func TestWalkAllPartsOfAST(t *testing.T) {
}

for i := range 20 {
sliceContainer.ASTImplementationElements = append(sliceContainer.ASTImplementationElements, &Leaf{v: i})
sliceContainer.ASTImplementationElements = append(sliceContainer.ASTImplementationElements, &Leaf{v: 3 + i})
}

ast := &RefContainer{
ASTType: sliceContainer,
NotASTType: 2,
ASTImplementationType: &Leaf{v: 3},
ASTImplementationType: &Leaf{v: 23},
}

var leafPaths []ASTPath
RewriteWithPaths(ast, func(c *Cursor) bool {
node := c.Node()
if !reflect.TypeOf(node).Comparable() {
return true
}
fmt.Println(ASTPath(c.current.ToPath()).DebugString())
if _, isLeaf := node.(*Leaf); isLeaf {
leafPaths = append(leafPaths, c.Path())
}
fmt.Println(c.Path().DebugString())
return true
}, nil)

require.Len(t, leafPaths, 23)
for idx, path := range leafPaths {
fmt.Println("Walking: " + path.DebugString())
node := GetNodeFromPath(ast, path)
require.IsType(t, &Leaf{}, node)
require.EqualValues(t, idx+1, node.(*Leaf).v)
}
}
Loading

0 comments on commit 179b70f

Please sign in to comment.