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


##########
src/types/json.h:
##########
@@ -253,6 +255,54 @@ struct JsonValue {
     return Status::OK();
   }
 
+  StatusOr<bool> Merge(const std::string_view path, const std::string 
&merge_value) {
+    bool is_updated = false;
+    const std::string json_root_path = "$";
+    try {
+      jsoncons::json patch_value = jsoncons::json::parse(merge_value);
+      bool not_exists = jsoncons::jsonpath::json_query(value, path).empty();
+
+      if (not_exists) {
+        // TODO:: Add ability to create an object from path.
+        return {Status::NotOK, "Path does not exist."};
+      }
+
+      if (path == json_root_path) {
+        // Merge with the root. Patch function complies with RFC7396 Json 
Merge Patch
+        jsoncons::mergepatch::apply_merge_patch(value, patch_value);
+        is_updated = true;
+      } else if (!patch_value.is_null()) {
+        // Replace value by path
+        jsoncons::jsonpath::json_replace(
+            value, path, [&patch_value, &is_updated](const std::string & 
/*path*/, jsoncons::json &target) {
+              target = patch_value;

Review Comment:
   ```suggestion
                 jsoncons::mergepatch::apply_merge_patch(target, patch_value);
   ```
   
   



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