✨ updates
This commit is contained in:
parent
77185abd6a
commit
b1ad069731
4 changed files with 114 additions and 8 deletions
28
src/lib.rs
28
src/lib.rs
|
@ -402,10 +402,18 @@ fn match_operator(
|
|||
}
|
||||
return Err(FilterError::KeyNotFound);
|
||||
}
|
||||
"$in" => {
|
||||
"$in" | "$contains" => {
|
||||
if let Some(valb) = obj.get(key) {
|
||||
if let serde_json::Value::Array(list) = valb {
|
||||
return Ok(list.iter().any(|x| x == op_arg));
|
||||
match valb {
|
||||
serde_json::Value::String(str) => {
|
||||
if let serde_json::Value::String(op_arg) = op_arg {
|
||||
return Ok(str.contains(op_arg));
|
||||
}
|
||||
}
|
||||
serde_json::Value::Array(list) => {
|
||||
return Ok(list.iter().any(|x| x == op_arg));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
return Err(FilterError::InvalidFilter);
|
||||
}
|
||||
|
@ -487,6 +495,20 @@ fn match_operator(
|
|||
}
|
||||
return Err(FilterError::KeyNotFound);
|
||||
}
|
||||
"$range" => {
|
||||
let x = op_arg.as_array().unwrap().get(0).unwrap();
|
||||
let y = op_arg.as_array().unwrap().get(1).unwrap();
|
||||
return match_operator(
|
||||
&json!({
|
||||
"$and": [
|
||||
{ key: { "$gte": x}},
|
||||
{ key: { "$lte": y}}
|
||||
]
|
||||
}),
|
||||
raw_obj,
|
||||
key,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
if op.starts_with('$') {
|
||||
return Err(FilterError::UnknownOperator);
|
||||
|
|
64
src/test.rs
64
src/test.rs
|
@ -109,6 +109,70 @@ mod tests {
|
|||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_contains() {
|
||||
assert!(matches(
|
||||
&json!({
|
||||
"key": {
|
||||
"$contains": "world"
|
||||
}
|
||||
}),
|
||||
&json!({
|
||||
"key": "hello world"
|
||||
})
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_op() {
|
||||
assert!(matches(
|
||||
&json!({"key": { "$range": [18, 30] }}),
|
||||
&json!({
|
||||
"key": 20
|
||||
})
|
||||
));
|
||||
|
||||
assert!(!matches(
|
||||
&json!({"key": { "$range": [18, 30] }}),
|
||||
&json!({
|
||||
"key": 15
|
||||
})
|
||||
));
|
||||
|
||||
assert!(!matches(
|
||||
&json!({"key": { "$range": [18, 30] }}),
|
||||
&json!({
|
||||
"key": 40
|
||||
})
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_array_contains() {
|
||||
assert!(matches(
|
||||
&json!({
|
||||
"key": {
|
||||
"$contains": 3
|
||||
}
|
||||
}),
|
||||
&json!({
|
||||
"key": [1,2,3,4,5]
|
||||
})
|
||||
));
|
||||
|
||||
assert!(matches(
|
||||
&json!({
|
||||
"$not": {
|
||||
"key": {
|
||||
"$contains": 3
|
||||
}}
|
||||
}),
|
||||
&json!({
|
||||
"key": [1,2,4,5]
|
||||
})
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_array() {
|
||||
assert!(matches(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue