-
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
Open
mapleFU
wants to merge
13
commits into
apache:unstable
Choose a base branch
from
mapleFU:json/support-basic-legacy-path
base: unstable
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6433101
JSON: support basic legacy path
mapleFU 0d06d9e
add basic impl
mapleFU 26be5fe
fix lint
mapleFU 366c316
fix arity to 4
mapleFU 6d7f215
Merge branch 'unstable' into json/support-basic-legacy-path
mapleFU 68c3677
Update: add JsonPath class
mapleFU 69ddf73
fix lint
mapleFU 50fa211
support more json-legacy-path
mapleFU 236bfce
resolve some comments
mapleFU 40c1851
add test
mapleFU b79c0a3
Merge branch 'unstable' into json/support-basic-legacy-path
mapleFU 9e110c2
resolve comment
mapleFU 6f66850
Update: resolve comment
mapleFU File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::string_view json_string; | ||
auto converted = tryConvertLegacyToJsonPath(path); | ||
if (converted.has_value()) { | ||
fixed_path = std::move(converted.value()); | ||
} | ||
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(path), std::move(fixed_path), 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,58 @@ | |
#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 &>; | ||
using JsonReplaceCallback = std::function<void(const std::string_view &, JsonType &)>; | ||
|
||
static constexpr std::string_view ROOT_PATH = "$"; | ||
|
||
static StatusOr<JsonPath> BuildJsonPath(std::string_view path); | ||
|
||
bool IsLegacy() const noexcept { return !fixed_path_.empty(); } | ||
|
||
std::string_view Path() const { | ||
if (IsLegacy()) { | ||
return fixed_path_; | ||
} | ||
return origin_; | ||
} | ||
|
||
std::string_view OriginPath() const { return origin_; } | ||
|
||
bool IsRootPath() const { return Path() == ROOT_PATH; } | ||
|
||
JsonType EvalQueryExpression(const JsonType &json_value) const { | ||
return expression_.evaluate(const_cast<JsonType &>(json_value)); | ||
} | ||
|
||
void EvalReplaceExpression(JsonType &json_value, const JsonReplaceCallback &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); | ||
} | ||
mapleFU marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private: | ||
static std::optional<std::string> tryConvertLegacyToJsonPath(std::string_view path); | ||
|
||
JsonPath(std::string path, std::string fixed_path, JsonPathExpression path_expression) | ||
: origin_(std::move(path)), fixed_path_(std::move(fixed_path)), expression_(std::move(path_expression)) {} | ||
|
||
std::string origin_; | ||
std::string fixed_path_; | ||
// 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 +102,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.EvalReplaceExpression( | ||
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. Actually I do not think this function name is clear enough. Maybe we can figure out another name. |
||
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.EvalQueryExpression(value); | ||
} catch (const jsoncons::jsonpath::jsonpath_error &e) { | ||
return {Status::NotOK, e.what()}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.