Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly Typecasting Numbers into float64 for ExpressionFunctions #161

Open
DanSeb1295 opened this issue Jun 8, 2022 · 0 comments
Open

Comments

@DanSeb1295
Copy link

DanSeb1295 commented Jun 8, 2022

functions := map[string]govaluate.ExpressionFunction {
		"strlen": func(args ...interface{}) (interface{}, error) {
			length := len(args[0].(string))
			return (float64)(length), nil
		},
	}

	expString := "strlen('someReallyLongInputString') <= 16"
	expression, _ := govaluate.NewEvaluableExpressionWithFunctions(expString, functions)

	result, _ := expression.Evaluate(nil)
	// result is now "false", the boolean value

image

why does the non-functional expression version handle the type-casting into a number type, while the functional version needs us to explicitly typecast our returned value into a float64? it fails and gives me a comparator/modifier error when i remove the explicit float64() typecasting.

callbackA := func(args ...interface{}) (interface{}, error) {
	return 123, nil
}

callbackB := func(args ...interface{}) (interface{}, error) {
	return 123, nil
}

callbacks := map[string]govaluate.ExpressionFunction{
	"callbackA": callbackA,
	"callbackB": callbackB,
}

expression, _ := govaluate.NewEvaluableExpressionWithFunctions("callbackA() >= callbackB()", callbacks)
res, err := expression.Evaluate(nil)
fmt.Println(res, err)

Returns:
Value '123' cannot be used with the comparator '>=', it is not a number

@DanSeb1295 DanSeb1295 changed the title Functional Explicitly Typecasting Numbers into float64 for ExpressionFunctions Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant