mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2025-05-12 10:55:46 +02:00
feat: allow querying of json objects stored as strings (#4399)
Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
parent
1c0f22f5fa
commit
21fcf5808e
2 changed files with 40 additions and 3 deletions
|
@ -657,11 +657,29 @@ bool LegacyModeIsEnabled(const std::vector<std::pair<std::string_view, WrappedJs
|
||||||
|
|
||||||
OpResult<std::string> OpJsonGet(const OpArgs& op_args, string_view key,
|
OpResult<std::string> OpJsonGet(const OpArgs& op_args, string_view key,
|
||||||
const JsonGetParams& params) {
|
const JsonGetParams& params) {
|
||||||
OpResult<JsonType*> result = GetJson(op_args, key);
|
auto it = op_args.GetDbSlice().FindReadOnly(op_args.db_cntx, key).it;
|
||||||
RETURN_ON_BAD_STATUS(result);
|
if (!IsValid(it))
|
||||||
|
return OpStatus::KEY_NOTFOUND;
|
||||||
|
|
||||||
|
const JsonType* json_ptr = nullptr;
|
||||||
|
JsonType json;
|
||||||
|
if (it->second.ObjType() == OBJ_JSON) {
|
||||||
|
json_ptr = it->second.GetJson();
|
||||||
|
} else if (it->second.ObjType() == OBJ_STRING) {
|
||||||
|
string tmp;
|
||||||
|
it->second.GetString(&tmp);
|
||||||
|
auto parsed_json = ShardJsonFromString(tmp);
|
||||||
|
if (!parsed_json) {
|
||||||
|
return OpStatus::WRONG_TYPE;
|
||||||
|
}
|
||||||
|
json.swap(*parsed_json);
|
||||||
|
json_ptr = &json;
|
||||||
|
} else {
|
||||||
|
return OpStatus::WRONG_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
const auto& paths = params.paths;
|
const auto& paths = params.paths;
|
||||||
const JsonType& json_entry = *(result.value());
|
const JsonType& json_entry = *json_ptr;
|
||||||
|
|
||||||
if (paths.empty()) {
|
if (paths.empty()) {
|
||||||
// this implicitly means that we're using . which
|
// this implicitly means that we're using . which
|
||||||
|
|
|
@ -2998,4 +2998,23 @@ TEST_F(JsonFamilyTest, MergeLegacy) {
|
||||||
EXPECT_EQ(resp, R"({"y":{"doubled":true},"z":{"answers":["xxx","yyy"],"doubled":false}})");
|
EXPECT_EQ(resp, R"({"y":{"doubled":true},"z":{"answers":["xxx","yyy"],"doubled":false}})");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonFamilyTest, GetString) {
|
||||||
|
string json = R"(
|
||||||
|
{ "a": "b",
|
||||||
|
"c": {
|
||||||
|
"d": "e",
|
||||||
|
"f": "g"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
auto resp = Run({"SET", "json", json});
|
||||||
|
EXPECT_THAT(resp, "OK");
|
||||||
|
resp = Run({"JSON.GET", "json", "$.c"});
|
||||||
|
EXPECT_EQ(resp, R"([{"d":"e","f":"g"}])");
|
||||||
|
Run({"SET", "not_json", "not_json"});
|
||||||
|
resp = Run({"JSON.GET", "not_json", "$.c"});
|
||||||
|
EXPECT_THAT(resp, ErrArg("WRONGTYPE"));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dfly
|
} // namespace dfly
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue