File tree Expand file tree Collapse file tree 3 files changed +89
-0
lines changed Expand file tree Collapse file tree 3 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -92,6 +92,16 @@ namespace json_logic
92
92
values = json::array ({values});
93
93
}
94
94
95
+ /* *
96
+ * In JsonLogic, operation names are treated as keywords and will result in one of the known operations being
97
+ * invoked with the provided logic and data objects. Logic objects with unrecognized operations are treated as
98
+ * "useless statements" that just returns its own values, such as:
99
+ *
100
+ * myLogic; // Returns myLogic.
101
+ *
102
+ * This is an important behaviour for the operation_array_merge, which can receive arbitrary JSON objects in its
103
+ * parameters and is expected to just return them as elements of a flattened array.
104
+ */
95
105
const auto operation_it = operations_.find (op);
96
106
97
107
if (operation_it != operations_.end ())
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ set(TEST_TARGET ${PROJECT_NAME}_test)
5
5
add_executable (
6
6
${TEST_TARGET}
7
7
8
+ add_rm_operation.cpp
8
9
operations/array/all.cpp
9
10
operations/array/filter.cpp
10
11
operations/array/in.cpp
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+ #include " gtest/gtest.h"
4
+ #include " json.hpp"
5
+
6
+ #include " fixture.h"
7
+
8
+ using namespace nlohmann ;
9
+
10
+ class AddRmOperation : public testing ::Test
11
+ {
12
+ protected:
13
+ JsonLogic* json_logic_;
14
+
15
+ void SetUp () override
16
+ {
17
+ json_logic_ = JsonLogic::GetInstance ();
18
+
19
+ json_logic_->AddOperation (" myOperation" , [this ](const json& values, const json& data) {
20
+ const double a = json_logic_->Apply (values.at (0 ), data);
21
+ const double b = json_logic_->Apply (values.at (1 ), data);
22
+
23
+ return a + b;
24
+ });
25
+ }
26
+ };
27
+
28
+ TEST_F (AddRmOperation, AddOperationPrimitive) {
29
+ const auto logic = R"(
30
+ {
31
+ "myOperation": [1, 2]
32
+ }
33
+ )" _json;
34
+
35
+ const auto result = json_logic_->Apply (logic);
36
+
37
+ EXPECT_EQ (result, 3 );
38
+ }
39
+
40
+ TEST_F (AddRmOperation, AddOperationData)
41
+ {
42
+ const auto logic = R"(
43
+ {
44
+ "myOperation": [
45
+ { "var": ["foo.bar"] },
46
+ { "var": ["foo.baz"] }
47
+ ]
48
+ }
49
+ )" _json;
50
+
51
+ const auto data = R"(
52
+ {
53
+ "foo": {
54
+ "bar": 1,
55
+ "baz": 2
56
+ }
57
+ }
58
+ )" _json;
59
+
60
+ const auto result = json_logic_->Apply (logic, data);
61
+
62
+ EXPECT_EQ (result, 3 );
63
+ }
64
+
65
+ TEST_F (AddRmOperation, RmOperation)
66
+ {
67
+ json_logic_->RmOperation (" myOperation" );
68
+
69
+ const auto logic = R"(
70
+ {
71
+ "myOperation": [1, 2]
72
+ }
73
+ )" _json;
74
+
75
+ const auto result = json_logic_->Apply (logic);
76
+
77
+ EXPECT_EQ (result, logic);
78
+ }
You can’t perform that action at this time.
0 commit comments