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 22, 2023
1 parent b224f01 commit 347dd17
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 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
2 changes: 1 addition & 1 deletion src/common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct StringInStatusOr<T, std::enable_if_t<sizeof(T) < sizeof(std::string)>> :
StringInStatusOr(StringInStatusOr<U>&& v) : BaseType(new std::string(*std::move(v))) {} // NOLINT
template <typename U, typename std::enable_if_t<!StringInStatusOr<U>::inplace, int> = 0>
StringInStatusOr(StringInStatusOr<U>&& v) // NOLINT
: BaseType((typename StringInStatusOr<U>::BaseType &&)(std::move(v))) {}
: BaseType((typename StringInStatusOr<U>::BaseType&&)(std::move(v))) {}

StringInStatusOr(const StringInStatusOr& v) = delete;

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 347dd17

Please sign in to comment.