govaluate icon indicating copy to clipboard operation
govaluate copied to clipboard

error when passing []interface{} to function or method

Open hate13 opened this issue 8 years ago • 3 comments

Using function like this:

udfs := map[string]govaluate.ExpressionFunction{
	"f": func(args ...interface{}) (interface{}, error) {
		arg := args[0].([]interface{})
		return arg[0], nil
	},
}

exp, _ := govaluate.NewEvaluableExpressionWithFunctions("f(p)", udfs)

params := map[string]interface{}{}
params["p"] = []interface{}{"ab", "cd", "e"} // []interface{}{[]interface{}{"ab", "cd", "e"}} is ok

res, err := exp.Evaluate(params)

panic: interface conversion: interface {} is string, not []interface {}

hate13 avatar Apr 13 '18 08:04 hate13

This happens because the parser doesn't differentiate between arrays passed as parameters, and an array made with separators (such as (1, 2, 3)). They both end up as []interface{}, and the function call treats them both as "expanded" (i.e., passes to the ExpressionFunction as foo...).

Going to call this a bug, I doubt anyone expects this behavior.

Knetic avatar Jun 17 '18 19:06 Knetic

Hi, is there any progress on this bug?

Jacky-Lau avatar Aug 28 '18 10:08 Jacky-Lau

any update here?

timmyimprint avatar Jun 07 '21 22:06 timmyimprint