forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

jsonpath_value.rs172 lines, 5.3 KiBHistoryRawDownload
1#![cfg(feature = "jsonpath")]
2
3use loro_internal::{
4 handler::ValueOrHandler, jsonpath::jsonpath_impl::evaluate_jsonpath, loro_value, HandlerTrait,
5 LoroValue, ToJson,
6};
7use pretty_assertions::assert_eq;
8use serde_json::{json, Value};
9use std::error::Error;
10
11fn query_json(root: &LoroValue, path: &str) -> Result<Value, Box<dyn Error>> {
12 Ok(Value::Array(
13 evaluate_jsonpath(root, path)?
14 .into_iter()
15 .map(|value| match value {
16 ValueOrHandler::Value(value) => value.to_json_value(),
17 ValueOrHandler::Handler(handler) => handler.get_deep_value().to_json_value(),
18 })
19 .collect(),
20 ))
21}
22
23fn root_value() -> LoroValue {
24 loro_value!({
25 "store": {
26 "books": [
27 {
28 "title": "1984",
29 "author": "George Orwell",
30 "price": 10,
31 "rating": 4.5,
32 "available": true,
33 "tags": ["classic", "dystopia"],
34 "meta": {"pages": 328, "edition": "paperback"},
35 "note": null,
36 "zero": 0,
37 "empty": "",
38 "empty_list": [],
39 "empty_map": {}
40 },
41 {
42 "title": "Animal Farm",
43 "author": "George Orwell",
44 "price": 6,
45 "rating": 4.0,
46 "available": true,
47 "tags": ["classic"],
48 "meta": {"pages": 112},
49 "note": "satire",
50 "zero": 0,
51 "empty": "",
52 "empty_list": [],
53 "empty_map": {}
54 },
55 {
56 "title": "Brave New World",
57 "author": "Aldous Huxley",
58 "price": 12,
59 "rating": 4.2,
60 "available": false,
61 "tags": [],
62 "meta": {},
63 "note": null,
64 "zero": 0,
65 "empty": "",
66 "empty_list": [],
67 "empty_map": {}
68 }
69 ],
70 "featured_authors": ["George Orwell"],
71 "min_price": 9,
72 "empty": "",
73 "truth": true,
74 "nothing": null
75 },
76 "numbers": [0, 1, 2, 3]
77 })
78}
79
80#[test]
81fn jsonpath_evaluates_raw_loro_values_with_comparisons_and_truthiness() -> Result<(), Box<dyn Error>>
82{
83 let root = root_value();
84
85 assert_eq!(query_json(&root, "$")?, json!([root.to_json_value()]));
86 assert_eq!(
87 query_json(&root, "$.store.books[-1].title")?,
88 json!(["Brave New World"])
89 );
90 assert_eq!(query_json(&root, "$.store.books[-99]")?, json!([]));
91
92 assert_eq!(
93 query_json(&root, "$.store.books[?(@.price != 10)].title")?,
94 json!(["Animal Farm", "Brave New World"])
95 );
96 assert_eq!(
97 query_json(
98 &root,
99 "$.store.books[?(@.price <= $.store.min_price)].title"
100 )?,
101 json!(["Animal Farm"])
102 );
103 assert_eq!(
104 query_json(&root, "$.store.books[?(@.price > 10)].title")?,
105 json!(["Brave New World"])
106 );
107 assert_eq!(
108 query_json(&root, "$.store.books[?(@.rating == 4.5)].title")?,
109 json!(["1984"])
110 );
111 assert_eq!(
112 query_json(&root, "$.store.books[?(@.rating >= 4.2)].title")?,
113 json!(["1984", "Brave New World"])
114 );
115 assert_eq!(
116 query_json(
117 &root,
118 "$.store.books[?(@.title >= 'Brave New World')].title"
119 )?,
120 json!(["Brave New World"])
121 );
122 assert_eq!(
123 query_json(&root, "$.store.books[?(@.title < 'Brave New World')].title")?,
124 json!(["1984", "Animal Farm"])
125 );
126 assert_eq!(
127 query_json(&root, "$.store.books[?(@.available != false)].title")?,
128 json!(["1984", "Animal Farm"])
129 );
130 assert_eq!(
131 query_json(
132 &root,
133 "$.store.books[?(@.author in $.store.featured_authors)].title"
134 )?,
135 json!(["1984", "Animal Farm"])
136 );
137 assert_eq!(
138 query_json(&root, "$.store.books[?(@.author in 'George Orwell')].title")?,
139 json!([])
140 );
141
142 assert_eq!(
143 query_json(&root, "$.store.books[?(count(@.title) == 1)].title")?,
144 json!(["1984", "Animal Farm", "Brave New World"])
145 );
146 assert!(evaluate_jsonpath(&root, "$.store.books[?(count(1) == 0)].title").is_err());
147 assert_eq!(
148 query_json(&root, "$.store.books[?(length(value(@.tags)) > 0)].title")?,
149 json!(["1984", "Animal Farm"])
150 );
151 assert_eq!(
152 query_json(&root, "$.store.books[?(length(value(@.meta)) >= 2)].title")?,
153 json!(["1984"])
154 );
155 assert_eq!(
156 query_json(&root, "$.store.books[?(length(value(@.price)) == 0)].title")?,
157 json!(["1984", "Animal Farm", "Brave New World"])
158 );
159 assert_eq!(
160 query_json(&root, "$.store.books[?(value(@.tags[*]) == null)].title")?,
161 json!(["1984", "Brave New World"])
162 );
163
164 assert!(evaluate_jsonpath(&root, "$.store.books[?(value(@.title))].title").is_err());
165 assert_eq!(
166 query_json(&root, "$.store.books[?(@.missing)].title")?,
167 json!([])
168 );
169 assert_eq!(query_json(&root, "$.numbers[?(@ >= 2)]")?, json!([2, 3]));
170
171 Ok(())
172}