mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Do not allow the reading of floats as integers and vice versa (#387)
This commit is contained in:
+19
-13
@@ -85,8 +85,7 @@ struct Reader {
|
||||
avro_value_get_boolean(_var.val_, &result);
|
||||
return (result != 0);
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (type == AVRO_DOUBLE) {
|
||||
double result = 0.0;
|
||||
const auto err = avro_value_get_double(_var.val_, &result);
|
||||
@@ -94,7 +93,21 @@ struct Reader {
|
||||
return error("Could not cast to double.");
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
} else if (type == AVRO_INT32) {
|
||||
} else if (type == AVRO_FLOAT) {
|
||||
float result = 0.0;
|
||||
const auto err = avro_value_get_float(_var.val_, &result);
|
||||
if (err) {
|
||||
return error("Could not cast to float.");
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
} else {
|
||||
return rfl::error(
|
||||
"Could not cast to numeric value. The type must be float "
|
||||
"or double.");
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (type == AVRO_INT32) {
|
||||
int32_t result = 0;
|
||||
const auto err = avro_value_get_int(_var.val_, &result);
|
||||
if (err) {
|
||||
@@ -108,17 +121,10 @@ struct Reader {
|
||||
return error("Could not cast to int64.");
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
} else if (type == AVRO_FLOAT) {
|
||||
float result = 0.0;
|
||||
const auto err = avro_value_get_float(_var.val_, &result);
|
||||
if (err) {
|
||||
return error("Could not cast to float.");
|
||||
}
|
||||
return static_cast<T>(result);
|
||||
} else {
|
||||
return rfl::error(
|
||||
"Could not cast to numeric value. The type must be integral.");
|
||||
}
|
||||
return rfl::error(
|
||||
"Could not cast to numeric value. The type must be integral, float "
|
||||
"or double.");
|
||||
|
||||
} else if constexpr (internal::is_literal_v<T>) {
|
||||
int value = 0;
|
||||
|
||||
@@ -110,6 +110,7 @@ struct Reader {
|
||||
return error(
|
||||
"Could not cast to string. The type must be UTF8 or symbol.");
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (btype != BSON_TYPE_BINARY) {
|
||||
@@ -123,17 +124,23 @@ struct Reader {
|
||||
const auto data =
|
||||
internal::ptr_cast<const std::byte*>(value.v_binary.data);
|
||||
return rfl::Bytestring(data, data + value.v_binary.data_len);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (btype != BSON_TYPE_BOOL) {
|
||||
return error("Could not cast to boolean.");
|
||||
}
|
||||
return value.v_bool;
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
switch (btype) {
|
||||
case BSON_TYPE_DOUBLE:
|
||||
return static_cast<T>(value.v_double);
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (btype != BSON_TYPE_DOUBLE) {
|
||||
return error(
|
||||
"Could not cast to numeric value. The type must be double, "
|
||||
"int32, int64 or date_time.");
|
||||
}
|
||||
return static_cast<T>(value.v_double);
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
switch (btype) {
|
||||
case BSON_TYPE_INT32:
|
||||
return static_cast<T>(value.v_int32);
|
||||
|
||||
@@ -145,9 +152,10 @@ struct Reader {
|
||||
|
||||
default:
|
||||
return error(
|
||||
"Could not cast to numeric value. The type must be double, "
|
||||
"Could not cast to numeric value. The type must be "
|
||||
"int32, int64 or date_time.");
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
|
||||
if (btype != BSON_TYPE_OID) {
|
||||
return error("Could not cast to OID.");
|
||||
|
||||
@@ -78,8 +78,13 @@ class Reader {
|
||||
}
|
||||
return _var.val_.as<bool>();
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (type != capnp::DynamicValue::FLOAT) {
|
||||
return error("Could not cast to float.");
|
||||
}
|
||||
return static_cast<T>(_var.val_.as<double>());
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
switch (type) {
|
||||
case capnp::DynamicValue::INT:
|
||||
return static_cast<T>(_var.val_.as<int64_t>());
|
||||
@@ -87,13 +92,8 @@ class Reader {
|
||||
case capnp::DynamicValue::UINT:
|
||||
return static_cast<T>(_var.val_.as<uint64_t>());
|
||||
|
||||
case capnp::DynamicValue::FLOAT:
|
||||
return static_cast<T>(_var.val_.as<double>());
|
||||
|
||||
default:
|
||||
return error(
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float or double.");
|
||||
return error("Could not cast to an integer.");
|
||||
}
|
||||
|
||||
} else if constexpr (internal::is_literal_v<T>) {
|
||||
|
||||
@@ -71,6 +71,7 @@ class Reader {
|
||||
return error("Could not cast to string.");
|
||||
}
|
||||
return _var.val_->as<std::string>();
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (!_var.val_->is_byte_string()) {
|
||||
@@ -79,25 +80,28 @@ class Reader {
|
||||
const auto vec = _var.val_->as<std::vector<uint8_t>>();
|
||||
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
|
||||
return rfl::Bytestring(data, data + vec.size());
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var.val_->is_bool()) {
|
||||
return error("Could not cast to boolean.");
|
||||
}
|
||||
return _var.val_->as<bool>();
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (_var.val_->is_double()) {
|
||||
return static_cast<T>(_var.val_->as<double>());
|
||||
}
|
||||
return error("Could not cast to double.");
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (_var.val_->is_int64()) {
|
||||
return static_cast<T>(_var.val_->as<int64_t>());
|
||||
}
|
||||
if (_var.val_->is_uint64()) {
|
||||
return static_cast<T>(_var.val_->as<uint64_t>());
|
||||
}
|
||||
return error(
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float or double.");
|
||||
return error("Could not cast to integer.");
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ struct Reader {
|
||||
return error("Could not cast to a string.");
|
||||
}
|
||||
return std::string(_var.AsString().c_str());
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (!_var.IsBlob()) {
|
||||
@@ -82,21 +83,25 @@ struct Reader {
|
||||
const auto blob = _var.AsBlob();
|
||||
const auto data = internal::ptr_cast<const std::byte*>(blob.data());
|
||||
return rfl::Bytestring(data, data + blob.size());
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var.IsBool()) {
|
||||
return error("Could not cast to boolean.");
|
||||
}
|
||||
return _var.AsBool();
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (!_var.IsNumeric()) {
|
||||
if (!_var.IsFloat()) {
|
||||
return error("Could not cast to double.");
|
||||
}
|
||||
return static_cast<T>(_var.AsDouble());
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (!_var.IsNumeric()) {
|
||||
if (!_var.IsIntOrUint()) {
|
||||
return error("Could not cast to int.");
|
||||
}
|
||||
return static_cast<T>(_var.AsInt64());
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -109,26 +109,31 @@ struct Reader {
|
||||
return error("Could not cast to string.");
|
||||
}
|
||||
return std::string(r);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!yyjson_is_bool(_var.val_)) {
|
||||
return error("Could not cast to boolean.");
|
||||
}
|
||||
return yyjson_get_bool(_var.val_);
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (!yyjson_is_num(_var.val_)) {
|
||||
return error("Could not cast to double.");
|
||||
}
|
||||
return static_cast<T>(yyjson_get_num(_var.val_));
|
||||
|
||||
} else if constexpr (std::is_unsigned<std::remove_cvref_t<T>>()) {
|
||||
if (!yyjson_is_int(_var.val_)) {
|
||||
return error("Could not cast to int.");
|
||||
}
|
||||
return static_cast<T>(yyjson_get_uint(_var.val_));
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (!yyjson_is_int(_var.val_)) {
|
||||
return error("Could not cast to int.");
|
||||
}
|
||||
return static_cast<T>(yyjson_get_sint(_var.val_));
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -64,6 +64,7 @@ struct Reader {
|
||||
}
|
||||
const auto str = _var.via.str;
|
||||
return std::string(str.ptr, str.size);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (type != MSGPACK_OBJECT_BIN) {
|
||||
@@ -72,11 +73,13 @@ struct Reader {
|
||||
const auto bin = _var.via.bin;
|
||||
const auto data = internal::ptr_cast<const std::byte*>(bin.ptr);
|
||||
return rfl::Bytestring(data, data + bin.size);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (type != MSGPACK_OBJECT_BOOLEAN) {
|
||||
return error("Could not cast to boolean.");
|
||||
}
|
||||
return _var.via.boolean;
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (type == MSGPACK_OBJECT_FLOAT32 || type == MSGPACK_OBJECT_FLOAT64 ||
|
||||
type == MSGPACK_OBJECT_FLOAT) {
|
||||
@@ -85,6 +88,7 @@ struct Reader {
|
||||
return error(
|
||||
"Could not cast to numeric value. The type must be float "
|
||||
"or double.");
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
|
||||
return static_cast<T>(_var.via.u64);
|
||||
|
||||
@@ -60,21 +60,25 @@ struct Reader {
|
||||
return error("Could not cast to std::string!");
|
||||
}
|
||||
return _var->as_string();
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var->is_boolean()) {
|
||||
return error("Could not cast to bool!");
|
||||
}
|
||||
return _var->as_boolean();
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (!_var->is_floating()) {
|
||||
return error("Could not cast to double!");
|
||||
}
|
||||
return static_cast<std::remove_cvref_t<T>>(_var->as_floating());
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (!_var->is_integer()) {
|
||||
return error("Could not cast to int64_t!");
|
||||
}
|
||||
return static_cast<std::remove_cvref_t<T>>(_var->as_integer());
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ class Reader {
|
||||
return error("Could not cast to string.");
|
||||
}
|
||||
return _var.val_->as<std::string>();
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (!_var.val_->is<std::vector<uint8_t>>()) {
|
||||
@@ -79,25 +80,28 @@ class Reader {
|
||||
const auto vec = _var.val_->as<std::vector<uint8_t>>();
|
||||
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
|
||||
return rfl::Bytestring(data, data + vec.size());
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var.val_->is_bool()) {
|
||||
return error("Could not cast to boolean.");
|
||||
}
|
||||
return _var.val_->as<bool>();
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (_var.val_->is_double()) {
|
||||
return static_cast<T>(_var.val_->as<double>());
|
||||
}
|
||||
return error("Could not cast to floating point value.");
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (_var.val_->is_int64()) {
|
||||
return static_cast<T>(_var.val_->as<int64_t>());
|
||||
}
|
||||
if (_var.val_->is_uint64()) {
|
||||
return static_cast<T>(_var.val_->as<uint64_t>());
|
||||
}
|
||||
return error(
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float or double.");
|
||||
return error("Could not cast to integer.");
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -83,8 +83,10 @@ struct Reader {
|
||||
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return std::visit(get_value, _var.node_or_attribute_);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return std::visit(get_value, _var.node_or_attribute_) == "true";
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
const auto str = std::visit(get_value, _var.node_or_attribute_);
|
||||
try {
|
||||
@@ -93,6 +95,7 @@ struct Reader {
|
||||
return error("Could not cast '" + std::string(str) +
|
||||
"' to floating point value.");
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
const auto str = std::visit(get_value, _var.node_or_attribute_);
|
||||
try {
|
||||
@@ -100,6 +103,7 @@ struct Reader {
|
||||
} catch (std::exception& e) {
|
||||
return error("Could not cast '" + std::string(str) + "' to integer.");
|
||||
}
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -76,11 +76,14 @@ struct Reader {
|
||||
std::is_same<std::remove_cvref_t<T>, bool>() ||
|
||||
std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return _var.node_.as<std::remove_cvref_t<T>>();
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return static_cast<T>(_var.node_.as<std::remove_cvref_t<int64_t>>());
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
} catch (std::exception& e) {
|
||||
return error(e.what());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user