Skip to content

Commit

Permalink
add default path
Browse files Browse the repository at this point in the history
  • Loading branch information
jihuayu committed Nov 6, 2023
1 parent a864766 commit 6837ff3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/commands/cmd_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ class CommandJsonObjkeys : public Commander {

std::vector<std::optional<std::vector<std::string>>> results;

auto s = json.ObjKeys(args_[1], args_[2], results);
// If path not specified set it to $
std::string path = (args_.size() > 2) ? args_[2] : "$";
auto s = json.ObjKeys(args_[1], path, results);
if (!s.ok() && !s.IsNotFound()) return {Status::RedisExecErr, s.ToString()};
if (s.IsNotFound()) {
*output = redis::NilString();
Expand Down
3 changes: 2 additions & 1 deletion tests/gocase/unit/type/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ func TestJson(t *testing.T) {
require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a1":[1,2]}`).Err())
require.EqualValues(t, []interface{}{}, rdb.Do(ctx, "JSON.OBJKEYS", "a", "$.not_exists").Val())
// json path not object
require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a1":[1,2]}`).Err())
require.EqualValues(t, []interface{}{nil}, rdb.Do(ctx, "JSON.OBJKEYS", "a", "$.a1").Val())
// default path
require.EqualValues(t, []interface{}{[]interface{}{"a1"}}, rdb.Do(ctx, "JSON.OBJKEYS", "a").Val())
// json path has one object
require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a1":{"b":1,"c":1}}`).Err())
require.EqualValues(t, []interface{}{[]interface{}{"b", "c"}}, rdb.Do(ctx, "JSON.OBJKEYS", "a", "$.a1").Val())
Expand Down

0 comments on commit 6837ff3

Please sign in to comment.