Started writing from_str and from_str_vec

This commit is contained in:
Dr. Patrick Urbanke
2025-04-05 18:00:47 +02:00
parent 7f9393ee46
commit 075bcb894a
2 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
#ifndef SQLGEN_INTERNAL_FROM_STR_HPP_
#define SQLGEN_INTERNAL_FROM_STR_HPP_
#include <optional>
#include <string>
#include <type_traits>
#include "../Result.hpp"
#include "../parsing/has_reflection_method.hpp"
#include "../parsing/is_nullable.hpp"
namespace sqlgen::internal {
template <class T>
Result<T> from_str(const std::optional<std::string>& _str) {
using Type = std::remove_cvref_t<T>;
if constexpr (parsing::has_reflection_method<Type>) {
return from_str<typename Type::ReflectionType>(_str).transform(
[](auto&& _v) { return Type(std::move(_v)); });
} else if constexpr (parsing::is_nullable_v<Type>) {
if (!_str) {
return Type();
}
if constexpr (parsing::is_ptr<Type>::value) {
return from_str<typename Type::element_type>(_str).transform(
[](auto&& _v) { return Type(std::move(_v)); });
} else {
return from_str<typename Type::value_type>(_str).transform(
[](auto&& _v) { return Type(std::move(_v)); });
}
} else {
if (!_str) {
return error("Encountered NULL value.");
}
if constexpr (std::is_same_v<Type, std::string>) {
return *_str;
} else {
try {
if constexpr (std::is_floating_point_v<Type>) {
return static_cast<Type>(std::stod(*_str));
} else if constexpr (std::is_integral_v<Type>) {
return static_cast<type>(std::stoll(*_str));
} else if (std::is_same_v<Type, bool>) {
return std::stoi(*_str) != 0;
} else {
static_assert(rfl::always_false<Type>, "Unsupported type");
}
} catch (std::exception& e) {
return error(e.what());
}
}
}
}
} // namespace sqlgen::internal
#endif

View File

@@ -0,0 +1,25 @@
#ifndef SQLGEN_INTERNAL_FROM_STR_VEC_HPP_
#define SQLGEN_INTERNAL_FROM_STR_VEC_HPP_
#include <optional>
#include <rfl.hpp>
#include <string>
#include <type_traits>
#include <vector>
#include "../Result.hpp"
namespace sqlgen::internal {
template <class T>
Result<T> from_str_vec(
const std::vector<std::optional<std::string>>& _str_vec) {
T t;
const auto view = rfl::to_view(t);
return t;
}
} // namespace sqlgen::internal
#endif