govaluate icon indicating copy to clipboard operation
govaluate copied to clipboard

Facing Issue with Govaluate Custom functions

Open Yash1256 opened this issue 1 year ago • 1 comments

Hi, I have been using govaluate in my code since a long time.. but I faced this issue very recently.. The issue is described as I wrote my custom function names as len which definition is as below..

"len": func(args ...interface{}) (interface{}, error) {
	fmt.Printf("Received args: %#v\n", args)
	return len(args), nil
}

And what am I doing is passing empty array to this function and writing an expression which says => len(array) != 0 The logs I recieved

Evaluating expression:  len(array) != 0  with params:  map[array:[]]

and it's giving the result as true that length is not equals to 0.. while the function logs are

Received args: []interface{}{}

But the same thing when I tried with the len(args) as a string.. by using strconv.Itoa(len(args)) and did len(array) != '0' it worked.. May someone please help me out here.

Yash1256 avatar Dec 18 '23 15:12 Yash1256

len(args) is the length of argments of function,you pass an argment to funtion, so length of argment is one. the argment's value is []interface{}{}, array is it's real type. if you want to get len(array). you must type assert at first. just like this: arr := args[0].([]interface{}), then len(arr)

zhangjianxiongZJX avatar Jan 23 '24 02:01 zhangjianxiongZJX