Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group eval fix #162

Merged
merged 3 commits into from Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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.
Expand Down
16 changes: 12 additions & 4 deletions jp/equation.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions jp/equation_test.go
Expand Up @@ -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())
}
9 changes: 3 additions & 6 deletions jp/script.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions jp/script_test.go
Expand Up @@ -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))
}