initial matador ng commit
This commit is contained in:
@@ -0,0 +1,239 @@
|
||||
#ifndef MATADOR_CONVERT_HPP
|
||||
#define MATADOR_CONVERT_HPP
|
||||
|
||||
#include "matador/utils/types.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
//#include "matador/utils/date.hpp"
|
||||
//#include "matador/utils/time.hpp"
|
||||
|
||||
//#include "matador/utils/placeholder.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
/*
|
||||
* Conversion matrix
|
||||
* from> | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | bool | float | double | string | date | time | blob | placeholder | null
|
||||
* to | | | | | | | | | | | | | | | | |
|
||||
* ------------+------+-------+-------+-------+-------+--------+--------+--------+------+-------+--------+--------+------+------+------+-------------+-----
|
||||
* int8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
||||
* int16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
||||
* int32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
||||
* int64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
||||
* uint8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
||||
* uint16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
||||
* uint32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
||||
* uint64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
||||
* bool | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
|
||||
* float | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
|
||||
* double | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
|
||||
* string | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A
|
||||
* date | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | N/A | N/A | N/A
|
||||
* time | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | N/A | N/A | N/A
|
||||
* blob | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A
|
||||
* placeholder | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | ok | N/A
|
||||
* null | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | ok
|
||||
*
|
||||
* from integral to date/time works, value is interpreted as std::chrono::timepoint
|
||||
* from integral to blob works, bytes of integral data will converted to blob
|
||||
* from boolean to blob works, byte of bool data will converted to blob
|
||||
* from floating point to blob works, bytes of floating point data will converted to blob
|
||||
* from date to blob works, bytes of date will converted to blob
|
||||
* from time to blob works, bytes of time will converted to blob
|
||||
* from string to blob works, bytes of string data will converted to blob
|
||||
* from floating point to integral works, fractions are truncated
|
||||
* from date/time to integral works, value will be converted from std::chrono::timepoint
|
||||
* from blob to integral works, value will be converted missing is filled with zero, rest is omitted
|
||||
* from blob to floating point works, value will be converted missing is filled with zero, rest is omitted
|
||||
* from blob to boolean works, value will be converted missing is filled with zero, rest is omitted
|
||||
*/
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
enum class conversion_error {
|
||||
Ok,
|
||||
NotConvertable,
|
||||
MissingData
|
||||
};
|
||||
|
||||
enum class conversion_policy {
|
||||
Strict,
|
||||
Relax
|
||||
};
|
||||
|
||||
|
||||
// template < typename DestType, typename SourceType >
|
||||
// result<DestType, conversion_error> to(const SourceType &/*from*/);
|
||||
// result<DestType, conversion_error> to(const SourceType &/*from*/, conversion_policy /*policy*/ = conversion_policy::Strict);
|
||||
|
||||
// {
|
||||
// return failure(conversion_error::NotConvertable);
|
||||
// }
|
||||
|
||||
/*
|
||||
* Integral, Floating point & bool conversion
|
||||
*/
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t<std::is_arithmetic_v<DestType> && std::is_arithmetic_v<SourceType>>* = nullptr)
|
||||
{
|
||||
return ok(static_cast<DestType>(source));
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t<std::is_same_v<std::string, DestType> && std::is_same_v<std::string, SourceType>>* = nullptr)
|
||||
{
|
||||
return ok(source);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t<std::is_integral_v<SourceType> && !std::is_same_v<bool, SourceType> && std::is_same_v<DestType, std::string>>* = nullptr)
|
||||
{
|
||||
std::array<char, 128> buffer{};
|
||||
auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), source, 10);
|
||||
if (ec == std::errc{}) {
|
||||
return ok(std::string(buffer.data(), ptr));
|
||||
}
|
||||
|
||||
// return failure({ec, "couldn't convert value to std::string"});
|
||||
return failure(conversion_error::NotConvertable/*, "couldn't convert value to std::string"}*/);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t<std::is_floating_point_v<SourceType> && std::is_same_v<DestType, std::string>>* = nullptr)
|
||||
{
|
||||
std::array<char, 128> buffer{};
|
||||
auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), source, std::chars_format::general);
|
||||
if (ec == std::errc{}) {
|
||||
return ok(std::string(buffer.data(), ptr));
|
||||
}
|
||||
|
||||
// return failure({ec, "couldn't convert value to std::string"});
|
||||
return failure(conversion_error::NotConvertable/*, "couldn't convert value to std::string"}*/);
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_integral_v<DestType> && std::is_signed_v<DestType>>* = nullptr)
|
||||
{
|
||||
if (source.empty()) {
|
||||
return failure(conversion_error::MissingData/*, "failed to convert empty string"}*/);
|
||||
}
|
||||
char *end;
|
||||
errno = 0;
|
||||
const auto result = strtoll(source.c_str(), &end, 10);
|
||||
if (errno == ERANGE) {
|
||||
return failure(conversion_error::NotConvertable/*, "failed to convert string value"}*/);
|
||||
}
|
||||
|
||||
return ok(static_cast<DestType>(result));
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_integral_v<DestType> && std::is_unsigned_v<DestType>>* = nullptr)
|
||||
{
|
||||
if (source.empty()) {
|
||||
return failure(conversion_error::MissingData/*, "failed to convert empty string"}*/);
|
||||
}
|
||||
char *end;
|
||||
errno = 0;
|
||||
const auto result = strtoull(source.c_str(), &end, 10);
|
||||
if (errno == ERANGE) {
|
||||
return failure(conversion_error::NotConvertable/*, "failed to convert string value"}*/);
|
||||
}
|
||||
|
||||
return ok(static_cast<DestType>(result));
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_floating_point_v<DestType>>* = nullptr)
|
||||
{
|
||||
if (source.empty()) {
|
||||
return failure(conversion_error::MissingData/*, "failed to convert empty string"}*/);
|
||||
}
|
||||
char *end;
|
||||
errno = 0;
|
||||
const auto result = strtod(source.c_str(), &end);
|
||||
if (errno == ERANGE) {
|
||||
return failure(conversion_error::NotConvertable/*, "failed to convert string value"}*/);
|
||||
}
|
||||
|
||||
return ok(static_cast<DestType>(result));
|
||||
}
|
||||
|
||||
template < typename DestType >
|
||||
result<DestType, conversion_error> to(const blob &source, std::enable_if_t<std::is_same_v<DestType, blob>>* = nullptr) {
|
||||
return ok(source);
|
||||
}
|
||||
|
||||
static std::unordered_map<std::string, bool> string_to_bool_map = {
|
||||
{"true", true},
|
||||
{"on", true},
|
||||
{"1", true},
|
||||
{"false", false},
|
||||
{"off", false},
|
||||
{"0", false}
|
||||
};
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t<std::is_same_v<SourceType, std::string> &&std::is_same_v<DestType, bool>>* = nullptr) {
|
||||
if (source.empty()) {
|
||||
return failure(conversion_error::MissingData);
|
||||
}
|
||||
const auto it = string_to_bool_map.find(source);
|
||||
if (it == string_to_bool_map.end()) {
|
||||
return failure(conversion_error::NotConvertable);
|
||||
}
|
||||
return ok(it->second);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t<std::is_same_v<SourceType, bool> &&std::is_same_v<DestType, std::string>>* = nullptr) {
|
||||
return ok(std::string(source ? "true" : "false"));
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<SourceType, blob> &&std::is_same_v<DestType, std::string>>* = nullptr) {
|
||||
return failure(conversion_error::NotConvertable);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t<std::is_same_v<SourceType, const char*> &&std::is_same_v<DestType, std::string>>* = nullptr) {
|
||||
return ok(std::string(source));
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_arithmetic_v<DestType> && std::is_same_v<SourceType, blob>>* = nullptr) {
|
||||
return failure(conversion_error::NotConvertable);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t<std::is_integral_v<SourceType> && std::is_same_v<DestType, blob>>* = nullptr) {
|
||||
DestType result;
|
||||
result.resize(sizeof(source));
|
||||
std::memcpy(result.data(), &source, sizeof(source));
|
||||
|
||||
return ok(result);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_floating_point_v<SourceType> && std::is_same_v<DestType, blob>>* = nullptr) {
|
||||
return failure(conversion_error::NotConvertable);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<SourceType, std::string> && std::is_same_v<DestType, blob>>* = nullptr) {
|
||||
return failure(conversion_error::NotConvertable);
|
||||
}
|
||||
|
||||
template < typename DestType, typename SourceType >
|
||||
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<SourceType, const char*> && std::is_same_v<DestType, blob>>* = nullptr) {
|
||||
return failure(conversion_error::NotConvertable);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //MATADOR_CONVERT_HPP
|
||||
Reference in New Issue
Block a user