From 827a09b1d0ca1fdbb77bd3acace1c800f4646f21 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 19 Sep 2024 00:40:06 +0300 Subject: [PATCH] refactor data/dv & update data/dv-related tests --- src/data/dv.cpp | 9 ++--- src/data/dv.hpp | 78 +++++++++++++++----------------------------- test/coders/json.cpp | 3 ++ test/data/dv.cpp | 2 ++ 4 files changed, 34 insertions(+), 58 deletions(-) diff --git a/src/data/dv.cpp b/src/data/dv.cpp index a61beb21..11b57efe 100644 --- a/src/data/dv.cpp +++ b/src/data/dv.cpp @@ -123,13 +123,8 @@ namespace dv { } boolean_t value::asBoolean() const { - if (type == value_type::boolean) { - return val.boolean; - } else if (type == value_type::integer) { - return val.integer != 0; - } - throw_type_error(type, value_type::boolean); - return false; // unreachable + check_type(type, value_type::boolean); + return val.boolean; } objects::Bytes& value::asBytes() { diff --git a/src/data/dv.hpp b/src/data/dv.hpp index 701c7501..99e98d95 100644 --- a/src/data/dv.hpp +++ b/src/data/dv.hpp @@ -62,10 +62,10 @@ namespace dv { } /// @brief nullable value reference returned by value.at(...) - struct elementreference { + struct optionalvalue { value* ptr; - elementreference(value* ptr) : ptr(ptr) {} + optionalvalue(value* ptr) : ptr(ptr) {} inline operator bool() const { return ptr != nullptr; @@ -217,36 +217,12 @@ namespace dv { return setNone(); } - inline value& operator=(char v) { - return setInteger(v); - } - inline value& operator=(short v) { - return setInteger(v); - } - inline value& operator=(int v) { - return setInteger(v); - } - inline value& operator=(long v) { - return setInteger(v); - } - inline value& operator=(long long v) { - return setInteger(v); - } - inline value& operator=(unsigned char v) { - return setInteger(v); - } - inline value& operator=(unsigned short v) { - return setInteger(v); - } - inline value& operator=(unsigned int v) { - return setInteger(v); - } - inline value& operator=(unsigned long v) { - return setInteger(v); - } - inline value& operator=(unsigned long long v) { + template + inline std::enable_if_t() && !std::is_same(), value&> + operator=(T v) { return setInteger(v); } + inline value& operator=(float v) { return setNumber(v); } @@ -470,23 +446,23 @@ namespace dv { } } - elementreference at(const key_t& k) const { + optionalvalue at(const key_t& k) const { check_type(type, value_type::object); const auto& found = val.object->find(k); if (found == val.object->end()) { - return elementreference(nullptr); + return optionalvalue(nullptr); } - return elementreference(&found->second); + return optionalvalue(&found->second); } - elementreference at(size_t index) { + optionalvalue at(size_t index) { check_type(type, value_type::list); - return elementreference(&val.list->at(index)); + return optionalvalue(&val.list->at(index)); } - const elementreference at(size_t index) const { + const optionalvalue at(size_t index) const { check_type(type, value_type::list); - return elementreference(&val.list->at(index)); + return optionalvalue(&val.list->at(index)); } bool has(const key_t& k) const; @@ -557,7 +533,7 @@ namespace dv { } return false; } - inline bool elementreference::get(std::string& dst) const { + inline bool optionalvalue::get(std::string& dst) const { if (ptr) { dst = ptr->asString(); return true; @@ -565,7 +541,7 @@ namespace dv { return false; } - inline bool elementreference::get(bool& dst) const { + inline bool optionalvalue::get(bool& dst) const { if (ptr) { dst = ptr->asBoolean(); return true; @@ -573,40 +549,40 @@ namespace dv { return false; } - inline bool elementreference::get(char& dst) const { + inline bool optionalvalue::get(char& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(short& dst) const { + inline bool optionalvalue::get(short& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(int& dst) const { + inline bool optionalvalue::get(int& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(long& dst) const { + inline bool optionalvalue::get(long& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(long long& dst) const { + inline bool optionalvalue::get(long long& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(unsigned char& dst) const { + inline bool optionalvalue::get(unsigned char& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(unsigned short& dst) const { + inline bool optionalvalue::get(unsigned short& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(unsigned int& dst) const { + inline bool optionalvalue::get(unsigned int& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(unsigned long& dst) const { + inline bool optionalvalue::get(unsigned long& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(unsigned long long& dst) const { + inline bool optionalvalue::get(unsigned long long& dst) const { return get_to_int(ptr, dst); } - inline bool elementreference::get(float& dst) const { + inline bool optionalvalue::get(float& dst) const { return get_to_num(ptr, dst); } - inline bool elementreference::get(double& dst) const { + inline bool optionalvalue::get(double& dst) const { return get_to_num(ptr, dst); } } diff --git a/test/coders/json.cpp b/test/coders/json.cpp index f1c28cc6..be9f0534 100644 --- a/test/coders/json.cpp +++ b/test/coders/json.cpp @@ -8,6 +8,7 @@ TEST(JSON, EncodeDecode) { const int bytesSize = 20; const int year = 2019; const float score = 3.141592; + const bool visible = true; dv::objects::Bytes srcBytes(bytesSize); for (int i = 0; i < bytesSize; i ++) { srcBytes[i] = rand(); @@ -19,6 +20,7 @@ TEST(JSON, EncodeDecode) { object["name"] = name; object["year"] = year; object["score"] = score; + object["visible"] = visible; object["data"] = srcBytes; text = json::stringify(object, false, ""); @@ -28,6 +30,7 @@ TEST(JSON, EncodeDecode) { EXPECT_EQ(object["name"].asString(), name); EXPECT_EQ(object["year"].asInteger(), year); EXPECT_FLOAT_EQ(object["score"].asNumber(), score); + EXPECT_EQ(object["visible"].asBoolean(), visible); auto b64string = object["data"].asString(); auto bytes = util::base64_decode(b64string); diff --git a/test/data/dv.cpp b/test/data/dv.cpp index 617175e9..b13e3323 100644 --- a/test/data/dv.cpp +++ b/test/data/dv.cpp @@ -10,6 +10,7 @@ TEST(dv, dv) { auto& obj = list.object(); obj["name"] = "user"; obj["age"] = 90; + obj["confirmed"] = true; obj["position"] = dv::list({40, -41, 52}); } } @@ -18,6 +19,7 @@ TEST(dv, dv) { for (const auto& obj : list) { EXPECT_EQ(obj["name"].asString(), "user"); EXPECT_EQ(obj["age"].asInteger(), 90); + EXPECT_EQ(obj["confirmed"].asBoolean(), true); auto& position = obj["position"]; EXPECT_EQ(position[0].asInteger(), 40); EXPECT_EQ(position[1].asInteger(), -41);