20#ifndef ORES_UTILITY_RFL_REFLECTORS_HPP
21#define ORES_UTILITY_RFL_REFLECTORS_HPP
29#include <boost/uuid/uuid.hpp>
30#include <boost/uuid/uuid_io.hpp>
31#include <boost/lexical_cast.hpp>
32#include <boost/asio/ip/address.hpp>
34#include "ores.utility/rfl/time_point_parser.hpp"
35#include "ores.utility/uuid/tenant_id.hpp"
40namespace ores::utility::serialization {
43namespace ores::comms::messaging {
44 enum class compression_type : std::uint8_t;
45 enum class message_type : std::uint16_t;
56struct Reflector<boost::uuids::uuid> {
57 using ReflType = std::string;
59 static boost::uuids::uuid to(
const ReflType& str) {
60 return boost::lexical_cast<boost::uuids::uuid>(str);
63 static ReflType from(
const boost::uuids::uuid& v) {
64 return boost::lexical_cast<std::string>(v);
74struct Reflector<
ores::utility::uuid::tenant_id> {
75 using ReflType = std::string;
80 throw std::runtime_error(
"Invalid tenant_id: " + result.error());
97struct Reflector<
std::optional<boost::uuids::uuid>> {
98 using ReflType = std::optional<std::string>;
100 static std::optional<boost::uuids::uuid> to(
const ReflType& str) {
101 if (!str.has_value()) {
104 return boost::lexical_cast<boost::uuids::uuid>(str.value());
107 static ReflType from(
const std::optional<boost::uuids::uuid>& v) {
108 if (!v.has_value()) {
111 return boost::lexical_cast<std::string>(v.value());
121struct Reflector<
std::chrono::year_month_day> {
122 using ReflType = std::string;
124 static std::chrono::year_month_day to(
const ReflType& str) {
127 std::istringstream ss(str);
128 ss.imbue(std::locale::classic());
129 if (!(ss >> y >> sep1 >> m >> sep2 >> d) || sep1 !=
'-' || sep2 !=
'-' || !ss.eof())
130 throw std::runtime_error(
"Invalid date format: " + str);
131 auto ymd = std::chrono::year{y} /
132 std::chrono::month{
static_cast<unsigned>(m)} /
133 std::chrono::day{
static_cast<unsigned>(d)};
135 throw std::runtime_error(
"Invalid date value: " + str);
139 static ReflType from(
const std::chrono::year_month_day& v) {
140 return std::format(
"{:%Y-%m-%d}", v);
150struct Reflector<boost::asio::ip::address> {
151 using ReflType = std::string;
153 static boost::asio::ip::address to(
const ReflType& str) {
154 return boost::asio::ip::make_address(str);
157 static ReflType from(
const boost::asio::ip::address& v) {
158 return v.to_string();
169struct Reflector<
ores::comms::messaging::compression_type> {
170 using ReflType = std::uint8_t;
172 static ores::comms::messaging::compression_type to(
const ReflType& v) {
175 throw std::runtime_error(
"Invalid value for compression_type enum: " +
178 return static_cast<ores::comms::messaging::compression_type
>(v);
181 static ReflType from(
const ores::comms::messaging::compression_type& v) {
182 return static_cast<ReflType
>(v);
193struct Reflector<
ores::utility::serialization::error_code> {
194 using ReflType = std::uint16_t;
196 static ores::utility::serialization::error_code to(
const ReflType& v) {
199 throw std::runtime_error(
"Invalid value for error_code enum: " +
202 return static_cast<ores::utility::serialization::error_code
>(v);
205 static ReflType from(
const ores::utility::serialization::error_code& v) {
206 return static_cast<ReflType
>(v);
217struct Reflector<
ores::comms::messaging::message_type> {
218 using ReflType = std::uint16_t;
220 static ores::comms::messaging::message_type to(
const ReflType& v) {
223 if (v == 0 || v > 0x5FFF) {
224 throw std::runtime_error(
"Invalid value for message_type enum: " +
227 return static_cast<ores::comms::messaging::message_type
>(v);
230 static ReflType from(
const ores::comms::messaging::message_type& v) {
231 return static_cast<ReflType
>(v);
ORE Studio - Graphical interface and data management for Open Source Risk Engine.
Definition pricing_engine_type.hpp:27
error_code
Error codes returned by service-layer request helpers.
Definition error_code.hpp:28
A strongly-typed wrapper around a UUID representing a tenant identifier.
Definition tenant_id.hpp:66
std::string to_string() const
Converts the tenant_id to its string representation.
Definition tenant_id.cpp:81
static std::expected< tenant_id, std::string > from_string(std::string_view str)
Creates a tenant_id from a string representation.
Definition tenant_id.cpp:57