chore: Support json paths without root selector (#2747)

* chore: Support json paths without root selector

---------

Signed-off-by: Vladislav Oleshko <vlad@dragonflydb.io>
This commit is contained in:
Vladislav 2024-03-19 21:09:52 +03:00 committed by GitHub
parent 2d246adbbb
commit 1ec603e568
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 0 deletions

View file

@ -5,6 +5,7 @@
#include "server/json_family.h"
#include <absl/strings/match.h>
#include <absl/strings/str_cat.h>
#include <absl/strings/str_join.h>
#include <absl/strings/str_split.h>
@ -1295,6 +1296,13 @@ OpResult<bool> OpSet(const OpArgs& op_args, string_view key, string_view path,
}
io::Result<JsonPathV2, string> ParsePathV2(string_view path) {
// We expect all valid paths to start with the root selector, otherwise prepend it
string tmp_buf;
if (!path.empty() && path.front() != '$') {
tmp_buf = absl::StrCat("$", path.front() != '.' ? "." : "", path);
path = tmp_buf;
}
if (absl::GetFlag(FLAGS_jsonpathv2)) {
return json::ParsePath(path);
}

View file

@ -69,6 +69,12 @@ TEST_F(JsonFamilyTest, SetGetBasic) {
resp = Run({"JSON.GET", "json", "//book[0]"});
EXPECT_THAT(resp, ArgType(RespExpr::ERROR));
resp = Run({"JSON.GET", "json", "store.book[0].category"});
EXPECT_EQ(resp, "[\"Fantasy\"]");
resp = Run({"JSON.GET", "json", ".store.book[0].category"});
EXPECT_EQ(resp, "[\"Fantasy\"]");
resp = Run({"SET", "xml", xml});
ASSERT_THAT(resp, "OK");