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

function in Ternary operator gets executed #176

Open
AlexCosteaWK opened this issue May 11, 2023 · 0 comments
Open

function in Ternary operator gets executed #176

AlexCosteaWK opened this issue May 11, 2023 · 0 comments

Comments

@AlexCosteaWK
Copy link

The Ternary operator bas a BUG,

in the expression
true ? say('yes) : say('no')
I get BOTH 'yes' and 'no' as an output when I should only get 'yes'

in the expression
false ? say('yes') : say('no')
I get only'no' as expected

Can someone help me with this?

Thank you,
Alex C.

please see attached code for testing

https://go.dev/play/p/NodbvidfIfa


package main

import (
	"fmt"

	"github.com/Knetic/govaluate"
)

func main() {
	funcs := map[string]govaluate.ExpressionFunction{
		"yes": func(args ...interface{}) (interface{}, error) {
			fmt.Println("should execute", args)
			return nil, nil
		},
		"no": func(args ...interface{}) (interface{}, error) {
			fmt.Println("should NOT execute", args)
			return nil, nil
		},
	}

	str := ` [var] == true ? yes('good') : no ('bad')`

	exp, e := govaluate.NewEvaluableExpressionWithFunctions(str, funcs)
	if e != nil {
		fmt.Println(e)
	}

	vars := map[string]interface{}{
		"var": true,
	}

	fmt.Println("----Exec with value is true----")
	exp.Evaluate(vars)

	fmt.Println("----Exec with value is false----")
	vars["var"] = false
	exp.Evaluate(vars)

}
----Exec with value is true----
should execute [good]
should NOT execute [bad]
----Exec with value is false----
should NOT execute [bad]

Program exited.
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