Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Valuer Patching for MemberNode #525

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions patcher/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ import (
// contain metadata such as column type and if a value is specifically a NULL value.
//
// Use it directly as an Option to expr.Compile()
var ValueGetter = func() expr.Option {
vPatcher := patcher{}
return func(c *conf.Config) {
c.Visitors = append(c.Visitors, vPatcher)
getValueFunc(c)
}
}()
var ValueGetter = expr.Option(func(c *conf.Config) {
c.Visitors = append(c.Visitors, patcher{})
getValueFunc(c)
})

// A AnyValuer provides a generic function for a custom type to return standard go values.
// It allows for returning a `nil` value but does not provide any type checking at expression compile.
Expand Down Expand Up @@ -136,21 +133,17 @@ var supportedInterfaces = []reflect.Type{
type patcher struct{}

func (patcher) Visit(node *ast.Node) {
id, ok := (*node).(*ast.IdentifierNode)
if !ok {
return
}

nodeType := id.Type()

for _, t := range supportedInterfaces {
if nodeType.Implements(t) {
callnode := &ast.CallNode{
Callee: &ast.IdentifierNode{Value: "$patcher_value_getter"},
Arguments: []ast.Node{id},
switch id := (*node).(type) {
case *ast.IdentifierNode, *ast.MemberNode:
nodeType := id.Type()
for _, t := range supportedInterfaces {
if nodeType.Implements(t) {
ast.Patch(node, &ast.CallNode{
Callee: &ast.IdentifierNode{Value: "$patcher_value_getter"},
Arguments: []ast.Node{id},
})
return
}

ast.Patch(node, callnode)
}
}
}
Expand Down
Loading