1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00

fix(server): JSON.RESP path arg should be optional (#852) (#865)

Signed-off-by: iko1 <me@remotecpp.dev>
This commit is contained in:
iko1 2023-02-23 09:38:25 +02:00 committed by GitHub
parent e8ca27971c
commit b5a8eef3e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View file

@ -38,6 +38,8 @@ using JsonReplaceCb = function<void(const string&, JsonType&)>;
using JsonReplaceVerify = std::function<OpStatus()>;
using CI = CommandId;
static const char DefaultJsonPath[] = "$";
namespace {
inline OpStatus JsonReplaceVerifyNoOp() {
@ -1004,7 +1006,10 @@ void JsonFamily::Set(CmdArgList args, ConnectionContext* cntx) {
void JsonFamily::Resp(CmdArgList args, ConnectionContext* cntx) {
string_view key = ArgS(args, 1);
string_view path = ArgS(args, 2);
string_view path = DefaultJsonPath;
if (args.size() > 2) {
path = ArgS(args, 2);
}
error_code ec;
JsonExpression expression = jsonpath::make_expression<JsonType>(path, ec);
@ -1661,7 +1666,7 @@ void JsonFamily::Register(CommandRegistry* registry) {
ArrAppend);
*registry << CI{"JSON.ARRINDEX", CO::READONLY | CO::FAST, -4, 1, 1, 1}.HFUNC(ArrIndex);
*registry << CI{"JSON.DEBUG", CO::READONLY | CO::FAST, -2, 1, 1, 1}.HFUNC(Debug);
*registry << CI{"JSON.RESP", CO::READONLY | CO::FAST, 3, 1, 1, 1}.HFUNC(Resp);
*registry << CI{"JSON.RESP", CO::READONLY | CO::FAST, -2, 1, 1, 1}.HFUNC(Resp);
*registry << CI{"JSON.SET", CO::WRITE | CO::DENYOOM | CO::FAST, 4, 1, 1, 1}.HFUNC(Set);
}

View file

@ -899,6 +899,9 @@ TEST_F(JsonFamilyTest, Resp) {
auto resp = Run({"JSON.SET", "json", ".", PhonebookJson});
ASSERT_THAT(resp, "OK");
resp = Run({"JSON.RESP", "json"});
ASSERT_EQ(RespExpr::ARRAY, resp.type);
resp = Run({"JSON.RESP", "json", "$.address.*"});
ASSERT_EQ(RespExpr::ARRAY, resp.type);
EXPECT_THAT(resp.GetVec(), ElementsAre("New York", "NY", "21 2nd Street", "10021-3100"));