ncalc / ncalc

NCalc is a fast and lightweight expression evaluator library for .NET, designed for flexibility and high performance. It supports a wide range of mathematical and logical operations.
https://ncalc.github.io/ncalc/
MIT License
695 stars 86 forks source link

5.2.0 No Parameters in Expression #305

Closed JEVietti closed 4 months ago

JEVietti commented 4 months ago

Describe the bug I've been using this with version 5.1.0 of NCalcSync, but with 5.2.0 new behavior occurs which I think is an error in which ParameterNames are missing from the GetParameterNames and the Parameters Dictionary in the NCalc.Expression itself is empty

Expression: if([Value] >= 50, 'background-color: #80ffcc;', null)

I first ran into the issue with an Index out of bounds exception upon upgrading and testing.

 System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)

Due to where I am assigning a parameter ["Value"] and inspected the expression and looked at the Parameters in the built Expression and it was empty. The GetParametersNames changed to GetParameterNames but now it is returning a List of just "null" without "Value" I tested by modifying the expression by changing [Value] to: Value, and {Value}, all of these yielded the same result of not appearing in the Parameters Property and would yield an IndexOutOfBoundException.

Example Expression Add the expression with your problem if applicable if([Value] >= 50, 'background-color: #80ffcc;', null)

Before in 5.1.0 I'd get styleExpression.GetParametersNames() Count = 2

[1]: "null"

Now I get Count = 1

Expression styleExpression = new Expression(rcp.StyleExpression, ExpressionOptions.AllowNullParameter);
return styleExpression .GetParametersNames().Where(x => x != "null").ToList() 
// Returns an empty list
// Any attempt to assign to the Parameters["Value"] = 1; now also throws an Exception of Index Out Of Bounds and leaving it out yields an error of missing Parameter with name "Value"

Funnily enough though if I don't attempt to assign a Test Value to Parameters I get this error:

 NCalc.Exceptions.NCalcParameterNotDefinedException: Parameter Value not defined.
   at NCalc.Visitors.EvaluationVisitor.Visit(Identifier identifier)
   at NCalc.Domain.Identifier.Accept[T](ILogicalExpressionVisitor`1 visitor)
   at NCalc.Visitors.EvaluationVisitor.Evaluate(LogicalExpression expression)
   at NCalc.Visitors.EvaluationVisitor.<>c__DisplayClass3_0.<Visit>b__0()

None of this behavior occurred in 5.1.0

gumbarros commented 4 months ago

@JEVietti PR #306 will fix the issue. #304 also happens at v5.2?

JEVietti commented 4 months ago

@gumbarros Hard for me to test this easily since, my project is setup in a way that relies on what your PR will fix. I'd assume though from the other issue test your posted, that #304 is resolved in v5.2; and if not I can continue to use the work around expression instead.