Arbitrary expression evaluation for golang
Multiple API breaks occurred between the previous release and this one. They are minor, and most users won't see any impact, but they measurably change the public interface and semantic meaning of some expressions.
Date localization
Previously, literal dates were parsed as UTC dates and always turned into the same value no matter what locale the expression ran. However, this quickly led to confusing situations where users would pass time.Now()
(which is localized) as a parameter, compare it against a literal date (which was not localized) and would see results that seemed wrong. For example, on December 31st, 2015, at 9:00pm in Pacific Time, the expression now > '2016-01-01'
would evaluate to true
.
Per #49 , all literal dates are parsed in the current locale (unless they explicitly include the locale in the date string).
GetTokenKindString
removed
Previously, an exported function called GetTokenKindString
existed. It has been removed, and is exactly replaced by the String()
function, which can be called from any OperatorToken
. This makes it more idiomatic, and also matches the convention used elsewhere in the library. See #37
Privatized constants The library used to expose a lot of constants which weren't useful to users, and were only used internally (mostly for stage planning). Some of them weren't even used at all. These have been privatized or removed. See #38
complex
and struct
types can now be passed as parameters. Before expression functions were available, these types made no sense in an expression. However, they can be perfectly valid now. See #57This entire repo has been re-tagged to match the gopkg.in standard. This means that instead of import "github.com/Knetic/govaluate"
(which always pulls master branch), users can now pin to a specific released version by doing import "gopkg.in/Knetic/govaluate.v3"
- where v3
is the tag of the major release that you'd like.
Evaluate()
(involves bugfixes on this feature from #46 and #47 ))
(found with #47)EvaluableExpresion.ChecksTypes
can be set to false
, which will cause all further Eval
or Evaluate
calls to ignore type checking, and instead panic if types are not valid. This should only be used by those who can guarantee that their types will never be invalid for the operators they're using them with. See #24 for some notes on cost/benefit.nil
instead of returning an error.ToSQLQuery()
has also been greatly modernized to include all operators/modifiers/comparators except for ternaries.
This release is contract-compatible with previous releases.
Previously, the ^
operator stood for "exponent", it now means "bitwise-XOR". The exponent operator is now **
, in line with the Ruby style (since it's the only language which seems to have an operator for that). This will break expressions that worked in 1.x. Before upgrading to the 2.x, check to see if you have any expressions which use the ^
operator, and change them to **
. There is no automatic checking for this, no switching for the semantics. You need to do an audit on your own, or your expressions may not evaluate how you expect.
It is not expected that any similar API breaks will happen in the future, this one was only due to author's oversight early in development. See #20 for some discussion of the change.
EvaluableExpression
is ever re-used, the gains will be apparent.map[string]interface{}
. Anything that exposes a Get(string)(interface{}, error)
method receiver can now be passed into Eval
. ( #14 )*regexp.Regexp
object to be passed in as a parameter, and used as the pattern for a regex. Strings used as patterns are still supported, but will be compiled during each evaluation, and therefore slower. ( #16 )foo > 5
is given a string
for foo
, an error is returned instead. (#11)=~
and !~
) which operate on strings. Implementation largely courtesy vjeantet.Evaluate()
was given a nil parameter map (#10)Implemented parsing for >1byte runes everywhere (means the entire utf8 range is open for use)
Fixed panics when using fixed-point parameters during evaluation
Fixed parsing error when parsing variables that contained underscores, such as when using used snake_case.
Fixed bugs which prevented using comparators after a clause, previously expressions like "(2 + 2) >= 4" would fail to parse
Added some benchmarks (go test -bench=.)
Operator prefixes ("!" for boolean inversion, and "-" for numeric negation) implemented.
Backslashes for escaping special characters implemented.
Escaped parameters implemented (so that parameters with minus signs or spaces in their names can be used).
Also implemented the ability to translate a valid govaluate expression into a SQL "where" clause, as a string. Support for MongoDB queries is experimental.
Contract-compatible with prior releases; expressions and code written for previous versions will operate exactly the same with this release.
Implemented date parsing/evaluation. Also implemented exponential and modulus operators.
Contract-compatible with prior releases.