Skip to content

Allow string coerce or array and object #19

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --all-features --all-targets

Expand All @@ -26,14 +26,14 @@ jobs:
platform: [ ubuntu-latest, macos-latest, windows-latest ]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo check --all-features --all-targets

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@clippy
- run: cargo clippy --all-features --tests -- -Dclippy::all -Dclippy::pedantic
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ keywords = [
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anydate = "0.3.0"
anyhow = "1.0.75"
chrono = { version = "0.4.31", features = ["serde"] }
clap = { version = "4.4.7", features = ["derive"] }
anydate = "0.4.0"
anyhow = "1.0.83"
chrono = { version = "0.4.38", features = ["serde"] }
clap = { version = "4.5.4", features = ["derive"] }
gjson = "0.8.1"
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
thiserror = "1.0.50"
serde = { version = "1.0.201", features = ["derive"] }
serde_json = "1.0.117"
thiserror = "1.0.60"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down
12 changes: 12 additions & 0 deletions src/parser/coercions/constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::parser::{Expression, Value};

#[derive(Debug)]
pub(in crate::parser) struct CoercedConst {
pub value: Value,
}

impl Expression for CoercedConst {
fn calculate(&self, _json: &[u8]) -> crate::parser::parse::Result<Value> {
Ok(self.value.clone())
}
}
24 changes: 24 additions & 0 deletions src/parser/coercions/date_time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Value};

#[derive(Debug)]
pub(in crate::parser) struct COERCEDateTime {
pub value: BoxedExpression,
}

impl Expression for COERCEDateTime {
fn calculate(&self, json: &[u8]) -> crate::parser::parse::Result<Value> {
let value = self.value.calculate(json)?;

match value {
Value::String(ref s) => match anydate::parse_utc(s) {
Err(_) => Ok(Value::Null),
Ok(dt) => Ok(Value::DateTime(dt)),
},
Value::Null => Ok(value),
value => Err(Error::UnsupportedCOERCE(
format!("{value} COERCE datetime",),
)),
}
}
}
17 changes: 17 additions & 0 deletions src/parser/coercions/lowercase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Value};

#[derive(Debug)]
pub(in crate::parser) struct CoerceLowercase {
pub value: BoxedExpression,
}

impl Expression for CoerceLowercase {
fn calculate(&self, json: &[u8]) -> crate::parser::parse::Result<Value> {
let v = self.value.calculate(json)?;
match v {
Value::String(s) => Ok(Value::String(s.to_lowercase())),
v => Err(Error::UnsupportedCOERCE(format!("{v} COERCE lowercase",))),
}
}
}
17 changes: 17 additions & 0 deletions src/parser/coercions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod constant;
mod date_time;
mod lowercase;
mod number;
mod string;
mod sub_str;
mod title;
mod uppercase;

pub(super) use constant::CoercedConst;
pub(super) use date_time::COERCEDateTime;
pub(super) use lowercase::CoerceLowercase;
pub(super) use number::COERCENumber;
pub(super) use string::COERCEString;
pub(super) use sub_str::CoerceSubstr;
pub(super) use title::CoerceTitle;
pub(super) use uppercase::CoerceUppercase;
28 changes: 28 additions & 0 deletions src/parser/coercions/number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Value};

#[derive(Debug)]
pub(in crate::parser) struct COERCENumber {
pub value: BoxedExpression,
}

impl Expression for COERCENumber {
#[allow(clippy::cast_precision_loss)]
fn calculate(&self, json: &[u8]) -> crate::parser::parse::Result<Value> {
let value = self.value.calculate(json)?;
match value {
Value::String(s) => Ok(Value::Number(
s.parse::<f64>()
.map_err(|e| Error::UnsupportedCOERCE(e.to_string()))?,
)),
Value::Number(num) => Ok(Value::Number(num)),
Value::Bool(b) => Ok(Value::Number(if b { 1.0 } else { 0.0 })),
Value::DateTime(dt) => Ok(Value::Number(
dt.timestamp_nanos_opt().unwrap_or_default() as f64
)),
_ => Err(Error::UnsupportedCOERCE(
format!("{value} COERCE datetime",),
)),
}
}
}
24 changes: 24 additions & 0 deletions src/parser/coercions/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Expression, Value};
use chrono::SecondsFormat;

