Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

function in Ternary operator gets executed #176

@AlexCosteaWK

Description

@AlexCosteaWK

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions