Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

Commit e3a16cc

Browse files
rogerpearceppete
andauthored
jsonlogic v0.2.0 (#49)
* Switch to jsonlogic v0.2.0 update tests to new API - Adapt tests to the new jsonlogic API: - Pre compile rules with jsonlogic::create_logic to get logic_rule - Use logic_rule::apply with jsonlogic::json_accessor - Replace any_expr / unpack_value<bool> with truthy(any_value) - Simplify lifetime handling by avoiding manual expr ownership --------- Co-authored-by: Peter Pirkelbauer <[email protected]>
1 parent 9f90431 commit e3a16cc

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

CMakeLists.txt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cmake_policy(SET CMP0135 NEW)
1414
endif()
1515

1616
project(CLIPPy
17-
VERSION 0.2
17+
VERSION 0.5
1818
DESCRIPTION "Command Line Interface Plus Python"
1919
LANGUAGES CXX)
2020

@@ -93,25 +93,18 @@ FetchContent_MakeAvailable(Boost)
9393

9494
#
9595
# JSONLogic
96-
# find_package(jsonlogic QUIET)
97-
# if (NOT jsonlogic_FOUND)
98-
# message(STATUS "jsonlogic not found, doing stuff")
99-
set(Boost_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/boost-install) # needed for jsonlogic
100-
101-
FetchContent_Declare(jsonlogic
102-
GIT_REPOSITORY https://github.com/LLNL/jsonlogic.git
103-
GIT_TAG nojson
104-
SOURCE_SUBDIR cpp
105-
)
106-
# set(jsonlogic_INCLUDE_DIR ${jsonlogic_SOURCE_DIR}/cpp/include/jsonlogic)
107-
FetchContent_MakeAvailable(jsonlogic)
108-
message(STATUS "jsonlogic source dir: ${jsonlogic_SOURCE_DIR}")
96+
set(Boost_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/boost-install) # needed for jsonlogic
10997

110-
111-
# else()
112-
# message(STATUS "jsonlogic found, weird")
98+
FetchContent_Declare(jsonlogic
99+
GIT_REPOSITORY https://github.com/LLNL/jsonlogic.git
100+
GIT_TAG v0.2.0
101+
SOURCE_SUBDIR cpp
102+
)
103+
# set(jsonlogic_INCLUDE_DIR ${jsonlogic_SOURCE_DIR}/cpp/include/jsonlogic)
104+
FetchContent_MakeAvailable(jsonlogic)
105+
message(STATUS "jsonlogic source dir: ${jsonlogic_SOURCE_DIR}")
113106

114-
# endif()
107+
115108

116109
### Require out-of-source builds
117110
file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH)

test/TestBag/remove_if.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ int main(int argc, char **argv) {
3232

3333
//
3434
// Expression here
35-
auto apply_jl = [&expression](int value) {
35+
jsonlogic::logic_rule jlrule = jsonlogic::create_logic(expression["rule"]);
36+
37+
auto apply_jl = [&jlrule](int value) {
3638
boostjsn::object data;
3739
data["value"] = value;
38-
jsonlogic::any_expr res = jsonlogic::apply(expression["rule"], data);
39-
return jsonlogic::unpack_value<bool>(res);
40+
return truthy(jlrule.apply(jsonlogic::json_accessor(std::move(data))));
4041
};
4142

4243
the_bag.remove_if(apply_jl);

test/TestGraph/where.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,17 @@ auto parse_where_expression(M& mvmap_, boost::json::object& expression,
1919

2020
// boost::json::object submission_data{};
2121
std::vector<boost::json::string> vars;
22-
jsonlogic::any_expr expression_rule_;
2322

2423
// we use expression_rule_ (and expression_rule; see below) in order to avoid
2524
// having to recompute this every time we call the lambda.
26-
std::tie(expression_rule_, vars, std::ignore) =
27-
jsonlogic::create_logic(exp2["rule"]);
28-
29-
// this works around a deficiency in C++ compilers where
30-
// unique pointers moved into a lambda cannot be moved into
31-
// an std::function.
32-
jsonlogic::expr* rawexpr = expression_rule_.release();
33-
std::shared_ptr<jsonlogic::expr> expression_rule{rawexpr};
25+
jsonlogic::logic_rule jlrule = jsonlogic::create_logic(exp2["rule"]);
3426

3527
std::cerr << "parse_where: # of vars: " << vars.size() << std::endl;
3628
for (const auto& var : vars) {
3729
std::cerr << " apply_jl var dump: var: " << var << std::endl;
3830
}
3931

40-
auto apply_jl = [&expression, vars, expression_rule, &mvmap_,
32+
auto apply_jl = [rule=std::move(jlrule), vars, &mvmap_,
4133
&submission_data](mvmap::locator loc) mutable {
4234
std::cerr << " apply_jl: # of vars: " << vars.size() << std::endl;
4335
for (const auto& var : vars) {
@@ -74,10 +66,9 @@ auto parse_where_expression(M& mvmap_, boost::json::object& expression,
7466
}
7567
std::cerr << " apply_jl: submission_data: " << submission_data
7668
<< std::endl;
77-
jsonlogic::any_expr res = jsonlogic::apply(
78-
*expression_rule, jsonlogic::data_accessor(submission_data));
69+
jsonlogic::any_value res = rule.apply(jsonlogic::json_accessor(submission_data));
7970
std::cerr << " apply_jl: res: " << res << std::endl;
80-
return jsonlogic::unpack_value<bool>(res);
71+
return truthy(res);
8172
};
8273

8374
return apply_jl;
@@ -146,4 +137,4 @@ std::vector<testgraph::node_t> where_nodes(const testgraph::testgraph& g,
146137
});
147138

148139
return filtered_results;
149-
}
140+
}

test/TestSet/remove_if.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ int main(int argc, char **argv) {
2828
auto expression = clip.get<boostjsn::object>("expression");
2929
auto the_set = clip.get_state<std::set<int>>(state_name);
3030

31+
//
3132
//
3233
// Expression here
33-
auto apply_jl = [&expression](int value) {
34-
boostjsn::object data;
35-
data["value"] = value;
36-
jsonlogic::any_expr res = jsonlogic::apply(expression["rule"], data);
37-
return jsonlogic::unpack_value<bool>(res);
34+
jsonlogic::logic_rule jlrule = jsonlogic::create_logic(expression["rule"]);
35+
36+
auto apply_jl = [&jlrule](int value) {
37+
return truthy(jlrule.apply(jsonlogic::json_accessor({{"value", value}})));
3838
};
3939

4040
for (auto first = the_set.begin(), last = the_set.end(); first != last;) {

0 commit comments

Comments
 (0)