Passing a context to the custom functions
schneekatze opened this issue · 1 comments
schneekatze commented
Hello guys,
at the moment if we want to define a custom function, we just extend the language
gval.Evaluate(
expression,
data,
gval.Function("foo", func(args ...interface{}) (interface{}, error) {
return "bar", nil
}),
...
)
Yet we can EvaluateWithContext
, say context with value.
ctx := context.WithValue(context.Background(), "input", data)
gval.EvaluateWithContext(
ctx,
expression,
data,
gval.Function("foo", func(args ...interface{}) (interface{}, error) {
return "bar", nil
}),
...
)
Unfortunately, the context is not passed into a function itself. So maybe, we could do something like
ctx := context.WithValue(context.Background(), "input", data)
gval.EvaluateWithContext(
ctx,
expression,
data,
gval.FunctionWithContext("foo", func(ctx context.Context, args ...interface{}) (interface{}, error) {
return count(ctx.Value("input")), nil
}),
...
)
Why would one need it? E.g.:
- To get an entire parameters, not only arguments selected for the function (say, count all elements in
[]interface{}
); - If the string is the argument, check in context, that this type should be
time
, parse as time and do something.
Wdyt? Or maybe there's already the way to pass some extra information into the gval function? I've tried with Init, but I apparently don't understand its concept that much.
Best regards,
sk
schneekatze commented
It's already there