Skip to content

Commit

Permalink
runned formatting, go lint and tidy.
Browse files Browse the repository at this point in the history
  • Loading branch information
2rueSid committed Oct 23, 2023
1 parent 7025f3f commit 5b4a7b3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/commands/cmd_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ class CommandJsonClear : public Commander {
}
};


REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandJsonSet>("json.set", 4, "write", 1, 1, 1),
MakeCmdAttr<CommandJsonGet>("json.get", -2, "read-only", 1, 1, 1),
MakeCmdAttr<CommandJsonType>("json.type", -2, "read-only", 1, 1, 1),
Expand Down
33 changes: 17 additions & 16 deletions src/types/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,23 @@ struct JsonValue {
Status Clear(std::string_view path, int *result) {
try {
int cleared_count = 0;
jsoncons::jsonpath::json_replace(
value, path, [&cleared_count](const std::string &
/*path*/, jsoncons::json &val) {
bool is_array = val.is_array() && !val.empty();
bool is_object = val.is_object() && !val.empty();
bool is_number = val.is_number() && val.as<double>() != 0;

if (!is_number && !is_array && !is_object) {
return;
}

if (is_array) val = jsoncons::json::array();
if (is_object) val = jsoncons::json::object();
if (is_number) val = 0;
cleared_count++;
});
jsoncons::jsonpath::json_replace(value, path,
[&cleared_count](const std::string &
/*path*/,
jsoncons::json &val) {
bool is_array = val.is_array() && !val.empty();
bool is_object = val.is_object() && !val.empty();
bool is_number = val.is_number() && val.as<double>() != 0;

if (!is_number && !is_array && !is_object) {
return;
}

if (is_array) val = jsoncons::json::array();
if (is_object) val = jsoncons::json::object();
if (is_number) val = 0;
cleared_count++;
});

*result = cleared_count;
} catch (const jsoncons::jsonpath::jsonpath_error &e) {
Expand Down
18 changes: 15 additions & 3 deletions tests/cppunit/types/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,22 @@ TEST_F(RedisJsonTest, ArrAppend) {
TEST_F(RedisJsonTest, Clear) {
int result = 0;

ASSERT_TRUE(json_->Set(key_, "$", R"({"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14})").ok());
ASSERT_TRUE(
json_
->Set(key_, "$",
R"({"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14})")
.ok());

ASSERT_TRUE(json_->Clear(key_, "$", &result).ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump().GetValue(), "{}");
ASSERT_EQ(result, 1);

ASSERT_TRUE(json_->Set(key_, "$", R"({"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14})").ok());
ASSERT_TRUE(
json_
->Set(key_, "$",
R"({"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14})")
.ok());

ASSERT_TRUE(json_->Clear(key_, "$.obj", &result).ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
Expand All @@ -204,7 +212,11 @@ TEST_F(RedisJsonTest, Clear) {
ASSERT_EQ(json_val_.Dump().GetValue(), R"({"arr":[],"bool":true,"float":3.14,"int":42,"obj":{},"str":"foo"})");
ASSERT_EQ(result, 1);

ASSERT_TRUE(json_->Set(key_, "$", R"({"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14})").ok());
ASSERT_TRUE(
json_
->Set(key_, "$",
R"({"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14})")
.ok());
ASSERT_TRUE(json_->Clear(key_, "$.*", &result).ok());
ASSERT_TRUE(json_->Get(key_, {}, &json_val_).ok());
ASSERT_EQ(json_val_.Dump().GetValue(), R"({"arr":[],"bool":true,"float":0,"int":0,"obj":{},"str":"foo"})");
Expand Down

0 comments on commit 5b4a7b3

Please sign in to comment.