PragmaTwice commented on code in PR #1829:
URL: https://github.com/apache/kvrocks/pull/1829#discussion_r1363586576
##########
src/types/json_path.cc:
##########
@@ -0,0 +1,65 @@
+/*
Review Comment:
Currently we can just put this class in json.h.
##########
src/types/json.h:
##########
@@ -54,20 +55,34 @@ struct JsonValue {
}
Status Set(std::string_view path, JsonValue &&new_value) {
+ auto path_express_status = JsonPath::BuildJsonPath(path);
+ if (!path_express_status) {
+ return path_express_status.ToStatus();
+ }
+ return Set(path_express_status.GetValue(), 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(
+ 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_express_status = JsonPath::BuildJsonPath(path);
+ if (!path_express_status) {
+ return path_express_status.ToStatus();
+ }
+ return Get(path_express_status.GetValue());
Review Comment:
```suggestion
return Get(GET_OR_RET(JsonPath::BuildJsonPath(path)));
```
##########
src/types/json.h:
##########
@@ -54,20 +55,34 @@ struct JsonValue {
}
Status Set(std::string_view path, JsonValue &&new_value) {
+ auto path_express_status = JsonPath::BuildJsonPath(path);
+ if (!path_express_status) {
+ return path_express_status.ToStatus();
+ }
+ return Set(path_express_status.GetValue(), std::move(new_value));
+ }
Review Comment:
ditto
##########
src/types/json_path.h:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ *
+ */
+
+#pragma once
+
+#include <jsoncons_ext/jsonpath/jsonpath.hpp>
+#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
+
+#include "jsoncons_ext/jsonpath/jsonpath_error.hpp"
+#include "status.h"
+
+class JsonPath {
+ public:
+ using JsonType = jsoncons::basic_json<char>;
+ using JsonPathExpression = jsoncons::jsonpath::jsonpath_expression<JsonType,
const JsonType&>;
+ using JsonQueryCallback = std::function<void(const std::string_view&, const
JsonType&)>;
Review Comment:
Cannot find where it is used 🤔
##########
src/types/json.h:
##########
@@ -54,20 +55,34 @@ struct JsonValue {
}
Status Set(std::string_view path, JsonValue &&new_value) {
+ auto path_express_status = JsonPath::BuildJsonPath(path);
+ if (!path_express_status) {
+ return path_express_status.ToStatus();
+ }
+ return Set(path_express_status.GetValue(), 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(
Review Comment:
Actually I do not think this function name is clear enough. Maybe we can
figure out another name.
##########
src/types/json_path.h:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ *
+ */
+
+#pragma once
+
+#include <jsoncons_ext/jsonpath/jsonpath.hpp>
+#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
+
+#include "jsoncons_ext/jsonpath/jsonpath_error.hpp"
+#include "status.h"
+
+class JsonPath {
+ public:
+ using JsonType = jsoncons::basic_json<char>;
+ using JsonPathExpression = jsoncons::jsonpath::jsonpath_expression<JsonType,
const JsonType&>;
+ using JsonQueryCallback = std::function<void(const std::string_view&, 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);
+ static JsonPath BuildJsonRootPath() { return
BuildJsonPath(std::string(ROOT_PATH)).GetValue(); }
+
+ 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);
+ }
Review Comment:
Is it necessary to provide two function?
##########
tests/cppunit/types/json_test.cc:
##########
@@ -119,3 +119,21 @@ TEST_F(RedisJsonTest, Get) {
ASSERT_TRUE(json_->Get(key_, {"$..x", "$..y", "$..z"}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump(),
R"({"$..x":[{"y":1},4],"$..y":[[2,{"z":3}],1],"$..z":[{"a":{"x":4}},3]})");
}
+
+TEST_F(RedisJsonTest, LegacyPath) {
+ ASSERT_THAT(json_->Set(key_, ".", "invalid").ToString(),
MatchesRegex(".*syntax_error.*"));
+ ASSERT_THAT(json_->Set(key_, ".", "{").ToString(),
MatchesRegex(".*Unexpected end of file.*"));
+
+ ASSERT_TRUE(json_->Set(key_, ".", " \t{\n } ").ok());
+ ASSERT_TRUE(json_->Get(key_, {"."}, &json_val_).ok());
+ ASSERT_EQ(json_val_.Dump(), "[{}]");
+
+ auto s = json_->Set(key_, ".", "1");
+ ASSERT_TRUE(s.ok()) << s.ToString();
+ ASSERT_TRUE(json_->Get(key_, {"."}, &json_val_).ok());
+ ASSERT_EQ(json_val_.Dump(), "[1]");
+
+ ASSERT_TRUE(json_->Set(key_, ".", "[1, 2, 3]").ok());
+ ASSERT_TRUE(json_->Get(key_, {"."}, &json_val_).ok());
+ ASSERT_EQ(json_val_.Dump(), "[[1,2,3]]");
Review Comment:
Could you provide more tests about `tryConvertLegacyToJsonPath`?
##########
src/types/json_path.h:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ *
+ */
+
+#pragma once
+
+#include <jsoncons_ext/jsonpath/jsonpath.hpp>
+#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
+
+#include "jsoncons_ext/jsonpath/jsonpath_error.hpp"
+#include "status.h"
+
+class JsonPath {
+ public:
+ using JsonType = jsoncons::basic_json<char>;
+ using JsonPathExpression = jsoncons::jsonpath::jsonpath_expression<JsonType,
const JsonType&>;
+ using JsonQueryCallback = std::function<void(const std::string_view&, const
JsonType&)>;
Review Comment:
Cannot find where it is used 🤔
##########
src/types/json_path.cc:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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_path.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`.
Review Comment:
Could you provide a URL to this function?
##########
src/types/json_path.h:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ *
+ */
+
+#pragma once
+
+#include <jsoncons_ext/jsonpath/jsonpath.hpp>
+#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
+
+#include "jsoncons_ext/jsonpath/jsonpath_error.hpp"
+#include "status.h"
+
+class JsonPath {
+ public:
+ using JsonType = jsoncons::basic_json<char>;
+ using JsonPathExpression = jsoncons::jsonpath::jsonpath_expression<JsonType,
const JsonType&>;
+ using JsonQueryCallback = std::function<void(const std::string_view&, 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);
+ static JsonPath BuildJsonRootPath() { return
BuildJsonPath(std::string(ROOT_PATH)).GetValue(); }
+
+ 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);
+ }
+
+ 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_;
Review Comment:
Is it necessary to store the original path string? Or just store the
original one rather than the modified one?
##########
src/types/json_path.h:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ *
+ */
+
+#pragma once
+
+#include <jsoncons_ext/jsonpath/jsonpath.hpp>
+#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
+
+#include "jsoncons_ext/jsonpath/jsonpath_error.hpp"
+#include "status.h"
+
+class JsonPath {
+ public:
+ using JsonType = jsoncons::basic_json<char>;
+ using JsonPathExpression = jsoncons::jsonpath::jsonpath_expression<JsonType,
const JsonType&>;
+ using JsonQueryCallback = std::function<void(const std::string_view&, 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);
+ static JsonPath BuildJsonRootPath() { return
BuildJsonPath(std::string(ROOT_PATH)).GetValue(); }
Review Comment:
Could we just provide a static variable rather than a function?
##########
src/types/json_path.h:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ *
+ */
+
+#pragma once
+
+#include <jsoncons_ext/jsonpath/jsonpath.hpp>
+#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
+
+#include "jsoncons_ext/jsonpath/jsonpath_error.hpp"
+#include "status.h"
+
+class JsonPath {
+ public:
+ using JsonType = jsoncons::basic_json<char>;
Review Comment:
```suggestion
using JsonType = jsoncons::json;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]