From a1754591ad7c7c8af2093e2c1f6273811df24564 Mon Sep 17 00:00:00 2001 From: Peter Ohler Date: Tue, 20 Feb 2024 19:12:26 -0500 Subject: [PATCH] Group eval fix (#162) Fix group eval and printing of groups. --- CHANGELOG.md | 4 ++++ jp/equation.go | 16 ++++++++++++---- jp/equation_test.go | 5 +++++ jp/script.go | 9 +++------ jp/script_test.go | 8 ++++++++ 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d4334d..89f1e66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm The structure and content of this file follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## [1.21.3] - 2024-02-20 +### Fixed +- Evaluation of a not group such as `!(@.x == 2)` is now correct. + ## [1.21.2] - 2024-02-14 ### Fixed - Reworked the jp equation parser to eliminate some parsing issues. diff --git a/jp/equation.go b/jp/equation.go index a8e574f..74e29cf 100644 --- a/jp/equation.go +++ b/jp/equation.go @@ -18,6 +18,14 @@ type Equation struct { right *Equation } +// MustParseEquation parses the string argument and returns an Equation or panics. +func MustParseEquation(str string) (eq *Equation) { + p := &parser{buf: []byte(str)} + eq = precedentCorrect(p.readEq()) + + return reduceGroups(eq, nil) +} + // Script creates and returns a Script that implements the equation. func (e *Equation) Script() *Script { if e.o == nil { @@ -260,10 +268,10 @@ func (e *Equation) Append(buf []byte, parens bool) []byte { buf = append(buf, ',', ' ') buf = e.right.Append(buf, false) buf = append(buf, ')') - // case group.code: - // if e.left != nil { - // buf = e.left.Append(buf, e.left.o != nil && e.left.o.prec >= e.o.prec) - // } + case group.code: + if e.left != nil { + buf = e.left.Append(buf, e.left.o != nil && e.left.o.prec >= e.o.prec) + } default: if e.left != nil { buf = e.left.Append(buf, e.left.o != nil && e.left.o.prec >= e.o.prec) diff --git a/jp/equation_test.go b/jp/equation_test.go index 61cd027..5ac8d18 100644 --- a/jp/equation_test.go +++ b/jp/equation_test.go @@ -91,3 +91,8 @@ func TestEquationScript(t *testing.T) { eq = jp.Not(nil) tt.Equal(t, "(!null)", eq.Script().String()) } + +func TestParseEquation(t *testing.T) { + eq := jp.MustParseEquation("!(@.text ~= /(?i)notexpected/)") + tt.Equal(t, "!(@.text ~= /(?i)notexpected/)", eq.String()) +} diff --git a/jp/script.go b/jp/script.go index c1e8aa8..98ed19a 100644 --- a/jp/script.go +++ b/jp/script.go @@ -111,12 +111,7 @@ func NewScript(str string) (s *Script, err error) { // MustNewScript parses the string argument and returns a script or an error. func MustNewScript(str string) (s *Script) { - p := &parser{buf: []byte(str)} - - eq := precedentCorrect(p.readEq()) - eq = reduceGroups(eq, nil) - - return eq.Script() + return MustParseEquation(str).Script() } // Append a string representation of the fragment to the buffer and then @@ -463,6 +458,8 @@ func evalStack(sstack []any) []any { right = sstack[i+2] } switch o.code { + case group.code: + sstack[i] = left case eq.code: if left == right { sstack[i] = true diff --git a/jp/script_test.go b/jp/script_test.go index 9e15e05..17010a3 100644 --- a/jp/script_test.go +++ b/jp/script_test.go @@ -392,3 +392,11 @@ func BenchmarkOjScriptDev(b *testing.B) { // result := s.Eval([]any{}, []any{5}) // fmt.Printf("*** %s\n", result) // } + +func TestScriptMatchWithNotGroup(t *testing.T) { + data := map[string]any{ + "text": "my Expected text NotExpected", + } + expr := jp.MustNewScript("!(@.text ~= /(?i)notexpected/)") + tt.Equal(t, false, expr.Match(data)) +}