14
14
15
15
namespace dfly {
16
16
17
- class Nothing {} ;
17
+ using Nothing = std::monostate ;
18
18
using JsonExpression = jsoncons::jsonpath::jsonpath_expression<JsonType>;
19
19
20
20
template <typename T>
@@ -24,11 +24,11 @@ template <typename T = Nothing> class MutateCallbackResult {
24
24
public:
25
25
MutateCallbackResult () = default ;
26
26
27
- explicit MutateCallbackResult (bool should_be_deleted_ ) : should_be_deleted (should_be_deleted_) {
27
+ explicit MutateCallbackResult (bool should_be_deleted ) : should_be_deleted_ (should_be_deleted_) {
28
28
}
29
29
30
- MutateCallbackResult (bool should_be_deleted_ , T&& value)
31
- : should_be_deleted( should_be_deleted_), value_(std::forward<T>(value)) {
30
+ MutateCallbackResult (bool should_be_deleted , T&& value)
31
+ : should_be_deleted_(should_be_deleted ), value_(std::forward<T>(value)) {
32
32
}
33
33
34
34
bool HasValue () const {
@@ -39,9 +39,12 @@ template <typename T = Nothing> class MutateCallbackResult {
39
39
return std::move (value_).value ();
40
40
}
41
41
42
- bool should_be_deleted;
42
+ bool ShouldBeDeleted () const {
43
+ return should_be_deleted_;
44
+ }
43
45
44
46
private:
47
+ bool should_be_deleted_;
45
48
std::optional<T> value_;
46
49
};
47
50
@@ -81,6 +84,9 @@ template <typename T> class JsonCallbackResult {
81
84
}
82
85
}
83
86
87
+ explicit JsonCallbackResult (std::error_code error_code) : error_code_(error_code) {
88
+ }
89
+
84
90
void AddValue (T&& value) {
85
91
if (IsV1 ()) {
86
92
details::OptionalEmplace (std::forward<T>(value), &AsV1 ());
@@ -94,27 +100,37 @@ template <typename T> class JsonCallbackResult {
94
100
}
95
101
96
102
JsonV1Result& AsV1 () {
103
+ DCHECK (!ErrorOccured ());
97
104
return std::get<JsonV1Result>(result_);
98
105
}
99
106
100
107
JsonV2Result& AsV2 () {
108
+ DCHECK (!ErrorOccured ());
101
109
return std::get<JsonV2Result>(result_);
102
110
}
103
111
104
112
const JsonV1Result& AsV1 () const {
113
+ DCHECK (!ErrorOccured ());
105
114
return std::get<JsonV1Result>(result_);
106
115
}
107
116
108
117
const JsonV2Result& AsV2 () const {
118
+ DCHECK (!ErrorOccured ());
109
119
return std::get<JsonV2Result>(result_);
110
120
}
111
121
112
- public:
113
- std::error_code error_code;
122
+ bool ErrorOccured () const {
123
+ return static_cast <bool >(error_code_);
124
+ }
125
+
126
+ const std::error_code& GetError () const {
127
+ return error_code_;
128
+ }
114
129
115
130
private:
116
131
std::variant<JsonV1Result, JsonV2Result> result_;
117
132
bool legacy_mode_is_enabled_;
133
+ std::error_code error_code_;
118
134
};
119
135
120
136
class WrappedJsonPath {
@@ -173,40 +189,39 @@ class WrappedJsonPath {
173
189
if (res.HasValue ()) {
174
190
mutate_result.AddValue (std::move (res).GetValue ());
175
191
}
176
- return res.should_be_deleted ;
192
+ return res.ShouldBeDeleted () ;
177
193
};
178
194
179
195
if (HoldsJsonPath ()) {
180
196
const auto & json_path = AsJsonPath ();
181
197
json::MutatePath (json_path, mutate_callback, json_entry);
182
198
} else {
183
- using evaluator_t = jsoncons::jsonpath::detail::jsonpath_evaluator<JsonType, JsonType&>;
184
- using value_type = evaluator_t ::value_type;
185
- using reference = evaluator_t ::reference;
186
- using json_selector_t = evaluator_t ::path_expression_type;
199
+ using namespace jsoncons ::jsonpath;
200
+ using namespace jsoncons ::jsonpath::detail;
201
+ using Evaluator = jsonpath_evaluator<JsonType, JsonType&>;
202
+ using ValueType = Evaluator::value_type;
203
+ using Reference = Evaluator::reference;
204
+ using JsonSelector = Evaluator::path_expression_type;
187
205
188
- jsoncons::jsonpath::custom_functions<JsonType> funcs =
189
- jsoncons::jsonpath::custom_functions<JsonType>();
206
+ custom_functions<JsonType> funcs = custom_functions<JsonType>();
190
207
191
- auto & ec = mutate_result. error_code ;
192
- jsoncons::jsonpath::detail:: static_resources<value_type, reference > static_resources (funcs);
193
- evaluator_t e;
208
+ std::error_code ec;
209
+ static_resources<ValueType, Reference > static_resources (funcs);
210
+ Evaluator e;
194
211
195
- json_selector_t expr = e.compile (static_resources, path_.view (), ec);
212
+ JsonSelector expr = e.compile (static_resources, path_.view (), ec);
196
213
if (ec) {
197
- return mutate_result ;
214
+ return JsonCallbackResult<T>{ec} ;
198
215
}
199
216
200
- jsoncons::jsonpath::detail:: dynamic_resources<value_type, reference > resources;
217
+ dynamic_resources<ValueType, Reference > resources;
201
218
202
- auto f = [&mutate_callback](const jsoncons::jsonpath::basic_path_node<char >& path,
203
- JsonType& val) {
204
- mutate_callback (jsoncons::jsonpath::to_string (path), &val);
219
+ auto f = [&mutate_callback](const basic_path_node<char >& path, JsonType& val) {
220
+ mutate_callback (to_string (path), &val);
205
221
};
206
222
207
- expr.evaluate (
208
- resources, *json_entry, json_selector_t ::path_node_type{}, *json_entry, std::move (f),
209
- jsoncons::jsonpath::result_options::nodups | jsoncons::jsonpath::result_options::path);
223
+ expr.evaluate (resources, *json_entry, JsonSelector::path_node_type{}, *json_entry,
224
+ std::move (f), result_options::nodups | result_options::path);
210
225
}
211
226
return mutate_result;
212
227
}
0 commit comments