PaesslerAG/gval

Unable to accept an array as input to custom function

timmyimprint opened this issue · 2 comments

`varsForPassing := map[string]interface{}{
	"AGG311": 1,
	"AGG312": 2,
	"AGG313": 3,
}

function := gval.Function("max", func(args ...interface{}) (interface{}, error) {
	arr := args[0].([]float64)
	largest := arr[0]
	for _, val := range arr {
		if val > largest {
			largest = val
		}
	}
	return largest, nil
})
value, err := gval.Evaluate("max([AGG311, AGG312, AGG313]) > 1000", varsForPassing, function)
fmt.Println(value, err)`

I get the error "panic: interface conversion: interface {} is []interface {}, not []float64"

Is it not possible to define an array with variables and use a function on it?

max is variadic max(AGG311, AGG312, AGG313) > 1000 works

edit:
sry. I missed an issue in your function. arg is not the array of floats it's an []interface{} where ich value can be casted to float64

this is an example from our test cases

Function("sum", func(arguments ...interface{}) (interface{}, error) {
	sum := 0.0
	for _, v := range arguments {
		sum += v.(float64)
	}
	return sum, nil
}