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


##########
src/types/json.h:
##########
@@ -215,6 +215,32 @@ struct JsonValue {
     return Status::OK();
   }
 
+  StatusOr<std::vector<std::optional<JsonValue>>> ArrPop(std::string_view 
path, int64_t index = -1) {
+    std::vector<std::optional<JsonValue>> popped_values;
+
+    try {
+      jsoncons::jsonpath::json_replace(value, path,
+                                       [&popped_values, index](const 
std::string & /*path*/, jsoncons::json &val) {
+                                         if (val.is_array() && !val.empty()) {
+                                           auto popped_iter = 
val.array_range().begin();
+                                           if (index == -1 || index >= 
static_cast<int64_t>(val.size())) {
+                                             popped_iter = 
val.array_range().end() - 1;
+                                           } else if (index > 0) {
+                                             popped_iter += index;
+                                           }

Review Comment:
   I think if the index is negative, we need to calculate the real position 
from the end. 
   ```shell
   127.0.0.1:6666> json.set a $ [1,2,3,4]
   OK
   127.0.0.1:6666> json.arrpop a $ -2
   1) "3"
   127.0.0.1:6666>
   ```
   I recommend you to try it by yourself : )
   



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