refactor data/dv & update data/dv-related tests

This commit is contained in:
MihailRis 2024-09-19 00:40:06 +03:00
parent d6d07f0a3a
commit 827a09b1d0
4 changed files with 34 additions and 58 deletions

View File

@ -123,13 +123,8 @@ namespace dv {
} }
boolean_t value::asBoolean() const { boolean_t value::asBoolean() const {
if (type == value_type::boolean) { check_type(type, value_type::boolean);
return val.boolean; return val.boolean;
} else if (type == value_type::integer) {
return val.integer != 0;
}
throw_type_error(type, value_type::boolean);
return false; // unreachable
} }
objects::Bytes& value::asBytes() { objects::Bytes& value::asBytes() {

View File

@ -62,10 +62,10 @@ namespace dv {
} }
/// @brief nullable value reference returned by value.at(...) /// @brief nullable value reference returned by value.at(...)
struct elementreference { struct optionalvalue {
value* ptr; value* ptr;
elementreference(value* ptr) : ptr(ptr) {} optionalvalue(value* ptr) : ptr(ptr) {}
inline operator bool() const { inline operator bool() const {
return ptr != nullptr; return ptr != nullptr;
@ -217,36 +217,12 @@ namespace dv {
return setNone(); return setNone();
} }
inline value& operator=(char v) { template<typename T>
return setInteger(v); inline std::enable_if_t<std::is_integral<T>() && !std::is_same<T, bool>(), value&>
} operator=(T 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) {
return setInteger(v); return setInteger(v);
} }
inline value& operator=(float v) { inline value& operator=(float v) {
return setNumber(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); check_type(type, value_type::object);
const auto& found = val.object->find(k); const auto& found = val.object->find(k);
if (found == val.object->end()) { 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); 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); 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; bool has(const key_t& k) const;
@ -557,7 +533,7 @@ namespace dv {
} }
return false; return false;
} }
inline bool elementreference::get(std::string& dst) const { inline bool optionalvalue::get(std::string& dst) const {
if (ptr) { if (ptr) {
dst = ptr->asString(); dst = ptr->asString();
return true; return true;
@ -565,7 +541,7 @@ namespace dv {
return false; return false;
} }
inline bool elementreference::get(bool& dst) const { inline bool optionalvalue::get(bool& dst) const {
if (ptr) { if (ptr) {
dst = ptr->asBoolean(); dst = ptr->asBoolean();
return true; return true;
@ -573,40 +549,40 @@ namespace dv {
return false; return false;
} }
inline bool elementreference::get(char& dst) const { inline bool optionalvalue::get(char& dst) const {
return get_to_int(ptr, dst); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); return get_to_num(ptr, dst);
} }
} }

View File

@ -8,6 +8,7 @@ TEST(JSON, EncodeDecode) {
const int bytesSize = 20; const int bytesSize = 20;
const int year = 2019; const int year = 2019;
const float score = 3.141592; const float score = 3.141592;
const bool visible = true;
dv::objects::Bytes srcBytes(bytesSize); dv::objects::Bytes srcBytes(bytesSize);
for (int i = 0; i < bytesSize; i ++) { for (int i = 0; i < bytesSize; i ++) {
srcBytes[i] = rand(); srcBytes[i] = rand();
@ -19,6 +20,7 @@ TEST(JSON, EncodeDecode) {
object["name"] = name; object["name"] = name;
object["year"] = year; object["year"] = year;
object["score"] = score; object["score"] = score;
object["visible"] = visible;
object["data"] = srcBytes; object["data"] = srcBytes;
text = json::stringify(object, false, ""); text = json::stringify(object, false, "");
@ -28,6 +30,7 @@ TEST(JSON, EncodeDecode) {
EXPECT_EQ(object["name"].asString(), name); EXPECT_EQ(object["name"].asString(), name);
EXPECT_EQ(object["year"].asInteger(), year); EXPECT_EQ(object["year"].asInteger(), year);
EXPECT_FLOAT_EQ(object["score"].asNumber(), score); EXPECT_FLOAT_EQ(object["score"].asNumber(), score);
EXPECT_EQ(object["visible"].asBoolean(), visible);
auto b64string = object["data"].asString(); auto b64string = object["data"].asString();
auto bytes = util::base64_decode(b64string); auto bytes = util::base64_decode(b64string);

View File

@ -10,6 +10,7 @@ TEST(dv, dv) {
auto& obj = list.object(); auto& obj = list.object();
obj["name"] = "user"; obj["name"] = "user";
obj["age"] = 90; obj["age"] = 90;
obj["confirmed"] = true;
obj["position"] = dv::list({40, -41, 52}); obj["position"] = dv::list({40, -41, 52});
} }
} }
@ -18,6 +19,7 @@ TEST(dv, dv) {
for (const auto& obj : list) { for (const auto& obj : list) {
EXPECT_EQ(obj["name"].asString(), "user"); EXPECT_EQ(obj["name"].asString(), "user");
EXPECT_EQ(obj["age"].asInteger(), 90); EXPECT_EQ(obj["age"].asInteger(), 90);
EXPECT_EQ(obj["confirmed"].asBoolean(), true);
auto& position = obj["position"]; auto& position = obj["position"];
EXPECT_EQ(position[0].asInteger(), 40); EXPECT_EQ(position[0].asInteger(), 40);
EXPECT_EQ(position[1].asInteger(), -41); EXPECT_EQ(position[1].asInteger(), -41);