Skip to content

How can I get access to the function type in Patcher in 1.15.8 as CallNode.Func has been removed? #514

Answered by antonmedv
tw1nk asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, this is still possible to do. Your current implementation has a few errors and I removed Func specifically to emphasize a correct solution: use call.Callee.Type() to get a func type.

switch (*node).(type) {
	case *ast.CallNode:
		call := (*node).(*ast.CallNode)
		fn := call.Callee.Type()
		if fn.Kind() != reflect.Func {
			return
		}
		if fn.NumIn() == 0 {
			return
		}
		if fn.In(0).String() != "context.Context" {
			return
		}
		ast.Patch(node, &ast.CallNode{
			Callee: call.Callee,
			Arguments: append([]ast.Node{
				&ast.IdentifierNode{Value: "ctx"},
			}, call.Arguments...),
		})
	}

Or you can also use a patcher that comes with Expr itself https://pkg.go.dev/github.com/expr-lan…

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@antonmedv
Comment options

@antonmedv
Comment options

@tw1nk
Comment options

@antonmedv
Comment options

@tw1nk
Comment options

Answer selected by tw1nk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants