-
Notifications
You must be signed in to change notification settings - Fork 433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON: support basic legacy path #1829
base: unstable
Are you sure you want to change the base?
Changes from all commits
6433101
0d06d9e
26be5fe
366c316
6d7f215
68c3677
69ddf73
50fa211
236bfce
40c1851
b79c0a3
9e110c2
6f66850
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
*/ | ||
|
||
#include "json.h" | ||
|
||
StatusOr<JsonPath> JsonPath::BuildJsonPath(std::string_view path) { | ||
std::string fixed_path; | ||
std::optional<std::string> converted = tryConvertLegacyToJsonPath(path); | ||
if (converted.has_value()) { | ||
fixed_path = std::move(converted.value()); | ||
} | ||
std::string_view json_string; | ||
if (fixed_path.empty()) { | ||
json_string = path; | ||
} else { | ||
json_string = fixed_path; | ||
} | ||
|
||
std::error_code json_parse_error; | ||
auto path_expression = jsoncons::jsonpath::make_expression<JsonType>(json_string, json_parse_error); | ||
if (json_parse_error) { | ||
return {Status::NotOK, json_parse_error.message()}; | ||
} | ||
return JsonPath(std::string(json_string), !fixed_path.empty(), std::move(path_expression)); | ||
} | ||
|
||
// https://redis.io/docs/data-types/json/path/#legacy-path-syntax | ||
// The logic here is port from RedisJson `Path::new`, see | ||
// https://github.com/RedisJSON/RedisJSON/blob/d4ba815fb13412c879b2cd3cd29ab60aee3c211b/redis_json/src/redisjson.rs#L109 | ||
std::optional<std::string> JsonPath::tryConvertLegacyToJsonPath(std::string_view path) { | ||
if (path.empty()) { | ||
return std::nullopt; | ||
} | ||
if (path[0] == '$') { | ||
if (path.size() == 1) { | ||
return std::nullopt; | ||
} | ||
if (path[1] == '.' || path[1] == '[') { | ||
return std::nullopt; | ||
} | ||
} | ||
if (path[0] == '.') { | ||
if (path.size() == 1) { | ||
return "$"; | ||
} | ||
return std::string("$") + std::string(path); | ||
} | ||
return std::string("$.") + std::string(path); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,10 +23,51 @@ | |||||
#include <jsoncons/json.hpp> | ||||||
#include <jsoncons/json_error.hpp> | ||||||
#include <jsoncons_ext/jsonpath/json_query.hpp> | ||||||
#include <jsoncons_ext/jsonpath/jsonpath_error.hpp> | ||||||
|
||||||
#include "jsoncons_ext/jsonpath/jsonpath_error.hpp" | ||||||
#include "status.h" | ||||||
|
||||||
class JsonPath { | ||||||
public: | ||||||
using JsonType = jsoncons::json; | ||||||
using JsonPathExpression = jsoncons::jsonpath::jsonpath_expression<JsonType, const JsonType &>; | ||||||
|
||||||
static constexpr std::string_view ROOT_PATH = "$"; | ||||||
|
||||||
static StatusOr<JsonPath> BuildJsonPath(std::string_view path); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
bool IsLegacy() const noexcept { return is_legacy_; } | ||||||
|
||||||
std::string_view Path() const { return path_; } | ||||||
|
||||||
bool IsRootPath() const { return Path() == ROOT_PATH; } | ||||||
|
||||||
JsonType EvalJsonQuery(const JsonType &json_value) const { | ||||||
return expression_.evaluate(const_cast<JsonType &>(json_value)); | ||||||
} | ||||||
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could it return multiple query result? e.g. query |
||||||
|
||||||
template <typename BinaryJsonFunction> | ||||||
void EvalJsonReplace(JsonType &json_value, const BinaryJsonFunction &callback) const { | ||||||
auto wrapped_cb = [&callback](const std::string_view &path, const JsonType &json) { | ||||||
// Though JsonPath supports mutable reference, `jsoncons::make_expression` | ||||||
// only supports const reference, so const_cast is used as a workaround. | ||||||
callback(path, const_cast<JsonType &>(json)); | ||||||
}; | ||||||
expression_.evaluate(json_value, wrapped_cb); | ||||||
} | ||||||
Comment on lines
+50
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as #1829 (comment), maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. json_replace has use a different expression 😅 I don't know how jsoncons consider it. I'll dive into it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PragmaTwice The author says he will implement a mutable in next release. Let me make this patch small and waiting for next release |
||||||
|
||||||
private: | ||||||
static std::optional<std::string> tryConvertLegacyToJsonPath(std::string_view path); | ||||||
|
||||||
JsonPath(std::string path, bool is_legacy, JsonPathExpression path_expression) | ||||||
: path_(std::move(path)), is_legacy_(is_legacy), expression_(std::move(path_expression)) {} | ||||||
|
||||||
std::string path_; | ||||||
bool is_legacy_; | ||||||
// Pre-build the expression to avoid built it multiple times. | ||||||
JsonPathExpression expression_; | ||||||
}; | ||||||
|
||||||
struct JsonValue { | ||||||
JsonValue() = default; | ||||||
explicit JsonValue(jsoncons::basic_json<char> value) : value(std::move(value)) {} | ||||||
|
@@ -54,20 +95,28 @@ struct JsonValue { | |||||
} | ||||||
|
||||||
Status Set(std::string_view path, JsonValue &&new_value) { | ||||||
auto path_expression = GET_OR_RET(JsonPath::BuildJsonPath(path)); | ||||||
return Set(path_expression, std::move(new_value)); | ||||||
} | ||||||
|
||||||
Status Set(const JsonPath &path, JsonValue &&new_value) { | ||||||
try { | ||||||
jsoncons::jsonpath::json_replace(value, path, [&new_value](const std::string & /*path*/, jsoncons::json &origin) { | ||||||
origin = new_value.value; | ||||||
}); | ||||||
path.EvalJsonReplace( | ||||||
value, [&new_value](const std::string_view & /*path*/, jsoncons::json &origin) { origin = new_value.value; }); | ||||||
} catch (const jsoncons::jsonpath::jsonpath_error &e) { | ||||||
return {Status::NotOK, e.what()}; | ||||||
} | ||||||
|
||||||
return Status::OK(); | ||||||
} | ||||||
|
||||||
StatusOr<JsonValue> Get(std::string_view path) const { | ||||||
auto path_expression = GET_OR_RET(JsonPath::BuildJsonPath(path)); | ||||||
return Get(path_expression); | ||||||
} | ||||||
|
||||||
StatusOr<JsonValue> Get(const JsonPath &json_path) const { | ||||||
try { | ||||||
return jsoncons::jsonpath::json_query(value, path); | ||||||
return json_path.EvalJsonQuery(value); | ||||||
} catch (const jsoncons::jsonpath::jsonpath_error &e) { | ||||||
return {Status::NotOK, e.what()}; | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe we can avoid re-construct json_string while it is fixed_path.