PragmaTwice commented on code in PR #1829:
URL: https://github.com/apache/kvrocks/pull/1829#discussion_r1364825956


##########
src/types/json.h:
##########
@@ -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);
+  }

Review Comment:
   You can check how json_replace is implemented for this.



##########
src/types/redis_json.cc:
##########
@@ -102,14 +108,18 @@ rocksdb::Status Json::Get(const std::string &user_key, 
const std::vector<std::st
   if (paths.empty()) {
     res = std::move(json_val);
   } else if (paths.size() == 1) {
-    auto get_res = json_val.Get(paths[0]);
+    auto json_path_result = JsonPath::BuildJsonPath(paths[0]);
+    if (!json_path_result) return 
rocksdb::Status::InvalidArgument(json_path_result.Msg());
+    auto get_res = json_val.Get(json_path_result.GetValue());
     if (!get_res) return rocksdb::Status::InvalidArgument(get_res.Msg());
     res = *std::move(get_res);
   } else {
     for (const auto &path : paths) {
-      auto get_res = json_val.Get(path);
+      auto json_path_result = JsonPath::BuildJsonPath(path);
+      if (!json_path_result) return 
rocksdb::Status::InvalidArgument(json_path_result.Msg());
+      auto get_res = json_val.Get(json_path_result.GetValue());
       if (!get_res) return rocksdb::Status::InvalidArgument(get_res.Msg());

Review Comment:
   I think maybe there's no difference between the original code and this patch 
here.



-- 
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]

Reply via email to