Skip to content

Commit 80ca0cd

Browse files
committed
jmespath function binary arg
1 parent 1fd217a commit 80ca0cd

File tree

2 files changed

+85
-35
lines changed

2 files changed

+85
-35
lines changed

include/jsoncons_ext/jmespath/jmespath.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4004,6 +4004,8 @@ namespace detail {
40044004
case expr_state::argument:
40054005
push_token(argument_arg, resources, output_stack, ec);
40064006
if (JSONCONS_UNLIKELY(ec)) {return jmespath_expression{};}
4007+
push_token(lparen_arg, resources, output_stack, ec);
4008+
if (JSONCONS_UNLIKELY(ec)) {return jmespath_expression{};}
40074009
state_stack.pop_back();
40084010
break;
40094011

@@ -5371,10 +5373,13 @@ namespace detail {
53715373
break;
53725374
case token_kind::key:
53735375
case token_kind::pipe:
5374-
case token_kind::argument:
53755376
case token_kind::begin_expression_type:
53765377
output_stack.push_back(std::move(tok));
53775378
break;
5379+
case token_kind::argument:
5380+
unwind_rparen(output_stack, ec);
5381+
output_stack.push_back(std::move(tok));
5382+
break;
53785383
case token_kind::lparen:
53795384
operator_stack_.emplace_back(std::move(tok));
53805385
break;

test/jmespath/src/jmespath_expression_tests.cpp

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,48 +79,93 @@ TEST_CASE("jmespath_expression tests")
7979

8080
TEST_CASE("jmespath issue")
8181
{
82-
std::string jtext = R"(
82+
SECTION("issue 1")
8383
{
84-
"locations": [
85-
{"name": "Seattle", "state": "WA"},
86-
{"name": "New York", "state": "NY"},
87-
{"name": "Bellevue", "state": "WA"},
88-
{"name": "Olympia", "state": "WA"}
89-
]
90-
}
91-
)";
92-
93-
std::string expr = R"(
94-
{
95-
name: locations[].name,
96-
state: locations[].state
84+
std::string jtext = R"(
85+
{
86+
"locations": [
87+
{"name": "Seattle", "state": "WA"},
88+
{"name": "New York", "state": "NY"},
89+
{"name": "Bellevue", "state": "WA"},
90+
{"name": "Olympia", "state": "WA"}
91+
]
92+
}
93+
)";
94+
95+
std::string expr = R"(
96+
{
97+
name: locations[].name,
98+
state: locations[].state
99+
}
100+
)";
101+
102+
auto doc = ojson::parse(jtext);
103+
104+
auto result = jmespath::search(doc, expr);
105+
106+
//std::cout << pretty_print(result) << "\n\n";
97107
}
98-
)";
108+
SECTION("parentheses issue")
109+
{
110+
auto doc = jsoncons::json::parse(R"(
111+
{"foo" : [[0, 1], [2, 3], [4, 5]]}
112+
)");
99113

100-
auto doc = ojson::parse(jtext);
114+
auto expected = jsoncons::json::parse(R"([0, 1])");
101115

102-
auto result = jmespath::search(doc, expr);
116+
std::string query = R"((foo[*])[0])";
117+
auto expr = jmespath::make_expression<jsoncons::json>(query);
103118

104-
//std::cout << pretty_print(result) << "\n\n";
119+
jsoncons::json result = expr.evaluate(doc);
120+
//std::cout << pretty_print(result) << "\n";
121+
CHECK(expected == result);
122+
}
105123
}
106124

107-
TEST_CASE("jmespath parentheses issue")
125+
TEST_CASE("jmespath issue 605")
108126
{
109-
auto doc = jsoncons::json::parse(R"(
110-
{"foo" : [[0, 1], [2, 3], [4, 5]]}
111-
)");
112-
113-
SECTION("test 1")
114-
{
115-
auto expected = jsoncons::json::parse(R"(
116-
[0, 1]
117-
)");
127+
SECTION("function with 1 arg")
128+
{
129+
std::string query = R"(
130+
to_array("gw:GWallInfo"."gw:DocumentStatistics"."gw:ContentGroups"."gw:ContentGroup" ||
131+
"gw:DocumentStatistics"."gw:ContentGroups"."gw:ContentGroup")
132+
)";
133+
134+
auto expr = jsoncons::jmespath::make_expression<json>(query);
135+
json j;
136+
j["gw:DocumentStatistics"]["gw:ContentGroups"]["gw:ContentGroup"] = 9;
137+
auto result = expr.evaluate(j);
138+
REQUIRE(result.is_array());
139+
REQUIRE_FALSE(result.empty());
140+
CHECK(9 == result[0]);
141+
142+
//std::cout << pretty_print(result) << "\n\n";
143+
}
144+
SECTION("function with 2 args")
145+
{
146+
std::string query = R"(starts_with(B || A,null || 'a'))";
118147

119-
std::string query = R"((foo[*])[0])";
120-
auto expr = jmespath::make_expression<jsoncons::json>(query);
148+
auto expr = jsoncons::jmespath::make_expression<json>(query);
149+
json j;
150+
j["A"] = "ab";
151+
//auto result = jsoncons::jmespath::search(j, expr);
152+
auto result = expr.evaluate(j);
153+
//std::cout << result << "\n";
121154

122-
jsoncons::json result = expr.evaluate(doc);
123-
//std::cout << pretty_print(result) << "\n";
124-
CHECK(expected == result);
125-
}
155+
CHECK(result.as<bool>());
156+
}
157+
SECTION("function with 2 args (2)")
158+
{
159+
std::string query = R"(starts_with(A || B,null || 'a'))";
160+
161+
auto expr = jsoncons::jmespath::make_expression<json>(query);
162+
json j;
163+
j["A"] = "ab";
164+
//auto result = jsoncons::jmespath::search(j, expr);
165+
auto result = expr.evaluate(j);
166+
//std::cout << result << "\n";
167+
168+
CHECK(result.as<bool>());
169+
}
126170
}
171+

0 commit comments

Comments
 (0)