From b0848b99e828e2e3332f96c17d46ee5c2ad3cec8 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Thu, 25 May 2023 08:54:30 -0700 Subject: [PATCH] fix AND evaluating RHS if LHS is false (#13) --- CHANGELOG.md | 7 ++++++- README.md | 2 +- go.mod | 4 ++-- go.sum | 5 +++-- parser.go | 20 ++++++++++++++++++++ parser_test.go | 9 +++++++-- 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 531c961..e3d40d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -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 diff --git a/README.md b/README.md index 9ddbc0c..123e423 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/go.mod b/go.mod index 9a9dbfb..dc72c3b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index eced720..9eeffc0 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/parser.go b/parser.go index 27233b2..127f959 100644 --- a/parser.go +++ b/parser.go @@ -879,6 +879,7 @@ func (e eq) Calculate(src []byte) (any, error) { if err != nil { return nil, err } + return reflect.DeepEqual(left, right), nil } @@ -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 @@ -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 @@ -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 diff --git a/parser_test.go b/parser_test.go index b0fe897..3969fdd 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,10 +1,9 @@ package ksql import ( + "github.com/stretchr/testify/require" "testing" "time" - - "github.com/stretchr/testify/require" ) func TestParser(t *testing.T) { @@ -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 {