Skip to content

Commit

Permalink
fix AND evaluating RHS if LHS is false (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Karn authored May 25, 2023
1 parent 1c227fb commit b0848b9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.6.1] - 2023-05-25
### Fixed
- Fixed AND and OR not early exiting evaluation.

## [0.6.0] - 2023-01-05
### Added
- Added new `_uppercase_` & `_title_` COERCE identifiers.
Expand Down Expand Up @@ -58,7 +62,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial conversion from https://github.com/rust-playground/ksql.

[Unreleased]: https://github.com/go-playground/ksql/compare/v0.6.0...HEAD
[Unreleased]: https://github.com/go-playground/ksql/compare/v0.6.1...HEAD
[0.6.1]: https://github.com/go-playground/ksql/compare/v0.6.0...v0.6.1
[0.6.0]: https://github.com/go-playground/ksql/compare/v0.5.1...v0.6.0
[0.5.1]: https://github.com/go-playground/ksql/compare/v0.5.0...v0.5.1
[0.5.0]: https://github.com/go-playground/ksql/compare/v0.4.0...v0.5.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ksql
=====
![Project status](https://img.shields.io/badge/version-0.5.1-green.svg)
![Project status](https://img.shields.io/badge/version-0.6.1-green.svg)
[![GoDoc](https://godoc.org/github.com/go-playground/ksql?status.svg)](https://pkg.go.dev/github.com/go-playground/ksql)
![License](https://img.shields.io/dub/l/vibe-d.svg)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.18

require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/go-playground/pkg/v5 v5.11.0
github.com/go-playground/itertools v0.1.0
github.com/go-playground/pkg/v5 v5.18.0
github.com/stretchr/testify v1.8.1
github.com/tidwall/gjson v1.14.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-playground/itertools v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoU
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/itertools v0.1.0 h1:isiUTLIViAz4J3qWrowvRoOYWSXy2ubkXKNJfujE3Sc=
github.com/go-playground/itertools v0.1.0/go.mod h1:+TD1WVpn32jr+GpvO+nnb2xXD45SSzt18Fo/C0y9PJE=
github.com/go-playground/pkg/v5 v5.11.0 h1:yYYmh0RLKBxiLbEwO7HZtp8XVDDuyIqHmhtrmuO0nsg=
github.com/go-playground/pkg/v5 v5.11.0/go.mod h1:eT8XZeFHnqZkfkpkbI8ayjfCw9GohV2/j8STbVmoR6s=
github.com/go-playground/pkg/v5 v5.18.0 h1:qnYuhWhwLdWj6ut9jagFjaiUI6PZTo3SafR5Um9w214=
github.com/go-playground/pkg/v5 v5.18.0/go.mod h1:eT8XZeFHnqZkfkpkbI8ayjfCw9GohV2/j8STbVmoR6s=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
20 changes: 20 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ func (e eq) Calculate(src []byte) (any, error) {
if err != nil {
return nil, err
}

return reflect.DeepEqual(left, right), nil
}

Expand Down Expand Up @@ -1027,6 +1028,7 @@ func (n not) Calculate(src []byte) (any, error) {
if err != nil {
return nil, err
}

switch t := value.(type) {
case bool:
return !t, nil
Expand All @@ -1047,6 +1049,14 @@ func (o or) Calculate(src []byte) (any, error) {
if err != nil {
return nil, err
}

switch t := left.(type) {
case bool:
if t {
return true, nil
}
}

right, err := o.right.Calculate(src)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1076,6 +1086,16 @@ func (a and) Calculate(src []byte) (any, error) {
if err != nil {
return nil, err
}

switch t := left.(type) {
case bool:
if !t {
return false, nil
}
default:
return false, nil
}

right, err := a.right.Calculate(src)
if err != nil {
return nil, err
Expand Down
9 changes: 7 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ksql

import (
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestParser(t *testing.T) {
Expand Down Expand Up @@ -620,6 +619,12 @@ func TestParser(t *testing.T) {
src: `{"name":"mr."}`,
expected: "Mr.",
},
{
name: "NOT NULL AND",
exp: `.MyValue != NULL && .MyValue > 19`,
src: `{}`,
expected: false,
},
}

for _, tc := range tests {
Expand Down

0 comments on commit b0848b9

Please sign in to comment.