Skip to content

Commit b05f886

Browse files
authored
JMESPath slices and pre-computed runtime expressions (#1510)
* More JMESPath unit tests * move detail::from_chars to parse.hpp * Supporting slices * slicing working * run-time slice working * multi-bracket access * jmespath_expression * Update JMESPath.md
1 parent d0e24b7 commit b05f886

File tree

4 files changed

+798
-288
lines changed

4 files changed

+798
-288
lines changed

docs/JMESPath.md

+74
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,77 @@ suite jmespath_read_at_tests = [] {
7272
};
7373
```
7474

75+
## Run-time Expressions
76+
77+
It can be expensive to tokenize at runtime. The runtime version of `glz::read_jmespath` takes in a `const glz::jmespath_expression&`. This allows expressions to be pre-computed, reused, and cached for better runtime performance.
78+
79+
```c++
80+
Person child{};
81+
// A runtime expression can be pre-computed and saved for more efficient lookups
82+
glz::jmespath_expression expression{"family.children[0]"};
83+
expect(not glz::read_jmespath(expression, child, buffer));
84+
expect(child.first_name == "Lilly");
85+
```
86+
87+
Note that this still works:
88+
89+
```c++
90+
expect(not glz::read_jmespath("family.children[0]", child, buffer));
91+
```
92+
93+
## Slices
94+
95+
```c++
96+
suite jmespath_slice_tests = [] {
97+
"slice compile-time"_test = [] {
98+
std::vector<int> data{0,1,2,3,4,5,6,7,8,9};
99+
std::string buffer{};
100+
expect(not glz::write_json(data, buffer));
101+
102+
std::vector<int> slice{};
103+
expect(not glz::read_jmespath<"[0:5]">(slice, buffer));
104+
expect(slice.size() == 5);
105+
expect(slice[0] == 0);
106+
expect(slice[1] == 1);
107+
expect(slice[2] == 2);
108+
expect(slice[3] == 3);
109+
expect(slice[4] == 4);
110+
};
111+
112+
"slice run-time"_test = [] {
113+
std::vector<int> data{0,1,2,3,4,5,6,7,8,9};
114+
std::string buffer{};
115+
expect(not glz::write_json(data, buffer));
116+
117+
std::vector<int> slice{};
118+
expect(not glz::read_jmespath("[0:5]", slice, buffer));
119+
expect(slice.size() == 5);
120+
expect(slice[0] == 0);
121+
expect(slice[1] == 1);
122+
expect(slice[2] == 2);
123+
expect(slice[3] == 3);
124+
expect(slice[4] == 4);
125+
};
126+
127+
"slice compile-time multi-bracket"_test = [] {
128+
std::vector<std::vector<int>> data{{1,2},{3,4,5},{6,7}};
129+
std::string buffer{};
130+
expect(not glz::write_json(data, buffer));
131+
132+
int v{};
133+
expect(not glz::read_jmespath<"[1][2]">(v, buffer));
134+
expect(v == 5);
135+
};
136+
137+
"slice run-time multi-bracket"_test = [] {
138+
std::vector<std::vector<int>> data{{1,2},{3,4,5},{6,7}};
139+
std::string buffer{};
140+
expect(not glz::write_json(data, buffer));
141+
142+
int v{};
143+
expect(not glz::read_jmespath("[1][2]", v, buffer));
144+
expect(v == 5);
145+
};
146+
};
147+
```
148+

0 commit comments

Comments
 (0)