govaluate icon indicating copy to clipboard operation
govaluate copied to clipboard

Unable to parse numeric value '.' to float64 when using a struct parameter.

Open kennethkoontz opened this issue 7 years ago • 15 comments

When passing in an expression that contains a struct to NewEvaluableExpression, I'm getting "Unable to parse numeric value '.' to float64" error.


// element.ShowCondition.Condition = "Survey.Element('q1').Row('r2').Selected()"
expression, err := govaluate.NewEvaluableExpression(element.ShowCondition.Condition)
if err != nil {
        return 0, e.SMSInvalidConditionError
}

Any ideas?

kennethkoontz avatar Jun 30 '17 18:06 kennethkoontz

I have exactly the same problem. Seems like accessors are broken.

E.g. create expression "xyz.Name == 'test'" and it will fail with Unable to parse numeric value '.' to float64"

This will compile, but won't evaluate correctly: "[xyz.Name] == 'test'"


Update: looks like this is another, unrelated issue. Accessors are not implemented in 3.0.0 release (Apr 2017), but are present in master. I switched to master and "xyz.Name == 'test'" indeed evaluates correctly.

romangithub1024 avatar Aug 11 '17 19:08 romangithub1024

The original problem is not supported yet, the initial implementation of accessors simply isn't smart enough to know that you can access the results of a function call. So something like "Survey.Element('q1').Row('r2').Selected()" will, unfortunately, not work (yet).

It's a completely valid use case, and the docs do not make it clear that this wouldn't work, so I'm considering this a bug. Bad news is that i don't have a timeline for when I can get to it, and there's no easy workaround.

However the second case should work, cases like "xyz.Name == 'test'" should be even covered in some of the unit tests, and i can't repro the problem off master.

Knetic avatar Aug 12 '17 01:08 Knetic

Do you have ideas about how you'd like this implemented? I'd really like to see this functionality added to the package, but if I spend time implementing this I'd like to make sure the approach is something you'd approve.

sambonfire avatar Apr 06 '18 01:04 sambonfire

This still isn't working in V3.0.0. How should I incorporate the package if using dep? Manually copy to vendors is the only thing I can think of.

NathanZarro avatar May 08 '18 23:05 NathanZarro

@NathanZarro using tagged version with dep isn't mandatory. You can always use specific revision or branch (master) in constraints section of Gopkg.toml. Isn't best solution but still better than manual copying while awaiting for new release of 3.x.

adamwasila avatar May 18 '18 07:05 adamwasila

if u use dep, change revision to 9aa49832a739dcd78a5542ff189fb82c3e423116 can resolve this problem,but i`m not sure is there any other problems

javasgl avatar Nov 22 '18 02:11 javasgl

I also encountered the same problem. Is there has any new progress?

wuyingsong avatar Sep 03 '19 09:09 wuyingsong

I have encountered the same problem, bare minimum reproduction example is:

func Test(t *testing.T) {
	_, err := govaluate.NewEvaluableExpression("width - extraInfo.spacing")

	assert.NilError(t, err)
}

Adding govaluate to project was evaluated as dependency in go.mod as:

	github.com/Knetic/govaluate v3.0.0+incompatible

And, in go.sum is following:

github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=

kravemir avatar Feb 20 '21 15:02 kravemir

Looking, that latest commit was made on Oct 22, 2017, and release on May 1, 2017. This library seems to be unsupported.

I've found alternative https://github.com/antonmedv/expr, which seems to be the best performing https://github.com/antonmedv/golang-expression-evaluation-comparison.

kravemir avatar Feb 20 '21 16:02 kravemir

https://github.com/antonmedv/expr

Thanks for sharing~

litao09h avatar Nov 03 '21 06:11 litao09h

Encountered the same problem, 2021-11-03

litao09h avatar Nov 03 '21 07:11 litao09h

Is there any workaround for this problem?

veith avatar Mar 29 '22 09:03 veith

Not so beautiful, but this seems to work:

import "github.com/iv-p/mapaccess"

var functions = map[string]govaluate.ExpressionFunction{
	"GetStringFromMap": func(args ...interface{}) (interface{}, error) {
		return mapaccess.Get(args[0], args[1].(string))
	},
}

In your evaluation you can use "GetStringFromMap(RootItem, 'foo.bar')"

veith avatar Mar 29 '22 09:03 veith

Hi guys, Any news on this? I recently install this package and having asme issue. Thanks, Jovan

jtomasevic avatar Jan 01 '23 02:01 jtomasevic

Yeah, would be cool to get the accessors working! Same issue here.

windowshopr avatar Oct 01 '23 23:10 windowshopr