#[derive(Debug)]
pub(in crate::parser) struct COERCEString {
pub value: BoxedExpression,
}

impl Expression for COERCEString {
fn calculate(&self, json: &[u8]) -> crate::parser::parse::Result<Value> {
let value = self.value.calculate(json)?;
match value {
Value::Null => Ok(Value::String("null".to_string())),
Value::String(s) => Ok(Value::String(s)),
Value::Number(num) => Ok(Value::String(num.to_string())),
Value::Bool(b) => Ok(Value::String(b.to_string())),
Value::DateTime(dt) => Ok(Value::String(
dt.to_rfc3339_opts(SecondsFormat::AutoSi, true),
)),
Value::Array(_) | Value::Object(_) => Ok(Value::String(value.to_string())),
}
}
}
33 changes: 33 additions & 0 deletions src/parser/coercions/sub_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Value};

#[derive(Debug)]
pub(in crate::parser) struct CoerceSubstr {
pub value: BoxedExpression,
pub start_idx: Option<usize>,
pub end_idx: Option<usize>,
}

impl Expression for CoerceSubstr {
fn calculate(&self, json: &[u8]) -> crate::parser::parse::Result<Value> {
let v = self.value.calculate(json)?;
match v {
Value::String(s) => match (self.start_idx, self.end_idx) {
(Some(start), Some(end)) => Ok(s
.get(start..end)
.map_or_else(|| Value::Null, |s| Value::String(s.to_string()))),
(Some(start), None) => Ok(s
.get(start..)
.map_or_else(|| Value::Null, |s| Value::String(s.to_string()))),
(None, Some(end)) => Ok(s
.get(..end)
.map_or_else(|| Value::Null, |s| Value::String(s.to_string()))),
_ => Err(Error::UnsupportedCOERCE(format!(
"COERCE substr for {s}, [{:?}:{:?}]",
self.start_idx, self.end_idx
))),
},
v => Err(Error::UnsupportedCOERCE(format!("{v} COERCE substr",))),
}
}
}
25 changes: 25 additions & 0 deletions src/parser/coercions/title.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Value};

#[derive(Debug)]
pub(in crate::parser) struct CoerceTitle {
pub value: BoxedExpression,
}

impl Expression for CoerceTitle {
fn calculate(&self, json: &[u8]) -> crate::parser::parse::Result<Value> {
let v = self.value.calculate(json)?;
match v {
Value::String(s) => {
let mut c = s.chars();
match c.next() {
None => Ok(Value::String(s)),
Some(f) => Ok(Value::String(
f.to_uppercase().collect::<String>() + c.as_str().to_lowercase().as_str(),
)),
}
}
v => Err(Error::UnsupportedCOERCE(format!("{v} COERCE title",))),
}
}
}
17 changes: 17 additions & 0 deletions src/parser/coercions/uppercase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Value};

#[derive(Debug)]
pub(in crate::parser) struct CoerceUppercase {
pub value: BoxedExpression,
}

impl Expression for CoerceUppercase {
fn calculate(&self, json: &[u8]) -> crate::parser::parse::Result<Value> {
let v = self.value.calculate(json)?;
match v {
Value::String(s) => Ok(Value::String(s.to_uppercase())),
v => Err(Error::UnsupportedCOERCE(format!("{v} COERCE uppercase",))),
}
}
}
25 changes: 25 additions & 0 deletions src/parser/expressions/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Result, Value};

#[derive(Debug)]
pub(in crate::parser) struct Add {
pub left: BoxedExpression,
pub right: BoxedExpression,
}

