project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use crate::handler::{ |
| 2 | Handler, ListHandler, MapHandler, MovableListHandler, TextHandler, TreeHandler, ValueOrHandler, |
| 3 | }; |
| 4 | use crate::jsonpath::ast::{ |
| 5 | ComparisonOperator, FilterExpression, LogicalOperator, Segment, Selector, |
| 6 | }; |
| 7 | use crate::jsonpath::JSONPathParser; |
| 8 | use crate::{HandlerTrait, LoroDoc}; |
| 9 | use loro_common::{ContainerID, LoroValue}; |
| 10 | use std::ops::ControlFlow; |
| 11 | use thiserror::Error; |
| 12 | |
| 13 | #[derive(Error, Debug)] |
| 14 | pub enum JsonPathError { |
| 15 | #[error("Invalid JSONPath: {0}")] |
| 16 | InvalidJsonPath(String), |
| 17 | #[error("JSONPath evaluation error: {0}")] |
| 18 | EvaluationError(String), |
| 19 | } |
| 20 | impl LoroDoc { |
| 21 | #[inline] |
| 22 | pub fn jsonpath(&self, jsonpath: &str) -> Result<Vec<ValueOrHandler>, JsonPathError> { |
| 23 | evaluate_jsonpath(self, jsonpath) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | enum ExprValue { |
| 28 | Bool(bool), |
| 29 | Value(LoroValue), |
| 30 | Nodes(Vec<ValueOrHandler>), |
| 31 | } |
| 32 | |
| 33 | pub fn evaluate_jsonpath( |
| 34 | root: &dyn PathValue, |
| 35 | jsonpath: &str, |
| 36 | ) -> Result<Vec<ValueOrHandler>, JsonPathError> { |
| 37 | let parser = JSONPathParser::new(); |
| 38 | let query = parser |
| 39 | .parse(jsonpath) |
| 40 | .map_err(|e| JsonPathError::InvalidJsonPath(e.to_string()))?; |
| 41 | |
| 42 | let mut results = Vec::new(); |
| 43 | evaluate_segment(root, &query.segments, &mut results); |
| 44 | Ok(results) |
| 45 | } |
| 46 | |
| 47 | fn evaluate_segment(root: &dyn PathValue, segment: &Segment, results: &mut Vec<ValueOrHandler>) { |
| 48 | match segment { |
| 49 | Segment::Root {} => { |
| 50 | if let Ok(cloned) = root.clone_this() { |
| 51 | results.push(cloned); |
| 52 | } |
| 53 | } |
| 54 | Segment::Child { left, selectors } => { |
| 55 | if left.is_singular() && matches!(**left, Segment::Root {}) { |
| 56 | apply_selectors(root, root, selectors, results); |
| 57 | } else { |
| 58 | let mut intermediate = Vec::new(); |
| 59 | evaluate_segment(root, left, &mut intermediate); |
| 60 | for node in intermediate { |
| 61 | apply_selectors(root, &node, selectors, results); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | Segment::Recursive { left, selectors } => { |
| 66 | if left.is_singular() && matches!(**left, Segment::Root {}) { |
| 67 | recursive_descent(root, root, selectors, results); |
| 68 | } else { |
| 69 | let mut intermediate = Vec::new(); |
| 70 | evaluate_segment(root, left, &mut intermediate); |
| 71 | for node in intermediate { |
| 72 | recursive_descent(root, &node, selectors, results); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn apply_selectors( |
| 80 | root: &dyn PathValue, |
| 81 | node: &dyn PathValue, |
| 82 | selectors: &[Selector], |
| 83 | results: &mut Vec<ValueOrHandler>, |
| 84 | ) { |
| 85 | for sel in selectors { |
| 86 | match sel { |
| 87 | Selector::Name { name } => { |
| 88 | if let Some(child) = node.get_by_key(name) { |
| 89 | results.push(child); |
| 90 | } |
| 91 | } |
| 92 | Selector::Index { index } => { |
| 93 | let mut idx = *index; |
| 94 | if idx < 0 { |
| 95 | idx += node.length_for_path() as i64; |
| 96 | } |
| 97 | if idx >= 0 { |
| 98 | if let Some(child) = node.get_by_index(idx as isize) { |
| 99 | results.push(child); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | Selector::Slice { start, stop, step } => { |
| 104 | let len = node.length_for_path() as i64; |
| 105 | let eff_start = start.unwrap_or(if step.unwrap_or(1) > 0 { 0 } else { len - 1 }); |
| 106 | let eff_stop = stop.unwrap_or(if step.unwrap_or(1) > 0 { len } else { -len - 1 }); |
| 107 | let eff_step = step.unwrap_or(1); |
| 108 | |
| 109 | if eff_step == 0 { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | let mut start_idx = if eff_start < 0 { |
| 114 | eff_start + len |
| 115 | } else { |
| 116 | eff_start |
| 117 | }; |
| 118 | let mut stop_idx = if eff_stop < 0 { |
| 119 | eff_stop + len |
| 120 | } else { |
| 121 | eff_stop |
| 122 | }; |
| 123 | start_idx = start_idx.max(0).min(len); |
| 124 | stop_idx = stop_idx.max(-1).min(len); |
| 125 | |
| 126 | // Pre-allocate if positive step for better perf |
| 127 | if eff_step > 0 { |
| 128 | let approx_count = ((stop_idx - start_idx) / eff_step).max(0) as usize; |
| 129 | results.reserve(approx_count); |
| 130 | let mut i = start_idx; |
| 131 | while i < stop_idx { |
| 132 | if let Some(child) = node.get_by_index(i as isize) { |
| 133 | results.push(child); |
| 134 | } |
| 135 | i += eff_step; |
| 136 | } |
| 137 | } else { |
| 138 | let approx_count = ((start_idx - stop_idx) / -eff_step).max(0) as usize; |
| 139 | results.reserve(approx_count); |
| 140 | let mut i = start_idx; |
| 141 | while i > stop_idx { |
| 142 | if let Some(child) = node.get_by_index(i as isize) { |
| 143 | results.push(child); |
| 144 | } |
| 145 | i += eff_step; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | Selector::Wild {} => { |
| 150 | node.for_each_for_path(&mut |child| { |
| 151 | results.push(child); |
| 152 | ControlFlow::Continue(()) |
| 153 | }); |
| 154 | } |
| 155 | Selector::Filter { expression } => { |
| 156 | node.for_each_for_path(&mut |child| { |
| 157 | if eval_filter_expr(root, &child, expression) { |
| 158 | results.push(child); |
| 159 | } |
| 160 | ControlFlow::Continue(()) |
| 161 | }); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | fn recursive_descent( |
| 168 | root: &dyn PathValue, |
| 169 | node: &dyn PathValue, |
| 170 | selectors: &[Selector], |
| 171 | results: &mut Vec<ValueOrHandler>, |
| 172 | ) { |
| 173 | // 1. apply selectors to the *current* node |
| 174 | apply_selectors(root, node, selectors, results); |
| 175 | |
| 176 | // 2. recurse into children |
| 177 | node.for_each_for_path(&mut |child| { |
| 178 | recursive_descent(root, &child, selectors, results); |
| 179 | ControlFlow::Continue(()) |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | fn eval_filter_expr( |
| 184 | root: &dyn PathValue, |
| 185 | current: &dyn PathValue, |
| 186 | expr: &FilterExpression, |
| 187 | ) -> bool { |
| 188 | to_logical(eval_expr(root, current, expr)) |
| 189 | } |
| 190 | |
| 191 | fn eval_expr(root: &dyn PathValue, current: &dyn PathValue, expr: &FilterExpression) -> ExprValue { |
| 192 | match expr { |
| 193 | FilterExpression::True_ {} => ExprValue::Bool(true), |
| 194 | FilterExpression::False_ {} => ExprValue::Bool(false), |
| 195 | FilterExpression::Null {} => ExprValue::Value(LoroValue::Null), |
| 196 | FilterExpression::StringLiteral { value } => { |
| 197 | ExprValue::Value(LoroValue::String(value.clone().into())) |
| 198 | } |
| 199 | FilterExpression::Int { value } => ExprValue::Value(LoroValue::I64(*value)), |
| 200 | FilterExpression::Float { value } => ExprValue::Value(LoroValue::Double(*value)), |
| 201 | FilterExpression::Array { values } => { |
| 202 | let mut list = Vec::with_capacity(values.len()); |
| 203 | for val in values { |
| 204 | match eval_expr(root, current, val) { |
| 205 | ExprValue::Value(v) => list.push(v), |
| 206 | ExprValue::Bool(b) => list.push(LoroValue::Bool(b)), |
| 207 | ExprValue::Nodes(_) => return ExprValue::Value(LoroValue::Null), |
| 208 | } |
| 209 | } |
| 210 | ExprValue::Value(LoroValue::List(list.into())) |
| 211 | } |
| 212 | FilterExpression::Not { expression } => { |
| 213 | ExprValue::Bool(!to_logical(eval_expr(root, current, expression))) |
| 214 | } |
| 215 | FilterExpression::Logical { |
| 216 | left, |
| 217 | operator, |
| 218 | right, |
| 219 | } => { |
| 220 | // Short-circuit evaluation |
| 221 | let l = to_logical(eval_expr(root, current, left)); |
| 222 | match operator { |
| 223 | LogicalOperator::And => { |
| 224 | if !l { |
| 225 | return ExprValue::Bool(false); |
| 226 | } |
| 227 | ExprValue::Bool(to_logical(eval_expr(root, current, right))) |
| 228 | } |
| 229 | LogicalOperator::Or => { |
| 230 | if l { |
| 231 | return ExprValue::Bool(true); |
| 232 | } |
| 233 | ExprValue::Bool(to_logical(eval_expr(root, current, right))) |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | FilterExpression::Comparison { |
| 238 | left, |
| 239 | operator, |
| 240 | right, |
| 241 | } => { |
| 242 | let l = eval_expr(root, current, left); |
| 243 | let r = eval_expr(root, current, right); |
| 244 | ExprValue::Bool(compare_expr(l, operator, r)) |
| 245 | } |
| 246 | FilterExpression::RelativeQuery { query } => { |
| 247 | let mut query_results = Vec::new(); |
| 248 | evaluate_segment(current, &query.segments, &mut query_results); |
| 249 | ExprValue::Nodes(query_results) |
| 250 | } |
| 251 | FilterExpression::RootQuery { query } => { |
| 252 | let mut query_results = Vec::new(); |
| 253 | evaluate_segment(root, &query.segments, &mut query_results); |
| 254 | ExprValue::Nodes(query_results) |
| 255 | } |
| 256 | FilterExpression::Function { name, args } => eval_function(root, current, name, args), |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | #[inline] |
| 261 | fn eval_function( |
| 262 | root: &dyn PathValue, |
| 263 | current: &dyn PathValue, |
| 264 | name: &str, |
| 265 | args: &[FilterExpression], |
| 266 | ) -> ExprValue { |
| 267 | match name { |
| 268 | "count" if args.len() == 1 => { |
| 269 | let arg = eval_expr(root, current, &args[0]); |
| 270 | if let ExprValue::Nodes(ns) = arg { |
| 271 | ExprValue::Value(LoroValue::I64(ns.len() as i64)) |
| 272 | } else { |
| 273 | ExprValue::Value(LoroValue::I64(0)) |
| 274 | } |
| 275 | } |
| 276 | "length" if args.len() == 1 => { |
| 277 | let arg = eval_expr(root, current, &args[0]); |
| 278 | if let ExprValue::Value(v) = arg { |
| 279 | let len = match v { |
| 280 | LoroValue::List(l) => l.len(), |
| 281 | LoroValue::Map(m) => m.len(), |
| 282 | LoroValue::String(s) => s.len(), |
| 283 | _ => 0, |
| 284 | }; |
| 285 | ExprValue::Value(LoroValue::I64(len as i64)) |
| 286 | } else { |
| 287 | ExprValue::Value(LoroValue::I64(0)) |
| 288 | } |
| 289 | } |
| 290 | "value" if args.len() == 1 => { |
| 291 | let arg = eval_expr(root, current, &args[0]); |
| 292 | if let ExprValue::Nodes(ns) = arg { |
| 293 | if ns.len() == 1 { |
| 294 | match &ns[0] { |
| 295 | ValueOrHandler::Value(v) => ExprValue::Value(v.clone()), |
| 296 | ValueOrHandler::Handler(h) => ExprValue::Value(h.get_value()), |
| 297 | } |
| 298 | } else { |
| 299 | ExprValue::Value(LoroValue::Null) |
| 300 | } |
| 301 | } else { |
| 302 | ExprValue::Value(LoroValue::Null) |
| 303 | } |
| 304 | } |
| 305 | "match" => { |
| 306 | unimplemented!("JSONPath function `match()` is declared but not implemented"); |
| 307 | } |
| 308 | "search" => { |
| 309 | unimplemented!("JSONPath function `search()` is declared but not implemented"); |
| 310 | } |
| 311 | other => { |
| 312 | unimplemented!( |
| 313 | "JSONPath function `{}` is declared but does not have an evaluator", |
| 314 | other |
| 315 | ); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | #[inline(always)] |
| 321 | fn to_logical(v: ExprValue) -> bool { |
| 322 | match v { |
| 323 | ExprValue::Bool(b) => b, |
| 324 | ExprValue::Value(v) => match v { |
| 325 | LoroValue::Bool(b) => b, |
| 326 | LoroValue::Null => false, |
| 327 | LoroValue::String(s) => !s.is_empty(), |
| 328 | LoroValue::I64(0) => false, |
| 329 | LoroValue::I64(_) => true, |
| 330 | LoroValue::Double(f) => f != 0.0 && !f.is_nan(), |
| 331 | LoroValue::List(l) => !l.is_empty(), |
| 332 | LoroValue::Map(m) => !m.is_empty(), |
| 333 | _ => false, |
| 334 | }, |
| 335 | ExprValue::Nodes(ns) => !ns.is_empty(), |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | #[inline] |
| 340 | fn compare_expr(l: ExprValue, op: &ComparisonOperator, r: ExprValue) -> bool { |
| 341 | match (l, r) { |
| 342 | (ExprValue::Value(l), ExprValue::Value(r)) => compare_values(&l, op, &r), |
| 343 | (ExprValue::Value(l), ExprValue::Bool(r)) => compare_values(&l, op, &LoroValue::Bool(r)), |
| 344 | (ExprValue::Bool(l), ExprValue::Value(r)) => compare_values(&LoroValue::Bool(l), op, &r), |
| 345 | (ExprValue::Bool(l), ExprValue::Bool(r)) => compare_bools(l, op, r), |
| 346 | (ExprValue::Nodes(ls), ExprValue::Nodes(rs)) => { |
| 347 | // Optimized: check if either is empty first |
| 348 | if ls.is_empty() || rs.is_empty() { |
| 349 | return false; |
| 350 | } |
| 351 | ls.iter().any(|a| { |
| 352 | let a_val = get_value(a); |
| 353 | rs.iter().any(|b| compare_values(&a_val, op, &get_value(b))) |
| 354 | }) |
| 355 | } |
| 356 | (ExprValue::Nodes(ls), v) => { |
| 357 | if ls.is_empty() { |
| 358 | return false; |
| 359 | } |
| 360 | let r_val = get_other_value(&v); |
| 361 | ls.iter().any(|a| compare_values(&get_value(a), op, &r_val)) |
| 362 | } |
| 363 | (v, ExprValue::Nodes(rs)) => { |
| 364 | if rs.is_empty() { |
| 365 | return false; |
| 366 | } |
| 367 | let l_val = get_other_value(&v); |
| 368 | rs.iter().any(|b| compare_values(&l_val, op, &get_value(b))) |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | #[inline(always)] |
| 374 | fn compare_values(l: &LoroValue, op: &ComparisonOperator, r: &LoroValue) -> bool { |
| 375 | match op { |
| 376 | ComparisonOperator::In => { |
| 377 | if let LoroValue::List(list) = r { |
| 378 | list.iter() |
| 379 | .any(|item| compare_values(l, &ComparisonOperator::Eq, item)) |
| 380 | } else { |
| 381 | false |
| 382 | } |
| 383 | } |
| 384 | _ => match (l, r) { |
| 385 | (LoroValue::Double(a), LoroValue::Double(b)) => compare_nums(*a, op, *b), |
| 386 | (LoroValue::I64(a), LoroValue::I64(b)) => compare_i64(*a, op, *b), |
| 387 | (LoroValue::Double(a), LoroValue::I64(b)) => compare_nums(*a, op, *b as f64), |
| 388 | (LoroValue::I64(a), LoroValue::Double(b)) => compare_nums(*a as f64, op, *b), |
| 389 | (LoroValue::String(a), LoroValue::String(b)) => { |
| 390 | compare_strs(a.as_ref(), op, b.as_ref()) |
| 391 | } |
| 392 | (LoroValue::Bool(a), LoroValue::Bool(b)) => compare_bools(*a, op, *b), |
| 393 | (LoroValue::Null, LoroValue::Null) => matches!(op, ComparisonOperator::Eq), |
| 394 | _ => false, |
| 395 | }, |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | #[inline(always)] |
| 400 | fn compare_i64(a: i64, op: &ComparisonOperator, b: i64) -> bool { |
| 401 | match op { |
| 402 | ComparisonOperator::Eq => a == b, |
| 403 | ComparisonOperator::Ne => a != b, |
| 404 | ComparisonOperator::Lt => a < b, |
| 405 | ComparisonOperator::Le => a <= b, |
| 406 | ComparisonOperator::Gt => a > b, |
| 407 | ComparisonOperator::Ge => a >= b, |
| 408 | _ => false, |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | #[inline(always)] |
| 413 | fn compare_nums(a: f64, op: &ComparisonOperator, b: f64) -> bool { |
| 414 | match op { |
| 415 | ComparisonOperator::Eq => (a - b).abs() < f64::EPSILON, |
| 416 | ComparisonOperator::Ne => (a - b).abs() >= f64::EPSILON, |
| 417 | ComparisonOperator::Lt => a < b, |
| 418 | ComparisonOperator::Le => a <= b, |
| 419 | ComparisonOperator::Gt => a > b, |
| 420 | ComparisonOperator::Ge => a >= b, |
| 421 | _ => false, |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | #[inline(always)] |
| 426 | fn compare_strs(a: &str, op: &ComparisonOperator, b: &str) -> bool { |
| 427 | match op { |
| 428 | ComparisonOperator::Eq => a == b, |
| 429 | ComparisonOperator::Ne => a != b, |
| 430 | ComparisonOperator::Lt => a < b, |
| 431 | ComparisonOperator::Le => a <= b, |
| 432 | ComparisonOperator::Gt => a > b, |
| 433 | ComparisonOperator::Ge => a >= b, |
| 434 | ComparisonOperator::Contains => a.contains(b), |
| 435 | _ => false, |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | #[inline(always)] |
| 440 | fn compare_bools(a: bool, op: &ComparisonOperator, b: bool) -> bool { |
| 441 | match op { |
| 442 | ComparisonOperator::Eq => a == b, |
| 443 | ComparisonOperator::Ne => a != b, |
| 444 | _ => false, |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | #[inline(always)] |
| 449 | fn get_value(node: &ValueOrHandler) -> LoroValue { |
| 450 | match node { |
| 451 | ValueOrHandler::Value(v) => v.clone(), |
| 452 | ValueOrHandler::Handler(h) => h.get_value(), |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | #[inline(always)] |
| 457 | fn get_other_value(v: &ExprValue) -> LoroValue { |
| 458 | match v { |
| 459 | ExprValue::Value(v) => v.clone(), |
| 460 | ExprValue::Bool(b) => LoroValue::Bool(*b), |
| 461 | _ => LoroValue::Null, |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // Implement necessary trait bounds for PathValue |
| 466 | pub trait PathValue { |
| 467 | fn get_by_key(&self, key: &str) -> Option<ValueOrHandler>; |
| 468 | fn get_by_index(&self, index: isize) -> Option<ValueOrHandler>; |
| 469 | fn for_each_for_path(&self, f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>); |
| 470 | fn length_for_path(&self) -> usize; |
| 471 | fn get_child_by_id(&self, id: ContainerID) -> Option<Handler>; |
| 472 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError>; |
| 473 | } |
| 474 | |
| 475 | // Implement PathValue for ValueOrHandler |
| 476 | impl PathValue for ValueOrHandler { |
| 477 | fn get_by_key(&self, key: &str) -> Option<ValueOrHandler> { |
| 478 | match self { |
| 479 | ValueOrHandler::Value(v) => v.get_by_key(key).cloned().map(ValueOrHandler::Value), |
| 480 | ValueOrHandler::Handler(h) => h.get_by_key(key), |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | fn get_by_index(&self, index: isize) -> Option<ValueOrHandler> { |
| 485 | match self { |
| 486 | ValueOrHandler::Value(v) => v.get_by_index(index).cloned().map(ValueOrHandler::Value), |
| 487 | ValueOrHandler::Handler(h) => h.get_by_index(index), |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | fn for_each_for_path(&self, f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 492 | match self { |
| 493 | ValueOrHandler::Value(v) => v.for_each_for_path(f), |
| 494 | ValueOrHandler::Handler(h) => h.for_each_for_path(f), |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | fn length_for_path(&self) -> usize { |
| 499 | match self { |
| 500 | ValueOrHandler::Value(v) => v.length_for_path(), |
| 501 | ValueOrHandler::Handler(h) => h.length_for_path(), |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | fn get_child_by_id(&self, id: ContainerID) -> Option<Handler> { |
| 506 | match self { |
| 507 | ValueOrHandler::Handler(h) => h.get_child_by_id(id), |
| 508 | _ => None, |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 513 | match self { |
| 514 | ValueOrHandler::Value(v) => Ok(ValueOrHandler::Value(v.clone())), |
| 515 | ValueOrHandler::Handler(h) => Ok(ValueOrHandler::Handler(h.clone())), |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // Implement PathValue for LoroDoc |
| 521 | impl PathValue for LoroDoc { |
| 522 | fn get_by_key(&self, key: &str) -> Option<ValueOrHandler> { |
| 523 | self.get_by_str_path(key) |
| 524 | } |
| 525 | |
| 526 | fn get_by_index(&self, _index: isize) -> Option<ValueOrHandler> { |
| 527 | None // LoroDoc doesn't support index-based access |
| 528 | } |
| 529 | |
| 530 | fn for_each_for_path(&self, f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 531 | let roots = self.state.lock().preferred_root_containers(); |
| 532 | let arena = self.arena(); |
| 533 | for c in roots { |
| 534 | let cid = arena.idx_to_id(c).unwrap(); |
| 535 | let h = self.get_handler(cid).unwrap(); |
| 536 | if f(ValueOrHandler::Handler(h)) == ControlFlow::Break(()) { |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | fn length_for_path(&self) -> usize { |
| 543 | self.state.lock().preferred_root_containers().len() |
| 544 | } |
| 545 | |
| 546 | fn get_child_by_id(&self, id: ContainerID) -> Option<Handler> { |
| 547 | self.get_handler(id) |
| 548 | } |
| 549 | |
| 550 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 551 | Ok(ValueOrHandler::Value(self.get_deep_value())) |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // Implement PathValue for Handler |
| 556 | impl PathValue for Handler { |
| 557 | fn get_by_key(&self, key: &str) -> Option<ValueOrHandler> { |
| 558 | match self { |
| 559 | Handler::Map(h) => h.get_by_key(key), |
| 560 | Handler::Tree(h) => h.get_by_key(key), |
| 561 | _ => None, |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | fn get_by_index(&self, index: isize) -> Option<ValueOrHandler> { |
| 566 | match self { |
| 567 | Handler::List(h) => h.get_by_index(index), |
| 568 | Handler::MovableList(h) => h.get_by_index(index), |
| 569 | _ => None, |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | fn for_each_for_path(&self, f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 574 | match self { |
| 575 | Handler::Map(h) => h.for_each_for_path(f), |
| 576 | Handler::List(h) => h.for_each_for_path(f), |
| 577 | Handler::MovableList(h) => h.for_each_for_path(f), |
| 578 | Handler::Tree(h) => h.for_each_for_path(f), |
| 579 | _ => {} |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | fn length_for_path(&self) -> usize { |
| 584 | match self { |
| 585 | Handler::Map(h) => h.length_for_path(), |
| 586 | Handler::List(h) => h.length_for_path(), |
| 587 | Handler::MovableList(h) => h.length_for_path(), |
| 588 | Handler::Tree(h) => h.length_for_path(), |
| 589 | Handler::Text(h) => h.length_for_path(), |
| 590 | _ => 0, |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | fn get_child_by_id(&self, id: ContainerID) -> Option<Handler> { |
| 595 | match self { |
| 596 | Handler::Map(h) => h.get_child_by_id(id), |
| 597 | Handler::List(h) => h.get_child_by_id(id), |
| 598 | Handler::MovableList(h) => h.get_child_by_id(id), |
| 599 | Handler::Tree(h) => h.get_child_by_id(id), |
| 600 | _ => None, |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 605 | Ok(ValueOrHandler::Handler(self.clone())) |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Implementations for specific handlers |
| 610 | impl PathValue for MapHandler { |
| 611 | fn get_by_key(&self, key: &str) -> Option<ValueOrHandler> { |
| 612 | self.get_(key) |
| 613 | } |
| 614 | |
| 615 | fn get_by_index(&self, _index: isize) -> Option<ValueOrHandler> { |
| 616 | None |
| 617 | } |
| 618 | |
| 619 | fn for_each_for_path(&self, f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 620 | let mut done = false; |
| 621 | self.for_each(|_, v| { |
| 622 | if done { |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | if let ControlFlow::Break(_) = f(v) { |
| 627 | done = true; |
| 628 | } |
| 629 | }); |
| 630 | } |
| 631 | |
| 632 | fn length_for_path(&self) -> usize { |
| 633 | self.len() |
| 634 | } |
| 635 | |
| 636 | fn get_child_by_id(&self, id: ContainerID) -> Option<Handler> { |
| 637 | self.get_child_handler(id.to_string().as_str()).ok() |
| 638 | } |
| 639 | |
| 640 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 641 | Ok(ValueOrHandler::Handler(Handler::Map(self.clone()))) |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | impl PathValue for ListHandler { |
| 646 | fn get_by_key(&self, _key: &str) -> Option<ValueOrHandler> { |
| 647 | None |
| 648 | } |
| 649 | |
| 650 | fn get_by_index(&self, index: isize) -> Option<ValueOrHandler> { |
| 651 | if index < 0 { |
| 652 | self.get_(self.len() - (-index) as usize) |
| 653 | } else { |
| 654 | self.get_(index as usize) |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | fn for_each_for_path(&self, f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 659 | let mut done = false; |
| 660 | self.for_each(|v| { |
| 661 | if done { |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | if let ControlFlow::Break(_) = f(v) { |
| 666 | done = true; |
| 667 | } |
| 668 | }); |
| 669 | } |
| 670 | |
| 671 | fn length_for_path(&self) -> usize { |
| 672 | self.len() |
| 673 | } |
| 674 | |
| 675 | fn get_child_by_id(&self, _id: ContainerID) -> Option<Handler> { |
| 676 | unimplemented!() |
| 677 | } |
| 678 | |
| 679 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 680 | Ok(ValueOrHandler::Handler(Handler::List(self.clone()))) |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | impl PathValue for MovableListHandler { |
| 685 | fn get_by_key(&self, _key: &str) -> Option<ValueOrHandler> { |
| 686 | None |
| 687 | } |
| 688 | |
| 689 | fn get_by_index(&self, index: isize) -> Option<ValueOrHandler> { |
| 690 | if index < 0 { |
| 691 | if self.len() > (-index) as usize { |
| 692 | self.get_(self.len() - (-index) as usize) |
| 693 | } else { |
| 694 | None |
| 695 | } |
| 696 | } else { |
| 697 | self.get_(index as usize) |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | fn for_each_for_path(&self, f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 702 | let mut done = false; |
| 703 | self.for_each(|v| { |
| 704 | if done { |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | if let ControlFlow::Break(_) = f(v) { |
| 709 | done = true; |
| 710 | } |
| 711 | }) |
| 712 | } |
| 713 | |
| 714 | fn length_for_path(&self) -> usize { |
| 715 | self.len() |
| 716 | } |
| 717 | |
| 718 | fn get_child_by_id(&self, _id: ContainerID) -> Option<Handler> { |
| 719 | unimplemented!() |
| 720 | } |
| 721 | |
| 722 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 723 | Ok(ValueOrHandler::Handler(Handler::MovableList(self.clone()))) |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | impl PathValue for TextHandler { |
| 728 | fn get_by_key(&self, _key: &str) -> Option<ValueOrHandler> { |
| 729 | None |
| 730 | } |
| 731 | |
| 732 | fn get_by_index(&self, _index: isize) -> Option<ValueOrHandler> { |
| 733 | None |
| 734 | } |
| 735 | |
| 736 | fn for_each_for_path(&self, _f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 737 | // TextHandler doesn't have children to iterate over |
| 738 | } |
| 739 | |
| 740 | fn length_for_path(&self) -> usize { |
| 741 | self.len_unicode() |
| 742 | } |
| 743 | |
| 744 | fn get_child_by_id(&self, _id: ContainerID) -> Option<Handler> { |
| 745 | None |
| 746 | } |
| 747 | |
| 748 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 749 | Ok(ValueOrHandler::Handler(Handler::Text(self.clone()))) |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | impl PathValue for TreeHandler { |
| 754 | fn get_by_key(&self, _key: &str) -> Option<ValueOrHandler> { |
| 755 | None |
| 756 | } |
| 757 | |
| 758 | fn get_by_index(&self, _index: isize) -> Option<ValueOrHandler> { |
| 759 | None |
| 760 | } |
| 761 | |
| 762 | fn for_each_for_path(&self, _f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 763 | unimplemented!() |
| 764 | } |
| 765 | |
| 766 | fn length_for_path(&self) -> usize { |
| 767 | unimplemented!() |
| 768 | } |
| 769 | |
| 770 | fn get_child_by_id(&self, _id: ContainerID) -> Option<Handler> { |
| 771 | unimplemented!() |
| 772 | } |
| 773 | |
| 774 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 775 | Ok(ValueOrHandler::Handler(Handler::Tree(self.clone()))) |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | impl PathValue for LoroValue { |
| 780 | fn get_by_key(&self, key: &str) -> Option<ValueOrHandler> { |
| 781 | match self { |
| 782 | LoroValue::Map(map) => map.get(key).map(|v| ValueOrHandler::Value(v.clone())), |
| 783 | _ => None, |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | fn get_by_index(&self, index: isize) -> Option<ValueOrHandler> { |
| 788 | match self { |
| 789 | LoroValue::List(list) => { |
| 790 | let index = if index < 0 { |
| 791 | if list.len() > (-index) as usize { |
| 792 | list.len() - (-index) as usize |
| 793 | } else { |
| 794 | return None; |
| 795 | } |
| 796 | } else { |
| 797 | index as usize |
| 798 | }; |
| 799 | list.get(index).map(|v| ValueOrHandler::Value(v.clone())) |
| 800 | } |
| 801 | _ => None, |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | fn for_each_for_path(&self, f: &mut dyn FnMut(ValueOrHandler) -> ControlFlow<()>) { |
| 806 | match self { |
| 807 | LoroValue::List(list) => { |
| 808 | for item in list.iter() { |
| 809 | if let ControlFlow::Break(_) = f(ValueOrHandler::Value(item.clone())) { |
| 810 | break; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | LoroValue::Map(map) => { |
| 815 | for (_, value) in map.iter() { |
| 816 | if let ControlFlow::Break(_) = f(ValueOrHandler::Value(value.clone())) { |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | _ => {} |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | fn length_for_path(&self) -> usize { |
| 826 | match self { |
| 827 | LoroValue::List(list) => list.len(), |
| 828 | LoroValue::Map(map) => map.len(), |
| 829 | LoroValue::String(s) => s.len(), |
| 830 | _ => 0, |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | fn get_child_by_id(&self, _id: ContainerID) -> Option<Handler> { |
| 835 | None |
| 836 | } |
| 837 | |
| 838 | fn clone_this(&self) -> Result<ValueOrHandler, JsonPathError> { |
| 839 | Ok(ValueOrHandler::Value(self.clone())) |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | #[cfg(test)] |
| 844 | mod tests { |
| 845 | use super::*; |
| 846 | use crate::{ListHandler, LoroDoc, LoroValue, MapHandler}; |
| 847 | |
| 848 | fn setup_test_doc() -> LoroDoc { |
| 849 | let doc = LoroDoc::new(); |
| 850 | doc.start_auto_commit(); |
| 851 | let store = doc.get_map("store"); |
| 852 | let books = store |
| 853 | .insert_container("books", ListHandler::new_detached()) |
| 854 | .unwrap(); |
| 855 | |
| 856 | // Book 1: 1984 |
| 857 | let book = books |
| 858 | .insert_container(0, MapHandler::new_detached()) |
| 859 | .unwrap(); |
| 860 | book.insert("title", "1984").unwrap(); |
| 861 | book.insert("author", "George Orwell").unwrap(); |
| 862 | book.insert("price", 10).unwrap(); |
| 863 | book.insert("available", true).unwrap(); |
| 864 | book.insert("isbn", "978-0451524935").unwrap(); |
| 865 | |
| 866 | // Book 2: Animal Farm |
| 867 | let book = books |
| 868 | .insert_container(1, MapHandler::new_detached()) |
| 869 | .unwrap(); |
| 870 | book.insert("title", "Animal Farm").unwrap(); |
| 871 | book.insert("author", "George Orwell").unwrap(); |
| 872 | book.insert("price", 8).unwrap(); |
| 873 | book.insert("available", true).unwrap(); |
| 874 | |
| 875 | // Book 3: Brave New World |
| 876 | let book = books |
| 877 | .insert_container(2, MapHandler::new_detached()) |
| 878 | .unwrap(); |
| 879 | book.insert("title", "Brave New World").unwrap(); |
| 880 | book.insert("author", "Aldous Huxley").unwrap(); |
| 881 | book.insert("price", 12).unwrap(); |
| 882 | book.insert("available", false).unwrap(); |
| 883 | book.insert("isbn", "978-0060850524").unwrap(); |
| 884 | |
| 885 | // Book 4: Fahrenheit 451 |
| 886 | let book = books |
| 887 | .insert_container(3, MapHandler::new_detached()) |
| 888 | .unwrap(); |
| 889 | book.insert("title", "Fahrenheit 451").unwrap(); |
| 890 | book.insert("author", "Ray Bradbury").unwrap(); |
| 891 | book.insert("price", 9).unwrap(); |
| 892 | book.insert("available", true).unwrap(); |
| 893 | book.insert("isbn", "978-1451673319").unwrap(); |
| 894 | |
| 895 | // Book 5: The Great Gatsby |
| 896 | let book = books |
| 897 | .insert_container(4, MapHandler::new_detached()) |
| 898 | .unwrap(); |
| 899 | book.insert("title", "The Great Gatsby").unwrap(); |
| 900 | book.insert("author", "F. Scott Fitzgerald").unwrap(); |
| 901 | book.insert("price", LoroValue::Null).unwrap(); |
| 902 | book.insert("available", true).unwrap(); |
| 903 | |
| 904 | // Book 6: To Kill a Mockingbird |
| 905 | let book = books |
| 906 | .insert_container(5, MapHandler::new_detached()) |
| 907 | .unwrap(); |
| 908 | book.insert("title", "To Kill a Mockingbird").unwrap(); |
| 909 | book.insert("author", "Harper Lee").unwrap(); |
| 910 | book.insert("price", 11).unwrap(); |
| 911 | book.insert("available", true).unwrap(); |
| 912 | |
| 913 | // Book 7: The Catcher in the Rye |
| 914 | let book = books |
| 915 | .insert_container(6, MapHandler::new_detached()) |
| 916 | .unwrap(); |
| 917 | book.insert("title", "The Catcher in the Rye").unwrap(); |
| 918 | book.insert("author", "J.D. Salinger").unwrap(); |
| 919 | book.insert("price", 10).unwrap(); |
| 920 | book.insert("available", false).unwrap(); |
| 921 | |
| 922 | // Book 8: Lord of the Flies |
| 923 | let book = books |
| 924 | .insert_container(7, MapHandler::new_detached()) |
| 925 | .unwrap(); |
| 926 | book.insert("title", "Lord of the Flies").unwrap(); |
| 927 | book.insert("author", "William Golding").unwrap(); |
| 928 | book.insert("price", 9).unwrap(); |
| 929 | book.insert("available", true).unwrap(); |
| 930 | |
| 931 | // Book 9: Pride and Prejudice |
| 932 | let book = books |
| 933 | .insert_container(8, MapHandler::new_detached()) |
| 934 | .unwrap(); |
| 935 | book.insert("title", "Pride and Prejudice").unwrap(); |
| 936 | book.insert("author", "Jane Austen").unwrap(); |
| 937 | book.insert("price", 7).unwrap(); |
| 938 | book.insert("available", true).unwrap(); |
| 939 | |
| 940 | // Book 10: The Hobbit |
| 941 | let book = books |
| 942 | .insert_container(9, MapHandler::new_detached()) |
| 943 | .unwrap(); |
| 944 | book.insert("title", "The Hobbit").unwrap(); |
| 945 | book.insert("author", "J.R.R. Tolkien").unwrap(); |
| 946 | book.insert("price", 14).unwrap(); |
| 947 | book.insert("available", true).unwrap(); |
| 948 | |
| 949 | // Additional metadata |
| 950 | store.insert("featured_author", "George Orwell").unwrap(); |
| 951 | let featured_authors = store |
| 952 | .insert_container("featured_authors", ListHandler::new_detached()) |
| 953 | .unwrap(); |
| 954 | featured_authors.push("George Orwell").unwrap(); |
| 955 | featured_authors.push("Aldous Huxley").unwrap(); |
| 956 | featured_authors.push("Ray Bradbury").unwrap(); |
| 957 | store.insert("min_price", 10).unwrap(); |
| 958 | doc |
| 959 | } |
| 960 | |
| 961 | mod jsonpath_selectors { |
| 962 | use super::*; |
| 963 | |
| 964 | #[test] |
| 965 | fn handles_child_selectors() -> Result<(), JsonPathError> { |
| 966 | let doc = setup_test_doc(); |
| 967 | let path = "$.store.books[0].title"; |
| 968 | let result = evaluate_jsonpath(&doc, path)?; |
| 969 | assert_eq!(result.len(), 1); |
| 970 | assert_eq!( |
| 971 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 972 | "1984" |
| 973 | ); |
| 974 | Ok(()) |
| 975 | } |
| 976 | |
| 977 | #[test] |
| 978 | fn handles_wildcard_selector() -> Result<(), JsonPathError> { |
| 979 | let doc = setup_test_doc(); |
| 980 | let path = "$.store.books[*].title"; |
| 981 | let result = evaluate_jsonpath(&doc, path)?; |
| 982 | assert_eq!(result.len(), 10); |
| 983 | let mut titles: Vec<&str> = result |
| 984 | .iter() |
| 985 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 986 | .collect(); |
| 987 | titles.sort(); |
| 988 | let mut expected = vec![ |
| 989 | "1984", |
| 990 | "Animal Farm", |
| 991 | "Brave New World", |
| 992 | "Fahrenheit 451", |
| 993 | "The Great Gatsby", |
| 994 | "To Kill a Mockingbird", |
| 995 | "The Catcher in the Rye", |
| 996 | "Lord of the Flies", |
| 997 | "Pride and Prejudice", |
| 998 | "The Hobbit", |
| 999 | ]; |
| 1000 | expected.sort(); |
| 1001 | assert_eq!(titles, expected); |
| 1002 | Ok(()) |
| 1003 | } |
| 1004 | |
| 1005 | #[test] |
| 1006 | fn handles_recursive_descent() -> Result<(), JsonPathError> { |
| 1007 | let doc = setup_test_doc(); |
| 1008 | let path = "$..title"; |
| 1009 | let result = evaluate_jsonpath(&doc, path)?; |
| 1010 | assert_eq!(result.len(), 10); |
| 1011 | Ok(()) |
| 1012 | } |
| 1013 | |
| 1014 | #[test] |
| 1015 | fn handles_quoted_keys() -> Result<(), JsonPathError> { |
| 1016 | let doc = setup_test_doc(); |
| 1017 | let path = "$.store['books'][0]['title']"; |
| 1018 | let result = evaluate_jsonpath(&doc, path)?; |
| 1019 | assert_eq!(result.len(), 1); |
| 1020 | assert_eq!( |
| 1021 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1022 | "1984" |
| 1023 | ); |
| 1024 | Ok(()) |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | mod string_filters { |
| 1029 | use super::*; |
| 1030 | |
| 1031 | #[test] |
| 1032 | fn filters_by_exact_string_match() -> Result<(), JsonPathError> { |
| 1033 | let doc = setup_test_doc(); |
| 1034 | let path = "$.store.books[?(@.title == '1984')].title"; |
| 1035 | let result = evaluate_jsonpath(&doc, path)?; |
| 1036 | assert_eq!(result.len(), 1); |
| 1037 | assert_eq!( |
| 1038 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1039 | "1984" |
| 1040 | ); |
| 1041 | Ok(()) |
| 1042 | } |
| 1043 | |
| 1044 | #[test] |
| 1045 | fn filters_by_string_contains() -> Result<(), JsonPathError> { |
| 1046 | let doc = setup_test_doc(); |
| 1047 | let path = "$.store.books[?(@.title contains 'Farm')].title"; |
| 1048 | let result = evaluate_jsonpath(&doc, path)?; |
| 1049 | assert_eq!(result.len(), 1); |
| 1050 | assert_eq!( |
| 1051 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1052 | "Animal Farm" |
| 1053 | ); |
| 1054 | Ok(()) |
| 1055 | } |
| 1056 | |
| 1057 | #[test] |
| 1058 | fn filters_by_recursive_string_match() -> Result<(), JsonPathError> { |
| 1059 | let doc = setup_test_doc(); |
| 1060 | let path = "$..[?(@.author contains 'Orwell')].title"; |
| 1061 | let result = evaluate_jsonpath(&doc, path)?; |
| 1062 | assert_eq!(result.len(), 2); |
| 1063 | let mut titles: Vec<&str> = result |
| 1064 | .iter() |
| 1065 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1066 | .collect(); |
| 1067 | titles.sort(); |
| 1068 | let mut expected = vec!["1984", "Animal Farm"]; |
| 1069 | expected.sort(); |
| 1070 | assert_eq!(titles, expected); |
| 1071 | Ok(()) |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | mod logical_operators { |
| 1076 | use super::*; |
| 1077 | |
| 1078 | #[test] |
| 1079 | fn filters_with_and_operator() -> Result<(), JsonPathError> { |
| 1080 | let doc = setup_test_doc(); |
| 1081 | let path = "$.store.books[?(@.author == 'George Orwell' && @.price < 10)].title"; |
| 1082 | let result = evaluate_jsonpath(&doc, path)?; |
| 1083 | assert_eq!(result.len(), 1); |
| 1084 | assert_eq!( |
| 1085 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1086 | "Animal Farm" |
| 1087 | ); |
| 1088 | Ok(()) |
| 1089 | } |
| 1090 | |
| 1091 | #[test] |
| 1092 | fn filters_with_or_operator() -> Result<(), JsonPathError> { |
| 1093 | let doc = setup_test_doc(); |
| 1094 | let path = "$.store.books[?(@.author == 'George Orwell' || @.price >= 10)].title"; |
| 1095 | let result = evaluate_jsonpath(&doc, path)?; |
| 1096 | assert_eq!(result.len(), 6); |
| 1097 | let mut titles: Vec<&str> = result |
| 1098 | .iter() |
| 1099 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1100 | .collect(); |
| 1101 | titles.sort(); |
| 1102 | let mut expected = vec![ |
| 1103 | "1984", |
| 1104 | "Animal Farm", |
| 1105 | "Brave New World", |
| 1106 | "To Kill a Mockingbird", |
| 1107 | "The Catcher in the Rye", |
| 1108 | "The Hobbit", |
| 1109 | ]; |
| 1110 | expected.sort(); |
| 1111 | assert_eq!(titles, expected); |
| 1112 | Ok(()) |
| 1113 | } |
| 1114 | |
| 1115 | #[test] |
| 1116 | fn filters_with_complex_and_or_combination() -> Result<(), JsonPathError> { |
| 1117 | let doc = setup_test_doc(); |
| 1118 | let path = "$.store.books[?(@.author == 'George Orwell' && (@.price < 10 || @.available == true))].title"; |
| 1119 | let result = evaluate_jsonpath(&doc, path)?; |
| 1120 | assert_eq!(result.len(), 2); |
| 1121 | let mut titles: Vec<&str> = result |
| 1122 | .iter() |
| 1123 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1124 | .collect(); |
| 1125 | titles.sort(); |
| 1126 | let mut expected = vec!["1984", "Animal Farm"]; |
| 1127 | expected.sort(); |
| 1128 | assert_eq!(titles, expected); |
| 1129 | Ok(()) |
| 1130 | } |
| 1131 | |
| 1132 | #[test] |
| 1133 | fn filters_with_not_operator() -> Result<(), JsonPathError> { |
| 1134 | let doc = setup_test_doc(); |
| 1135 | let path = "$.store.books[?(!(@.available == false))].title"; |
| 1136 | let result = evaluate_jsonpath(&doc, path)?; |
| 1137 | assert_eq!(result.len(), 8); |
| 1138 | let mut titles: Vec<&str> = result |
| 1139 | .iter() |
| 1140 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1141 | .collect(); |
| 1142 | titles.sort(); |
| 1143 | let mut expected = vec![ |
| 1144 | "1984", |
| 1145 | "Animal Farm", |
| 1146 | "Fahrenheit 451", |
| 1147 | "The Great Gatsby", |
| 1148 | "To Kill a Mockingbird", |
| 1149 | "Lord of the Flies", |
| 1150 | "Pride and Prejudice", |
| 1151 | "The Hobbit", |
| 1152 | ]; |
| 1153 | expected.sort(); |
| 1154 | assert_eq!(titles, expected); |
| 1155 | Ok(()) |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | mod in_operator { |
| 1160 | use super::*; |
| 1161 | |
| 1162 | #[test] |
| 1163 | fn filters_by_author_in_list() -> Result<(), JsonPathError> { |
| 1164 | let doc = setup_test_doc(); |
| 1165 | let path = "$.store.books[?(@.author in ['George Orwell', 'Jane Austen'])].title"; |
| 1166 | let result = evaluate_jsonpath(&doc, path)?; |
| 1167 | assert_eq!(result.len(), 3); |
| 1168 | let mut titles: Vec<&str> = result |
| 1169 | .iter() |
| 1170 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1171 | .collect(); |
| 1172 | titles.sort(); |
| 1173 | let mut expected = vec!["1984", "Animal Farm", "Pride and Prejudice"]; |
| 1174 | expected.sort(); |
| 1175 | assert_eq!(titles, expected); |
| 1176 | Ok(()) |
| 1177 | } |
| 1178 | |
| 1179 | #[test] |
| 1180 | fn filters_by_price_in_list() -> Result<(), JsonPathError> { |
| 1181 | let doc = setup_test_doc(); |
| 1182 | let path = "$.store.books[?(@.price in [7, 10, 14])].title"; |
| 1183 | let result = evaluate_jsonpath(&doc, path)?; |
| 1184 | assert_eq!(result.len(), 4); |
| 1185 | let mut titles: Vec<&str> = result |
| 1186 | .iter() |
| 1187 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1188 | .collect(); |
| 1189 | titles.sort(); |
| 1190 | let mut expected = vec![ |
| 1191 | "1984", |
| 1192 | "Pride and Prejudice", |
| 1193 | "The Catcher in the Rye", |
| 1194 | "The Hobbit", |
| 1195 | ]; |
| 1196 | expected.sort(); |
| 1197 | assert_eq!(titles, expected); |
| 1198 | Ok(()) |
| 1199 | } |
| 1200 | |
| 1201 | #[test] |
| 1202 | fn filters_with_in_operator_and_null_values() -> Result<(), JsonPathError> { |
| 1203 | let doc = setup_test_doc(); |
| 1204 | let path = "$.store.books[?(@.price in [null, 9])].title"; |
| 1205 | let result = evaluate_jsonpath(&doc, path)?; |
| 1206 | assert_eq!(result.len(), 3); |
| 1207 | let mut titles: Vec<&str> = result |
| 1208 | .iter() |
| 1209 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1210 | .collect(); |
| 1211 | titles.sort(); |
| 1212 | let mut expected = vec!["Fahrenheit 451", "Lord of the Flies", "The Great Gatsby"]; |
| 1213 | expected.sort(); |
| 1214 | assert_eq!(titles, expected); |
| 1215 | Ok(()) |
| 1216 | } |
| 1217 | |
| 1218 | #[test] |
| 1219 | fn filters_with_in_operator_in_recursive_descent() -> Result<(), JsonPathError> { |
| 1220 | let doc = setup_test_doc(); |
| 1221 | let path = "$..[?(@.author in ['George Orwell', 'Ray Bradbury'])].title"; |
| 1222 | let result = evaluate_jsonpath(&doc, path)?; |
| 1223 | assert_eq!(result.len(), 3); |
| 1224 | let mut titles: Vec<&str> = result |
| 1225 | .iter() |
| 1226 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1227 | .collect(); |
| 1228 | titles.sort(); |
| 1229 | let mut expected = vec!["1984", "Animal Farm", "Fahrenheit 451"]; |
| 1230 | expected.sort(); |
| 1231 | assert_eq!(titles, expected); |
| 1232 | Ok(()) |
| 1233 | } |
| 1234 | |
| 1235 | #[test] |
| 1236 | fn filters_with_root_list_in() -> Result<(), JsonPathError> { |
| 1237 | let doc = setup_test_doc(); |
| 1238 | let path = "$.store.books[?(@.author in $.store.featured_authors)].title"; |
| 1239 | let result = evaluate_jsonpath(&doc, path)?; |
| 1240 | assert_eq!(result.len(), 4); |
| 1241 | let mut titles: Vec<&str> = result |
| 1242 | .iter() |
| 1243 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1244 | .collect(); |
| 1245 | titles.sort(); |
| 1246 | let mut expected = vec!["1984", "Animal Farm", "Brave New World", "Fahrenheit 451"]; |
| 1247 | expected.sort(); |
| 1248 | assert_eq!(titles, expected); |
| 1249 | Ok(()) |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | mod union_and_slice_operations { |
| 1254 | use super::*; |
| 1255 | |
| 1256 | #[test] |
| 1257 | fn handles_union_indexes() -> Result<(), JsonPathError> { |
| 1258 | let doc = setup_test_doc(); |
| 1259 | let path = "$.store.books[0,2].title"; |
| 1260 | let result = evaluate_jsonpath(&doc, path)?; |
| 1261 | assert_eq!(result.len(), 2); |
| 1262 | assert_eq!( |
| 1263 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1264 | "1984" |
| 1265 | ); |
| 1266 | assert_eq!( |
| 1267 | &**result[1].as_value().unwrap().as_string().unwrap(), |
| 1268 | "Brave New World" |
| 1269 | ); |
| 1270 | Ok(()) |
| 1271 | } |
| 1272 | |
| 1273 | #[test] |
| 1274 | fn handles_union_keys() -> Result<(), JsonPathError> { |
| 1275 | let doc = setup_test_doc(); |
| 1276 | let path = "$.store.books[0]['title','author']"; |
| 1277 | let result = evaluate_jsonpath(&doc, path)?; |
| 1278 | assert_eq!(result.len(), 2); |
| 1279 | assert_eq!( |
| 1280 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1281 | "1984" |
| 1282 | ); |
| 1283 | assert_eq!( |
| 1284 | &**result[1].as_value().unwrap().as_string().unwrap(), |
| 1285 | "George Orwell" |
| 1286 | ); |
| 1287 | Ok(()) |
| 1288 | } |
| 1289 | |
| 1290 | #[test] |
| 1291 | fn handles_union_with_negative_indexes() -> Result<(), JsonPathError> { |
| 1292 | let doc = setup_test_doc(); |
| 1293 | let path = "$.store.books[-2,-1].title"; |
| 1294 | let result = evaluate_jsonpath(&doc, path)?; |
| 1295 | assert_eq!(result.len(), 2); |
| 1296 | assert_eq!( |
| 1297 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1298 | "Pride and Prejudice" |
| 1299 | ); |
| 1300 | assert_eq!( |
| 1301 | &**result[1].as_value().unwrap().as_string().unwrap(), |
| 1302 | "The Hobbit" |
| 1303 | ); |
| 1304 | Ok(()) |
| 1305 | } |
| 1306 | |
| 1307 | #[test] |
| 1308 | fn handles_basic_slice() -> Result<(), JsonPathError> { |
| 1309 | let doc = setup_test_doc(); |
| 1310 | let path = "$.store.books[0:3].title"; |
| 1311 | let result = evaluate_jsonpath(&doc, path)?; |
| 1312 | assert_eq!(result.len(), 3); |
| 1313 | let titles: Vec<&str> = result |
| 1314 | .iter() |
| 1315 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1316 | .collect(); |
| 1317 | assert_eq!(titles, vec!["1984", "Animal Farm", "Brave New World"]); |
| 1318 | Ok(()) |
| 1319 | } |
| 1320 | |
| 1321 | #[test] |
| 1322 | fn handles_slice_with_step() -> Result<(), JsonPathError> { |
| 1323 | let doc = setup_test_doc(); |
| 1324 | let path = "$.store.books[0:5:2].title"; |
| 1325 | let result = evaluate_jsonpath(&doc, path)?; |
| 1326 | assert_eq!(result.len(), 3); |
| 1327 | let titles: Vec<&str> = result |
| 1328 | .iter() |
| 1329 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1330 | .collect(); |
| 1331 | assert_eq!(titles, vec!["1984", "Brave New World", "The Great Gatsby"]); |
| 1332 | Ok(()) |
| 1333 | } |
| 1334 | |
| 1335 | #[test] |
| 1336 | fn handles_negative_slice() -> Result<(), JsonPathError> { |
| 1337 | let doc = setup_test_doc(); |
| 1338 | let path = "$.store.books[-2:].title"; |
| 1339 | let result = evaluate_jsonpath(&doc, path)?; |
| 1340 | assert_eq!(result.len(), 2); |
| 1341 | let titles: Vec<&str> = result |
| 1342 | .iter() |
| 1343 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1344 | .collect(); |
| 1345 | assert_eq!(titles, vec!["Pride and Prejudice", "The Hobbit"]); |
| 1346 | Ok(()) |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | mod complex_and_recursive_filters { |
| 1351 | use super::*; |
| 1352 | |
| 1353 | #[test] |
| 1354 | fn filters_with_multiple_conditions() -> Result<(), JsonPathError> { |
| 1355 | let doc = setup_test_doc(); |
| 1356 | let path = "$.store.books[?(@.price >= 10 && @.available == true && @.title contains '1984')].title"; |
| 1357 | let result = evaluate_jsonpath(&doc, path)?; |
| 1358 | assert_eq!(result.len(), 1); |
| 1359 | assert_eq!( |
| 1360 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1361 | "1984" |
| 1362 | ); |
| 1363 | Ok(()) |
| 1364 | } |
| 1365 | |
| 1366 | #[test] |
| 1367 | fn filters_with_path_expressions() -> Result<(), JsonPathError> { |
| 1368 | let doc = setup_test_doc(); |
| 1369 | let path = "$.store.books[?(@.author == 'George Orwell' && @.title != '1984')].title"; |
| 1370 | let result = evaluate_jsonpath(&doc, path)?; |
| 1371 | assert_eq!(result.len(), 1); |
| 1372 | assert_eq!( |
| 1373 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1374 | "Animal Farm" |
| 1375 | ); |
| 1376 | Ok(()) |
| 1377 | } |
| 1378 | |
| 1379 | #[test] |
| 1380 | fn filters_with_null_checks() -> Result<(), JsonPathError> { |
| 1381 | let doc = setup_test_doc(); |
| 1382 | let path = "$.store.books[?(@.price == null || @.price < 10)].title"; |
| 1383 | let result = evaluate_jsonpath(&doc, path)?; |
| 1384 | assert_eq!(result.len(), 5); |
| 1385 | let mut titles: Vec<&str> = result |
| 1386 | .iter() |
| 1387 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1388 | .collect(); |
| 1389 | titles.sort(); |
| 1390 | let mut expected = vec![ |
| 1391 | "Animal Farm", |
| 1392 | "Fahrenheit 451", |
| 1393 | "The Great Gatsby", |
| 1394 | "Pride and Prejudice", |
| 1395 | "Lord of the Flies", |
| 1396 | ]; |
| 1397 | expected.sort(); |
| 1398 | assert_eq!(titles, expected); |
| 1399 | Ok(()) |
| 1400 | } |
| 1401 | |
| 1402 | #[test] |
| 1403 | fn handles_recursive_filter_with_price_condition() -> Result<(), JsonPathError> { |
| 1404 | let doc = setup_test_doc(); |
| 1405 | let path = "$..[?(@.price > 10)].title"; |
| 1406 | let result = evaluate_jsonpath(&doc, path)?; |
| 1407 | assert_eq!(result.len(), 3); |
| 1408 | let mut titles: Vec<&str> = result |
| 1409 | .iter() |
| 1410 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1411 | .collect(); |
| 1412 | titles.sort(); |
| 1413 | let mut expected = vec!["Brave New World", "To Kill a Mockingbird", "The Hobbit"]; |
| 1414 | expected.sort(); |
| 1415 | assert_eq!(titles, expected); |
| 1416 | Ok(()) |
| 1417 | } |
| 1418 | |
| 1419 | #[test] |
| 1420 | fn handles_recursive_filter_with_logical_operators() -> Result<(), JsonPathError> { |
| 1421 | let doc = setup_test_doc(); |
| 1422 | let path = "$..[?(@.author == 'George Orwell' || @.price > 10)].title"; |
| 1423 | let result = evaluate_jsonpath(&doc, path)?; |
| 1424 | assert_eq!(result.len(), 5); |
| 1425 | let mut titles: Vec<&str> = result |
| 1426 | .iter() |
| 1427 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1428 | .collect(); |
| 1429 | titles.sort(); |
| 1430 | let mut expected = vec![ |
| 1431 | "1984", |
| 1432 | "Animal Farm", |
| 1433 | "Brave New World", |
| 1434 | "To Kill a Mockingbird", |
| 1435 | "The Hobbit", |
| 1436 | ]; |
| 1437 | expected.sort(); |
| 1438 | assert_eq!(titles, expected); |
| 1439 | Ok(()) |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | mod edge_cases_and_error_handling { |
| 1444 | use super::*; |
| 1445 | |
| 1446 | #[test] |
| 1447 | fn handles_quoted_keys_with_special_characters() -> Result<(), JsonPathError> { |
| 1448 | let doc = LoroDoc::new(); |
| 1449 | doc.start_auto_commit(); |
| 1450 | let map = doc.get_map("store"); |
| 1451 | let book = map |
| 1452 | .insert_container("book-with-dash", MapHandler::new_detached()) |
| 1453 | .unwrap(); |
| 1454 | book.insert("price-$10", "cheap").unwrap(); |
| 1455 | let path = "$['store']['book-with-dash']['price-$10']"; |
| 1456 | let result = evaluate_jsonpath(&doc, path)?; |
| 1457 | assert_eq!(result.len(), 1); |
| 1458 | assert_eq!( |
| 1459 | &**result[0].as_value().unwrap().as_string().unwrap(), |
| 1460 | "cheap" |
| 1461 | ); |
| 1462 | Ok(()) |
| 1463 | } |
| 1464 | |
| 1465 | #[test] |
| 1466 | fn handles_quoted_keys_with_escaped_quotes() -> Result<(), JsonPathError> { |
| 1467 | let doc = setup_test_doc(); |
| 1468 | let path = "$.store.books[?(@.author == 'George Orwell')].title"; |
| 1469 | let result = evaluate_jsonpath(&doc, path)?; |
| 1470 | assert_eq!(result.len(), 2); |
| 1471 | let mut titles: Vec<&str> = result |
| 1472 | .iter() |
| 1473 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1474 | .collect(); |
| 1475 | titles.sort(); |
| 1476 | let mut expected = vec!["1984", "Animal Farm"]; |
| 1477 | expected.sort(); |
| 1478 | assert_eq!(titles, expected); |
| 1479 | Ok(()) |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | mod root_filters { |
| 1484 | use super::*; |
| 1485 | |
| 1486 | #[test] |
| 1487 | fn filters_with_root_reference() -> Result<(), JsonPathError> { |
| 1488 | let doc = setup_test_doc(); |
| 1489 | let path = "$.store.books[?(@.author == $.store.featured_author)].title"; |
| 1490 | let result = evaluate_jsonpath(&doc, path)?; |
| 1491 | assert_eq!(result.len(), 2); |
| 1492 | let mut titles: Vec<&str> = result |
| 1493 | .iter() |
| 1494 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1495 | .collect(); |
| 1496 | titles.sort(); |
| 1497 | let mut expected = vec!["1984", "Animal Farm"]; |
| 1498 | expected.sort(); |
| 1499 | assert_eq!(titles, expected); |
| 1500 | Ok(()) |
| 1501 | } |
| 1502 | |
| 1503 | #[test] |
| 1504 | fn filters_with_root_numerical_comparison() -> Result<(), JsonPathError> { |
| 1505 | let doc = setup_test_doc(); |
| 1506 | let path = "$.store.books[?(@.price > $.store.min_price)].title"; |
| 1507 | let result = evaluate_jsonpath(&doc, path)?; |
| 1508 | assert_eq!(result.len(), 3); |
| 1509 | let mut titles: Vec<&str> = result |
| 1510 | .iter() |
| 1511 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1512 | .collect(); |
| 1513 | titles.sort(); |
| 1514 | let mut expected = vec!["Brave New World", "The Hobbit", "To Kill a Mockingbird"]; |
| 1515 | expected.sort(); |
| 1516 | assert_eq!(titles, expected); |
| 1517 | Ok(()) |
| 1518 | } |
| 1519 | |
| 1520 | #[test] |
| 1521 | fn filters_with_root_not_equal() -> Result<(), JsonPathError> { |
| 1522 | let doc = setup_test_doc(); |
| 1523 | let path = "$.store.books[?(@.author != $.store.featured_author)].title"; |
| 1524 | let result = evaluate_jsonpath(&doc, path)?; |
| 1525 | assert_eq!(result.len(), 8); |
| 1526 | let mut titles: Vec<&str> = result |
| 1527 | .iter() |
| 1528 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1529 | .collect(); |
| 1530 | titles.sort(); |
| 1531 | let mut expected = vec![ |
| 1532 | "Brave New World", |
| 1533 | "Fahrenheit 451", |
| 1534 | "The Great Gatsby", |
| 1535 | "To Kill a Mockingbird", |
| 1536 | "The Catcher in the Rye", |
| 1537 | "Lord of the Flies", |
| 1538 | "Pride and Prejudice", |
| 1539 | "The Hobbit", |
| 1540 | ]; |
| 1541 | expected.sort(); |
| 1542 | assert_eq!(titles, expected); |
| 1543 | Ok(()) |
| 1544 | } |
| 1545 | |
| 1546 | #[test] |
| 1547 | fn filters_with_root_complex() -> Result<(), JsonPathError> { |
| 1548 | let doc = setup_test_doc(); |
| 1549 | let path = "$.store.books[?(@.author == $.store.featured_author && @.price <= $.store.min_price)].title"; |
| 1550 | let result = evaluate_jsonpath(&doc, path)?; |
| 1551 | assert_eq!(result.len(), 2); |
| 1552 | let mut titles: Vec<&str> = result |
| 1553 | .iter() |
| 1554 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1555 | .collect(); |
| 1556 | titles.sort(); |
| 1557 | let mut expected = vec!["1984", "Animal Farm"]; |
| 1558 | expected.sort(); |
| 1559 | assert_eq!(titles, expected); |
| 1560 | Ok(()) |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | mod existence_filters { |
| 1565 | use super::*; |
| 1566 | #[test] |
| 1567 | fn filters_by_existence_of_isbn() -> Result<(), JsonPathError> { |
| 1568 | let doc = setup_test_doc(); |
| 1569 | let path = "$.store.books[?(@.isbn)].title"; |
| 1570 | let result = evaluate_jsonpath(&doc, path)?; |
| 1571 | assert_eq!(result.len(), 3); |
| 1572 | let mut titles: Vec<&str> = result |
| 1573 | .iter() |
| 1574 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1575 | .collect(); |
| 1576 | titles.sort(); |
| 1577 | let mut expected = vec!["1984", "Brave New World", "Fahrenheit 451"]; |
| 1578 | expected.sort(); |
| 1579 | assert_eq!(titles, expected); |
| 1580 | Ok(()) |
| 1581 | } |
| 1582 | |
| 1583 | #[test] |
| 1584 | fn filters_by_non_existence_of_isbn() -> Result<(), JsonPathError> { |
| 1585 | let doc = setup_test_doc(); |
| 1586 | let path = "$.store.books[?(!@.isbn)].title"; |
| 1587 | let result = evaluate_jsonpath(&doc, path)?; |
| 1588 | assert_eq!(result.len(), 7); |
| 1589 | let mut titles: Vec<&str> = result |
| 1590 | .iter() |
| 1591 | .map(|v| v.as_value().unwrap().as_string().unwrap().as_str()) |
| 1592 | .collect(); |
| 1593 | titles.sort(); |
| 1594 | let mut expected = vec![ |
| 1595 | "Animal Farm", |
| 1596 | "The Great Gatsby", |
| 1597 | "To Kill a Mockingbird", |
| 1598 | "The Catcher in the Rye", |
| 1599 | "Lord of the Flies", |
| 1600 | "Pride and Prejudice", |
| 1601 | "The Hobbit", |
| 1602 | ]; |
| 1603 | expected.sort(); |
| 1604 | assert_eq!(titles, expected); |
| 1605 | Ok(()) |
| 1606 | } |
| 1607 | |
| 1608 | #[test] |
| 1609 | fn filters_by_existence_of_price() -> Result<(), JsonPathError> { |
| 1610 | let doc = setup_test_doc(); |
| 1611 | let path = "$.store.books[?(@.price)].title"; |
| 1612 | let result = evaluate_jsonpath(&doc, path)?; |
| 1613 | assert_eq!(result.len(), 10); |
| 1614 | Ok(()) |
| 1615 | } |
| 1616 | |
| 1617 | #[test] |
| 1618 | fn filters_by_non_existence_of_price() -> Result<(), JsonPathError> { |
| 1619 | let doc = setup_test_doc(); |
| 1620 | let path = "$.store.books[?(!@.price)].title"; |
| 1621 | let result = evaluate_jsonpath(&doc, path)?; |
| 1622 | assert_eq!(result.len(), 0); |
| 1623 | Ok(()) |
| 1624 | } |
| 1625 | |
| 1626 | #[test] |
| 1627 | fn filters_by_non_existence_of_root_path() -> Result<(), JsonPathError> { |
| 1628 | let doc = setup_test_doc(); |
| 1629 | let path = "$.store.books[?($.store.nonexistent)].title"; |
| 1630 | let result = evaluate_jsonpath(&doc, path)?; |
| 1631 | assert_eq!(result.len(), 0); |
| 1632 | Ok(()) |
| 1633 | } |
| 1634 | } |
| 1635 | } |