Skip to content

Commit

Permalink
fix AND & OR (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Karn authored May 25, 2023
1 parent 82693d5 commit 72ca000
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 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.9.1] - 2023-05-25
### Fixed
- Fixed AND & OR not early exiting evaluation.

## [0.9.0] - 2023-01-05
### Added
- Added new `_uppercase_` & `_title_` COERCE identifiers.
Expand Down Expand Up @@ -95,7 +99,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial release.

[Unreleased]: https://github.com/rust-playground/ksql/compare/v0.9.0...HEAD
[Unreleased]: https://github.com/rust-playground/ksql/compare/v0.9.1...HEAD
[0.9.1]: https://github.com/rust-playground/ksql/compare/v0.9.0...v0.9.1
[0.9.0]: https://github.com/rust-playground/ksql/compare/v0.8.0...v0.9.0
[0.8.0]: https://github.com/rust-playground/ksql/compare/v0.7.0...v0.8.0
[0.7.0]: https://github.com/rust-playground/ksql/compare/v0.6.2...v0.7.0
Expand Down
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ksql"
description = "A JSON data expression lexer, parser, cli and library"
version = "0.9.0"
version = "0.9.1"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -17,16 +17,16 @@ keywords = [

[dependencies]
anydate = "0.3.0"
anyhow = "1.0.69"
chrono = { version = "0.4.23", features = ["serde"] }
clap = { version = "4.1.4", features = ["derive"] }
anyhow = "1.0.71"
chrono = { version = "0.4.24", features = ["serde"] }
clap = { version = "4.3.0", features = ["derive"] }
gjson = "0.8.1"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.92"
thiserror = "1.0.38"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
thiserror = "1.0.40"

[dev-dependencies]
criterion = { version = "0.4.0", features = ["html_reports"] }
criterion = { version = "0.5.0", features = ["html_reports"] }

[lib]
bench = false
Expand Down
19 changes: 19 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,13 @@ struct Or {
impl Expression for Or {
fn calculate(&self, json: &[u8]) -> Result<Value> {
let left = self.left.calculate(json)?;

if let Value::Bool(is_true) = left {
if is_true {
return Ok(left);
}
}

let right = self.right.calculate(json)?;

match (left, right) {
Expand All @@ -859,6 +866,13 @@ struct And {
impl Expression for And {
fn calculate(&self, json: &[u8]) -> Result<Value> {
let left = self.left.calculate(json)?;

if let Value::Bool(is_true) = left {
if !is_true {
return Ok(left);
}
}

let right = self.right.calculate(json)?;

match (left, right) {
Expand Down Expand Up @@ -1807,6 +1821,11 @@ mod tests {
let result = ex.calculate(src)?;
assert_eq!(Value::Bool(true), result);

let expression = r#".MyValue != NULL && .MyValue > 19"#;
let ex = Parser::parse(expression)?;
let result = ex.calculate(src)?;
assert_eq!(Value::Bool(false), result);

Ok(())
}
}

0 comments on commit 72ca000

Please sign in to comment.