PaesslerAG / gval

Expression evaluation in golang
BSD 3-Clause "New" or "Revised" License
731 stars 82 forks source link

Unable to accept an array as input to custom function #61

Closed timmyimprint closed 3 years ago

timmyimprint commented 3 years ago
`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?

generikvault commented 3 years ago

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

generikvault commented 3 years ago

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
}