impl Expression for Add {
fn calculate(&self, json: &[u8]) -> Result<Value> {
let left = self.left.calculate(json)?;
let right = self.right.calculate(json)?;

match (left, right) {
(Value::String(s1), Value::String(ref s2)) => Ok(Value::String(s1 + s2)),
(Value::String(s1), Value::Null) => Ok(Value::String(s1)),
(Value::Null, Value::String(s2)) => Ok(Value::String(s2)),
(Value::Number(n1), Value::Number(n2)) => Ok(Value::Number(n1 + n2)),
(Value::Number(n1), Value::Null) => Ok(Value::Number(n1)),
(Value::Null, Value::Number(n2)) => Ok(Value::Number(n2)),
(l, r) => Err(Error::UnsupportedTypeComparison(format!("{l} + {r}",))),
}
}
}
27 changes: 27 additions & 0 deletions src/parser/expressions/and.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Result, Value};

#[derive(Debug)]
pub(in crate::parser) struct And {
pub left: BoxedExpression,
pub right: BoxedExpression,
}

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) {
(Value::Bool(b1), Value::Bool(b2)) => Ok(Value::Bool(b1 && b2)),
(l, r) => Err(Error::UnsupportedTypeComparison(format!("{l} && {r}",))),
}
}
}
17 changes: 17 additions & 0 deletions src/parser/expressions/arr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Expression, Result, Value};

#[derive(Debug)]
pub(in crate::parser) struct Arr {
pub arr: Vec<BoxedExpression>,
}

impl Expression for Arr {
fn calculate(&self, json: &[u8]) -> Result<Value> {
let mut arr = Vec::new();
for e in &self.arr {
arr.push(e.calculate(json)?);
}
Ok(Value::Array(arr))
}
}
35 changes: 35 additions & 0 deletions src/parser/expressions/between.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Result, Value};

#[derive(Debug)]
pub(in crate::parser) struct Between {
pub left: BoxedExpression,
pub right: BoxedExpression,
pub value: BoxedExpression,
}

impl Expression for Between {
fn calculate(&self, json: &[u8]) -> Result<Value> {
let left = self.left.calculate(json)?;
let right = self.right.calculate(json)?;
let value = self.value.calculate(json)?;

match (value, left, right) {
(Value::String(v), Value::String(lhs), Value::String(rhs)) => {
Ok(Value::Bool(v > lhs && v < rhs))
}
(Value::Number(v), Value::Number(lhs), Value::Number(rhs)) => {
Ok(Value::Bool(v > lhs && v < rhs))
}
(Value::DateTime(v), Value::DateTime(lhs), Value::DateTime(rhs)) => {
Ok(Value::Bool(v > lhs && v < rhs))
}
(Value::Null, _, _) | (_, Value::Null, _) | (_, _, Value::Null) => {
Ok(Value::Bool(false))
}
(v, lhs, rhs) => Err(Error::UnsupportedTypeComparison(format!(
"{v} BETWEEN {lhs} {rhs}",
))),
}
}
}
12 changes: 12 additions & 0 deletions src/parser/expressions/bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::parser::{Expression, Result, Value};

#[derive(Debug)]
pub(in crate::parser) struct Bool {
pub b: bool,
}

impl Expression for Bool {
fn calculate(&self, _: &[u8]) -> Result<Value> {
Ok(Value::Bool(self.b))
}
}
22 changes: 22 additions & 0 deletions src/parser/expressions/contains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::parser::parse::BoxedExpression;
use crate::parser::{Error, Expression, Result, Value};

#[derive(Debug)]
pub(in crate::parser) struct Contains {
pub left: BoxedExpression,
pub right: BoxedExpression,
}

impl Expression for Contains {
fn calculate(&self, json: &[u8]) -> Result<Value> {
let left = self.left.calculate(json)?;
let right = self.right.calculate(json)?;
match (left, right) {
(Value::String(s1), Value::String(s2)) => Ok(Value::Bool(s1.contains(&s2))),
(Value::Array(arr1), v) => Ok(Value::Bool(arr1.contains(&v))),
(l, r) => Err(Error::UnsupportedTypeComparison(format!(
"{l} CONTAINS {r}",
))),
}
}
}
Loading