Skip to content

Commit

Permalink
Support Valuer Patching for MemberNode (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckganesan authored Jan 16, 2024
1 parent e213a66 commit 6691bc8
Showing 1 changed file with 14 additions and 21 deletions.
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

0 comments on commit 6691bc8

Please sign in to comment.