From 5b4a7b37a77544c617fee7fe775bb380966618b3 Mon Sep 17 00:00:00 2001 From: Dima Kachurovskyy Date: Sun, 22 Oct 2023 17:28:37 -0400 Subject: [PATCH] runned formatting, go lint and tidy. --- src/commands/cmd_json.cc | 1 - src/types/json.h | 33 ++++++++++++++++---------------- tests/cppunit/types/json_test.cc | 18 ++++++++++++++--- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/commands/cmd_json.cc b/src/commands/cmd_json.cc index 247535a7eb6..d17b3b556be 100644 --- a/src/commands/cmd_json.cc +++ b/src/commands/cmd_json.cc @@ -171,7 +171,6 @@ class CommandJsonClear : public Commander { } }; - REDIS_REGISTER_COMMANDS(MakeCmdAttr("json.set", 4, "write", 1, 1, 1), MakeCmdAttr("json.get", -2, "read-only", 1, 1, 1), MakeCmdAttr("json.type", -2, "read-only", 1, 1, 1), diff --git a/src/types/json.h b/src/types/json.h index 46cea12140b..fbcb956f947 100644 --- a/src/types/json.h +++ b/src/types/json.h @@ -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() != 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() != 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) { diff --git a/tests/cppunit/types/json_test.cc b/tests/cppunit/types/json_test.cc index bb760d7cbff..79ff4fd5994 100644 --- a/tests/cppunit/types/json_test.cc +++ b/tests/cppunit/types/json_test.cc @@ -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()); @@ -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"})");