Skip to content

Commit

Permalink
Add support for != opeartor
Browse files Browse the repository at this point in the history
Update README
  • Loading branch information
mitghi committed Mar 11, 2023
1 parent 67daca7 commit b45da47
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[<img src="https://img.shields.io/badge/try-online%20repl-brightgreen"></img>](https://jetro.io)
![GitHub](https://img.shields.io/github/license/mitghi/jetro)

Jetro is a tool for transforming, querying and comparing data in JSON format.
Jetro is a tool with custom DSL for transforming, querying and comparing data in JSON format.

```rust
let data = serde_json::json!({
Expand Down Expand Up @@ -34,6 +34,17 @@ let output: Option<Output> = values.from_index(0);

# example

Jetro combines access path with functions which operate on those values matched within the pipeline.

By convention, functions are denoted using # operator. Functions can be composed.

- #pick('string' | expression, ...) [ as | as* 'binding_value' ]
- #all
- #sum
- #formats('format with placeholder {} {}', 'key_a', 'key_b') [ as | as* 'binding_value' ]
- #filter('target_key' (>, <, >=, <=, ==, ~=) (string, boolean, number))


```json
{
"bar": {
Expand Down
4 changes: 4 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum FilterOp {
Lq,
Gq,
Eq,
NotEq,
Almost,
}

Expand Down Expand Up @@ -111,6 +112,7 @@ impl FilterOp {
"<" => Some(Self::Less),
">" => Some(Self::Gt),
"~=" => Some(Self::Almost),
"!=" => Some(Self::NotEq),
_ => None,
}
}
Expand Down Expand Up @@ -304,6 +306,7 @@ macro_rules! do_comparision {
FilterOp::Gt => $lhs > $rhs,
FilterOp::Lq => $lhs <= $rhs,
FilterOp::Gq => $lhs >= $rhs,
FilterOp::NotEq => $lhs != $rhs,
FilterOp::Almost => $lhs.to_lowercase() == $rhs.to_lowercase(),
_ => false,
}
Expand All @@ -316,6 +319,7 @@ macro_rules! do_comparision {
FilterOp::Gt => $lhs > $rhs,
FilterOp::Lq => $lhs <= $rhs,
FilterOp::Gq => $lhs >= $rhs,
FilterOp::NotEq => $lhs != $rhs,
FilterOp::Almost => {
todo!("implement 'almost' operator");
}
Expand Down
1 change: 1 addition & 0 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Module containing string formater for built-in #format function.
use crate::context::FormatOp;
use dynfmt::{Format, SimpleCurlyFormat};
use serde_json::Value;
Expand Down
3 changes: 2 additions & 1 deletion src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ greater_equal = { ">=" }
less_equal = { "<=" }
equal = { "==" }
almost = { "~=" }
cmp = { almost | greater_equal | less_equal | equal | greater | less }
not_euql = { "!=" }
cmp = { almost | not_euql | greater_equal | less_equal | equal | greater | less }
true_bool = { "true" }
false_bool = { "false" }
truthy = { true_bool | false_bool }
Expand Down
21 changes: 21 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,4 +722,25 @@ mod test {
]
);
}

#[test]
fn filter_not_equal() {
let actions = parse(">/foo/#filter('some_key' != 10)").unwrap();
assert_eq!(
actions,
vec![
Filter::Root,
Filter::Child("foo".to_string()),
Filter::MultiFilter(Rc::new(RefCell::new(FilterAST {
operand: FilterLogicalOp::None,
left: Some(Rc::new(RefCell::new(FilterInner::Cond {
left: "some_key".to_string(),
op: FilterOp::NotEq,
right: FilterInnerRighthand::Number(10),
}))),
right: None,
})))
]
);
}
}

0 comments on commit b45da47

Please sign in to comment.