Skip to content

Commit 467899f

Browse files
refactor(json_family): code clean up
Signed-off-by: Stepan Bagritsevich <[email protected]>
1 parent d98c5d5 commit 467899f

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

src/server/detail/wrapped_json_path.h

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace dfly {
1616

17-
class Nothing {};
17+
using Nothing = std::monostate;
1818
using JsonExpression = jsoncons::jsonpath::jsonpath_expression<JsonType>;
1919

2020
template <typename T>
@@ -24,11 +24,11 @@ template <typename T = Nothing> class MutateCallbackResult {
2424
public:
2525
MutateCallbackResult() = default;
2626

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_) {
2828
}
2929

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)) {
3232
}
3333

3434
bool HasValue() const {
@@ -39,9 +39,12 @@ template <typename T = Nothing> class MutateCallbackResult {
3939
return std::move(value_).value();
4040
}
4141

42-
bool should_be_deleted;
42+
bool ShouldBeDeleted() const {
43+
return should_be_deleted_;
44+
}
4345

4446
private:
47+
bool should_be_deleted_;
4548
std::optional<T> value_;
4649
};
4750

@@ -81,6 +84,9 @@ template <typename T> class JsonCallbackResult {
8184
}
8285
}
8386

87+
explicit JsonCallbackResult(std::error_code error_code) : error_code_(error_code) {
88+
}
89+
8490
void AddValue(T&& value) {
8591
if (IsV1()) {
8692
details::OptionalEmplace(std::forward<T>(value), &AsV1());
@@ -94,27 +100,37 @@ template <typename T> class JsonCallbackResult {
94100
}
95101

96102
JsonV1Result& AsV1() {
103+
DCHECK(!ErrorOccured());
97104
return std::get<JsonV1Result>(result_);
98105
}
99106

100107
JsonV2Result& AsV2() {
108+
DCHECK(!ErrorOccured());
101109
return std::get<JsonV2Result>(result_);
102110
}
103111

104112
const JsonV1Result& AsV1() const {
113+
DCHECK(!ErrorOccured());
105114
return std::get<JsonV1Result>(result_);
106115
}
107116

108117
const JsonV2Result& AsV2() const {
118+
DCHECK(!ErrorOccured());
109119
return std::get<JsonV2Result>(result_);
110120
}
111121

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+
}
114129

115130
private:
116131
std::variant<JsonV1Result, JsonV2Result> result_;
117132
bool legacy_mode_is_enabled_;
133+
std::error_code error_code_;
118134
};
119135

120136
class WrappedJsonPath {
@@ -173,40 +189,39 @@ class WrappedJsonPath {
173189
if (res.HasValue()) {
174190
mutate_result.AddValue(std::move(res).GetValue());
175191
}
176-
return res.should_be_deleted;
192+
return res.ShouldBeDeleted();
177193
};
178194

179195
if (HoldsJsonPath()) {
180196
const auto& json_path = AsJsonPath();
181197
json::MutatePath(json_path, mutate_callback, json_entry);
182198
} 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;
187205

188-
jsoncons::jsonpath::custom_functions<JsonType> funcs =
189-
jsoncons::jsonpath::custom_functions<JsonType>();
206+
custom_functions<JsonType> funcs = custom_functions<JsonType>();
190207

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;
194211

195-
json_selector_t expr = e.compile(static_resources, path_.view(), ec);
212+
JsonSelector expr = e.compile(static_resources, path_.view(), ec);
196213
if (ec) {
197-
return mutate_result;
214+
return JsonCallbackResult<T>{ec};
198215
}
199216

200-
jsoncons::jsonpath::detail::dynamic_resources<value_type, reference> resources;
217+
dynamic_resources<ValueType, Reference> resources;
201218

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);
205221
};
206222

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);
210225
}
211226
return mutate_result;
212227
}

src/server/json_family.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ template <typename T> void Send(const std::vector<T>& vec, RedisReplyBuilder* rb
134134
}
135135

136136
template <typename T> void Send(const JsonCallbackResult<T>& result, RedisReplyBuilder* rb) {
137-
if (result.error_code) {
138-
rb->SendError(result.error_code.message());
137+
if (result.ErrorOccured()) {
138+
rb->SendError(result.GetError().message());
139139
return;
140140
}
141141

@@ -295,7 +295,7 @@ error_code JsonReplace(JsonType& instance, string_view path, json::MutateCallbac
295295
return ec;
296296
}
297297

298-
template <typename T>
298+
template <typename T = Nothing>
299299
OpResult<JsonCallbackResult<T>> UpdateEntry(const OpArgs& op_args, std::string_view key,
300300
const WrappedJsonPath& json_path,
301301
JsonPathMutateCallback<T> cb,
@@ -308,18 +308,17 @@ OpResult<JsonCallbackResult<T>> UpdateEntry(const OpArgs& op_args, std::string_v
308308
JsonType* json_val = pv.GetJson();
309309
DCHECK(json_val) << "should have a valid JSON object for key '" << key << "' the type for it is '"
310310
<< pv.ObjType() << "'";
311-
JsonType& json_entry = *json_val;
312311

313312
op_args.shard->search_indices()->RemoveDoc(key, op_args.db_cntx, pv);
314313

315314
auto mutate_res = json_path.Mutate(json_val, cb);
316-
if (mutate_res.error_code) {
317-
return mutate_res; // TODO(continue)
315+
if (mutate_res.ErrorOccured()) {
316+
return mutate_res; // TODO(If error occured return or continue)
318317
}
319318

320319
// Make sure that we don't have other internal issue with the operation
321320
if (verify_op) {
322-
verify_op(json_entry);
321+
verify_op(*json_val);
323322
}
324323

325324
it_res->post_updater.Run();

0 commit comments

Comments
 (0)