Add async/resilience/reflection primitives + FSM v2 rewrite
A round of new universal capabilities (all generic — templated/reflection based, no app coupling), verified by the test suite (1103 passing): Data reflection: - Reflect.hpp now (de)serializes chrono::duration, chrono::time_point, std::variant (tagged), std::array, and non-string-keyed std::map; Meta.hpp gains is_time_point_v et al. Async: - EventBus gains Mode::Async (worker-thread dispatch + flush()). - ThreadPool gains a bounded queue, try_submit(), and rejection stats. New modules / containers: - Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker, ResourceMonitor. - Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V> (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats). - System: process/CPU/memory metrics (Metrics.hpp) and a streaming Process API. Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions, history states, deferred events), Configuration/Logging reflection helpers, and the accompanying test suites. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
43b5a81504
commit
80293ee56f
93 files changed
+8500
-3312
No files matched your search
+536
-334
@@ -1,9 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Core/Meta.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Detail/Enum.hpp>
|
||||
#include <Void/Time/Format.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
@@ -11,8 +16,10 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#if __has_include(<meta>)
|
||||
@@ -20,7 +27,8 @@
|
||||
#define VOID_HAS_JSON_REFLECTION 1
|
||||
#endif
|
||||
|
||||
// ── Reflection → JSON ─────────────────────────────────────────────────────────
|
||||
// ── Reflection → JSON
|
||||
// ─────────────────────────────────────────────────────────
|
||||
// Serialize/deserialize aggregate structs to Json::Value using C++26 P2996
|
||||
// reflection (GCC 16+, -freflection). Field names become object keys.
|
||||
//
|
||||
@@ -43,381 +51,575 @@
|
||||
namespace Void::Json
|
||||
{
|
||||
|
||||
struct ReflectError {
|
||||
std::string field; // dotted path of the offending field ("" if global)
|
||||
std::string message;
|
||||
};
|
||||
struct ReflectError {
|
||||
std::string
|
||||
field; // dotted path of the offending field ("" if global)
|
||||
std::string message;
|
||||
};
|
||||
|
||||
// ── customization point ───────────────────────────────────────────────────────
|
||||
// ── field annotations
|
||||
// ───────────────────────────────────────────────────────── Annotate
|
||||
// aggregate members to control (de)serialization:
|
||||
// struct User {
|
||||
// [[= Json::Rename{"user_id"}]] int id; // JSON key override
|
||||
// [[= Json::Skip{}]] int cache; // never
|
||||
// (de)serialized
|
||||
// [[= Json::Required{}]] std::string nm; // error if absent
|
||||
// [[= Json::Flatten{}]] Address addr; // inline nested
|
||||
// object
|
||||
// };
|
||||
struct Rename {
|
||||
Void::Meta::Str key;
|
||||
};
|
||||
struct Skip {
|
||||
};
|
||||
struct Required {
|
||||
};
|
||||
struct Flatten {
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
concept HasMemberToJson = requires(const T& t) {
|
||||
{ t.to_json() } -> std::same_as<Value>;
|
||||
};
|
||||
// ── customization point
|
||||
// ───────────────────────────────────────────────────────
|
||||
|
||||
template <typename T>
|
||||
concept HasMemberFromJson = requires(const Value& v) {
|
||||
{ T::from_json(v) } -> std::same_as<std::expected<T, ReflectError>>;
|
||||
};
|
||||
template <typename T>
|
||||
concept HasMemberToJson = requires(const T& t) {
|
||||
{ t.to_json() } -> std::same_as<Value>;
|
||||
};
|
||||
|
||||
// ── supported leaf / container shapes ─────────────────────────────────────────
|
||||
template <typename T>
|
||||
concept HasMemberFromJson = requires(const Value& v) {
|
||||
{ T::from_json(v) } -> std::same_as<std::expected<T, ReflectError>>;
|
||||
};
|
||||
|
||||
namespace ReflectDetail
|
||||
{
|
||||
template <typename T>
|
||||
struct IsOptional : std::false_type {};
|
||||
template <typename U>
|
||||
struct IsOptional<std::optional<U>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
struct IsVector : std::false_type {};
|
||||
template <typename U>
|
||||
struct IsVector<std::vector<U>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
struct IsStringMap : std::false_type {};
|
||||
template <typename U>
|
||||
struct IsStringMap<std::map<std::string, U>> : std::true_type {};
|
||||
} // namespace ReflectDetail
|
||||
// ── supported leaf / container shapes (single source: Void::Meta)
|
||||
// ─────────────
|
||||
|
||||
#ifdef VOID_HAS_JSON_REFLECTION
|
||||
|
||||
template <typename T>
|
||||
concept ReflectableAggregate =
|
||||
std::is_aggregate_v<T> && !std::is_array_v<T> &&
|
||||
std::is_default_constructible_v<T>;
|
||||
template <typename T>
|
||||
concept ReflectableAggregate =
|
||||
std::is_aggregate_v<T> && !std::is_array_v<T> &&
|
||||
std::is_default_constructible_v<T>;
|
||||
|
||||
#else
|
||||
|
||||
template <typename T>
|
||||
concept ReflectableAggregate = false;
|
||||
template <typename T>
|
||||
concept ReflectableAggregate = false;
|
||||
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
concept JsonConvertible =
|
||||
HasMemberToJson<T> || std::same_as<T, bool> || std::is_arithmetic_v<T> ||
|
||||
std::is_enum_v<T> || std::same_as<T, std::string> ||
|
||||
ReflectDetail::IsOptional<T>::value || ReflectDetail::IsVector<T>::value ||
|
||||
ReflectDetail::IsStringMap<T>::value || ReflectableAggregate<T>;
|
||||
template <typename T>
|
||||
concept JsonConvertible =
|
||||
HasMemberToJson<T> || std::same_as<T, bool> ||
|
||||
std::is_arithmetic_v<T> || std::is_enum_v<T> ||
|
||||
std::same_as<T, std::string> || Void::Meta::is_optional_v<T> ||
|
||||
Void::Meta::is_vector_v<T> || Void::Meta::is_string_map_v<T> ||
|
||||
Void::Meta::is_duration_v<T> || Void::Meta::is_time_point_v<T> ||
|
||||
Void::Meta::is_variant_v<T> || Void::Meta::is_std_array_v<T> ||
|
||||
Void::Meta::is_map_v<T> || ReflectableAggregate<T>;
|
||||
|
||||
// Anything serializable through this header (customized or reflected).
|
||||
template <typename T>
|
||||
concept Reflectable = JsonConvertible<T>;
|
||||
// Anything serializable through this header (customized or reflected).
|
||||
template <typename T>
|
||||
concept Reflectable = JsonConvertible<T>;
|
||||
|
||||
// ── to_json ───────────────────────────────────────────────────────────────────
|
||||
// ── to_json
|
||||
// ───────────────────────────────────────────────────────────────────
|
||||
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] Value to_json(const T& v);
|
||||
template <Reflectable T> [[nodiscard]] Value to_json(const T& v);
|
||||
|
||||
namespace ReflectDetail
|
||||
{
|
||||
template <typename T>
|
||||
Value leaf_to_json(const T& v)
|
||||
{
|
||||
if constexpr (HasMemberToJson<T>) {
|
||||
return v.to_json();
|
||||
}
|
||||
else if constexpr (std::same_as<T, bool>) {
|
||||
return Value{v};
|
||||
}
|
||||
else if constexpr (std::is_enum_v<T>) {
|
||||
namespace ReflectDetail
|
||||
{
|
||||
template <typename T> Value leaf_to_json(const T& v)
|
||||
{
|
||||
if constexpr (HasMemberToJson<T>) {
|
||||
return v.to_json();
|
||||
} else if constexpr (std::same_as<T, bool>) {
|
||||
return Value{v};
|
||||
} else if constexpr (std::is_enum_v<T>) {
|
||||
#ifdef VOID_HAS_ENUM_REFLECTION
|
||||
// Enum values serialize by name — stable across binary versions.
|
||||
return Value{Void::Detail::enum_name(v)};
|
||||
// Enum values serialize by name — stable across binary
|
||||
// versions.
|
||||
return Value{Void::Detail::enum_name(v)};
|
||||
#else
|
||||
return Value{static_cast<double>(
|
||||
static_cast<std::underlying_type_t<T>>(v))};
|
||||
return Value{static_cast<double>(
|
||||
static_cast<std::underlying_type_t<T>>(v))};
|
||||
#endif
|
||||
}
|
||||
else if constexpr (std::is_arithmetic_v<T>) {
|
||||
return Value{static_cast<double>(v)};
|
||||
}
|
||||
else if constexpr (std::same_as<T, std::string>) {
|
||||
return Value{v};
|
||||
}
|
||||
else if constexpr (IsOptional<T>::value) {
|
||||
return v ? Json::to_json(*v) : Value{nullptr};
|
||||
}
|
||||
else if constexpr (IsVector<T>::value) {
|
||||
auto arr = Json::array();
|
||||
arr.as_array().reserve(v.size());
|
||||
for (const auto& e : v) {
|
||||
arr.as_array().push_back(Json::to_json(e));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
else if constexpr (IsStringMap<T>::value) {
|
||||
auto obj = Json::object();
|
||||
for (const auto& [k, e] : v) {
|
||||
obj.as_object().emplace_back(k, Json::to_json(e));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
else {
|
||||
static_assert(ReflectableAggregate<T>,
|
||||
"type is not serializable via Json reflection");
|
||||
} else if constexpr (std::is_arithmetic_v<T>) {
|
||||
return Value{static_cast<double>(v)};
|
||||
} else if constexpr (std::same_as<T, std::string>) {
|
||||
return Value{v};
|
||||
} else if constexpr (Void::Meta::is_optional_v<T>) {
|
||||
return v ? Json::to_json(*v) : Value{nullptr};
|
||||
} else if constexpr (Void::Meta::is_vector_v<T>) {
|
||||
auto arr = Json::array();
|
||||
arr.as_array().reserve(v.size());
|
||||
for (const auto& e : v) {
|
||||
arr.as_array().push_back(Json::to_json(e));
|
||||
}
|
||||
return arr;
|
||||
} else if constexpr (Void::Meta::is_string_map_v<T>) {
|
||||
auto obj = Json::object();
|
||||
for (const auto& [k, e] : v) {
|
||||
obj.as_object().emplace_back(k, Json::to_json(e));
|
||||
}
|
||||
return obj;
|
||||
} else if constexpr (Void::Meta::is_duration_v<T>) {
|
||||
return Value{Void::Time::duration_to_string(v)};
|
||||
} else if constexpr (Void::Meta::is_time_point_v<T>) {
|
||||
// Encode as milliseconds-since-epoch (e.g. "1750000000000ms").
|
||||
// Millisecond magnitude stays exact through the double-based
|
||||
// parse_duration, where a native-nanosecond count would not.
|
||||
return Value{Void::Time::duration_to_string(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
v.time_since_epoch()))};
|
||||
} else if constexpr (Void::Meta::is_std_array_v<T>) {
|
||||
auto arr = Json::array();
|
||||
arr.as_array().reserve(v.size());
|
||||
for (const auto& e : v) {
|
||||
arr.as_array().push_back(Json::to_json(e));
|
||||
}
|
||||
return arr;
|
||||
} else if constexpr (Void::Meta::is_map_v<T>) {
|
||||
// Non-string-keyed map: array of {key, value} objects.
|
||||
auto arr = Json::array();
|
||||
for (const auto& [k, e] : v) {
|
||||
auto pair = Json::object();
|
||||
pair.as_object().emplace_back("key", Json::to_json(k));
|
||||
pair.as_object().emplace_back("value", Json::to_json(e));
|
||||
arr.as_array().push_back(std::move(pair));
|
||||
}
|
||||
return arr;
|
||||
} else if constexpr (Void::Meta::is_variant_v<T>) {
|
||||
return std::visit(
|
||||
[](const auto& alt) -> Value {
|
||||
using Alt = std::decay_t<decltype(alt)>;
|
||||
auto obj = Json::object();
|
||||
obj.as_object().emplace_back(
|
||||
"type", Value{Void::Meta::type_name<Alt>()});
|
||||
obj.as_object().emplace_back("value",
|
||||
Json::to_json(alt));
|
||||
return obj;
|
||||
},
|
||||
v);
|
||||
} else {
|
||||
static_assert(ReflectableAggregate<T>,
|
||||
"type is not serializable via Json reflection");
|
||||
#ifdef VOID_HAS_JSON_REFLECTION
|
||||
auto obj = Json::object();
|
||||
static constexpr auto members = std::define_static_array(
|
||||
std::meta::nonstatic_data_members_of(
|
||||
^^T, std::meta::access_context::current()));
|
||||
template for (constexpr auto m : members) {
|
||||
obj.as_object().emplace_back(
|
||||
std::string{std::meta::identifier_of(m)},
|
||||
Json::to_json(v.[:m:]));
|
||||
}
|
||||
return obj;
|
||||
auto obj = Json::object();
|
||||
template for (constexpr auto m : Void::Meta::members_v<T>)
|
||||
{
|
||||
if constexpr (!Void::Meta::has_annotation<Skip>(m)) {
|
||||
if constexpr (Void::Meta::has_annotation<Flatten>(m)) {
|
||||
// Inline the nested object's keys into this object.
|
||||
auto sub = Json::to_json(v.[:m:]);
|
||||
if (sub.is_object()) {
|
||||
for (auto& kv : sub.as_object()) {
|
||||
obj.as_object().emplace_back(
|
||||
kv.first, std::move(kv.second));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
constexpr auto rn =
|
||||
Void::Meta::annotation_of<Rename>(m);
|
||||
std::string key =
|
||||
rn.has_value()
|
||||
? std::string{rn->key.view()}
|
||||
: std::string{std::meta::identifier_of(m)};
|
||||
obj.as_object().emplace_back(
|
||||
std::move(key), Json::to_json(v.[:m:]));
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} // namespace ReflectDetail
|
||||
}
|
||||
}
|
||||
} // namespace ReflectDetail
|
||||
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] Value to_json(const T& v)
|
||||
{
|
||||
return ReflectDetail::leaf_to_json(v);
|
||||
}
|
||||
template <Reflectable T> [[nodiscard]] Value to_json(const T& v)
|
||||
{
|
||||
return ReflectDetail::leaf_to_json(v);
|
||||
}
|
||||
|
||||
// ── from_json ─────────────────────────────────────────────────────────────────
|
||||
// ── from_json
|
||||
// ─────────────────────────────────────────────────────────────────
|
||||
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] std::expected<T, ReflectError> from_json(const Value& j);
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] std::expected<T, ReflectError> from_json(const Value& j);
|
||||
|
||||
namespace ReflectDetail
|
||||
{
|
||||
[[nodiscard]] inline ReflectError nest(std::string_view field,
|
||||
ReflectError inner)
|
||||
{
|
||||
if (inner.field.empty()) {
|
||||
inner.field = std::string{field};
|
||||
}
|
||||
else {
|
||||
inner.field = std::string{field} + "." + inner.field;
|
||||
}
|
||||
return inner;
|
||||
}
|
||||
namespace ReflectDetail
|
||||
{
|
||||
[[nodiscard]] inline ReflectError nest(std::string_view field,
|
||||
ReflectError inner)
|
||||
{
|
||||
if (inner.field.empty()) {
|
||||
inner.field = std::string{field};
|
||||
} else {
|
||||
inner.field = std::string{field} + "." + inner.field;
|
||||
}
|
||||
return inner;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::expected<T, ReflectError> leaf_from_json(const Value& j)
|
||||
{
|
||||
if constexpr (HasMemberFromJson<T>) {
|
||||
return T::from_json(j);
|
||||
}
|
||||
else if constexpr (std::same_as<T, bool>) {
|
||||
if (!j.is_bool()) {
|
||||
return std::unexpected{ReflectError{"", "expected bool"}};
|
||||
}
|
||||
return j.as_bool();
|
||||
}
|
||||
else if constexpr (std::is_enum_v<T>) {
|
||||
template <typename T>
|
||||
std::expected<T, ReflectError> leaf_from_json(const Value& j)
|
||||
{
|
||||
if constexpr (HasMemberFromJson<T>) {
|
||||
return T::from_json(j);
|
||||
} else if constexpr (std::same_as<T, bool>) {
|
||||
if (!j.is_bool()) {
|
||||
return std::unexpected{ReflectError{"", "expected bool"}};
|
||||
}
|
||||
return j.as_bool();
|
||||
} else if constexpr (std::is_enum_v<T>) {
|
||||
#ifdef VOID_HAS_ENUM_REFLECTION
|
||||
if (!j.is_string()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected enum name string"}};
|
||||
}
|
||||
auto parsed = Void::Detail::enum_parse<T>(j.as_string());
|
||||
if (!parsed) {
|
||||
return std::unexpected{ReflectError{
|
||||
"", "unknown enumerator '" + j.as_string() + "'"}};
|
||||
}
|
||||
return *parsed;
|
||||
if (!j.is_string()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected enum name string"}};
|
||||
}
|
||||
auto parsed = Void::Detail::enum_parse<T>(j.as_string());
|
||||
if (!parsed) {
|
||||
return std::unexpected{ReflectError{
|
||||
"", "unknown enumerator '" + j.as_string() + "'"}};
|
||||
}
|
||||
return *parsed;
|
||||
#else
|
||||
if (!j.is_number()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected enum number"}};
|
||||
}
|
||||
return static_cast<T>(
|
||||
static_cast<std::underlying_type_t<T>>(j.as_number()));
|
||||
if (!j.is_number()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected enum number"}};
|
||||
}
|
||||
return static_cast<T>(
|
||||
static_cast<std::underlying_type_t<T>>(j.as_number()));
|
||||
#endif
|
||||
}
|
||||
else if constexpr (std::is_arithmetic_v<T>) {
|
||||
if (!j.is_number()) {
|
||||
return std::unexpected{ReflectError{"", "expected number"}};
|
||||
}
|
||||
return static_cast<T>(j.as_number());
|
||||
}
|
||||
else if constexpr (std::same_as<T, std::string>) {
|
||||
if (!j.is_string()) {
|
||||
return std::unexpected{ReflectError{"", "expected string"}};
|
||||
}
|
||||
return j.as_string();
|
||||
}
|
||||
else if constexpr (IsOptional<T>::value) {
|
||||
using U = typename T::value_type;
|
||||
if (j.is_null()) {
|
||||
return T{std::nullopt};
|
||||
}
|
||||
auto inner = Json::from_json<U>(j);
|
||||
if (!inner) {
|
||||
return std::unexpected{std::move(inner).error()};
|
||||
}
|
||||
return T{std::move(*inner)};
|
||||
}
|
||||
else if constexpr (IsVector<T>::value) {
|
||||
using U = typename T::value_type;
|
||||
if (!j.is_array()) {
|
||||
return std::unexpected{ReflectError{"", "expected array"}};
|
||||
}
|
||||
T out;
|
||||
out.reserve(j.as_array().size());
|
||||
std::size_t index = 0;
|
||||
for (const auto& e : j.as_array()) {
|
||||
auto inner = Json::from_json<U>(e);
|
||||
if (!inner) {
|
||||
return std::unexpected{nest(
|
||||
"[" + std::to_string(index) + "]",
|
||||
std::move(inner).error())};
|
||||
}
|
||||
out.push_back(std::move(*inner));
|
||||
++index;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
else if constexpr (IsStringMap<T>::value) {
|
||||
using U = typename T::mapped_type;
|
||||
if (!j.is_object()) {
|
||||
return std::unexpected{ReflectError{"", "expected object"}};
|
||||
}
|
||||
T out;
|
||||
for (const auto& [k, e] : j.as_object()) {
|
||||
auto inner = Json::from_json<U>(e);
|
||||
if (!inner) {
|
||||
return std::unexpected{nest(k, std::move(inner).error())};
|
||||
}
|
||||
out.emplace(k, std::move(*inner));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
else {
|
||||
static_assert(ReflectableAggregate<T>,
|
||||
"type is not deserializable via Json reflection");
|
||||
} else if constexpr (std::is_arithmetic_v<T>) {
|
||||
if (!j.is_number()) {
|
||||
return std::unexpected{ReflectError{"", "expected number"}};
|
||||
}
|
||||
return static_cast<T>(j.as_number());
|
||||
} else if constexpr (std::same_as<T, std::string>) {
|
||||
if (!j.is_string()) {
|
||||
return std::unexpected{ReflectError{"", "expected string"}};
|
||||
}
|
||||
return j.as_string();
|
||||
} else if constexpr (Void::Meta::is_optional_v<T>) {
|
||||
using U = typename T::value_type;
|
||||
if (j.is_null()) {
|
||||
return T{std::nullopt};
|
||||
}
|
||||
auto inner = Json::from_json<U>(j);
|
||||
if (!inner) {
|
||||
return std::unexpected{std::move(inner).error()};
|
||||
}
|
||||
return T{std::move(*inner)};
|
||||
} else if constexpr (Void::Meta::is_vector_v<T>) {
|
||||
using U = typename T::value_type;
|
||||
if (!j.is_array()) {
|
||||
return std::unexpected{ReflectError{"", "expected array"}};
|
||||
}
|
||||
T out;
|
||||
out.reserve(j.as_array().size());
|
||||
std::size_t index = 0;
|
||||
for (const auto& e : j.as_array()) {
|
||||
auto inner = Json::from_json<U>(e);
|
||||
if (!inner) {
|
||||
return std::unexpected{
|
||||
nest("[" + std::to_string(index) + "]",
|
||||
std::move(inner).error())};
|
||||
}
|
||||
out.push_back(std::move(*inner));
|
||||
++index;
|
||||
}
|
||||
return out;
|
||||
} else if constexpr (Void::Meta::is_string_map_v<T>) {
|
||||
using U = typename T::mapped_type;
|
||||
if (!j.is_object()) {
|
||||
return std::unexpected{ReflectError{"", "expected object"}};
|
||||
}
|
||||
T out;
|
||||
for (const auto& [k, e] : j.as_object()) {
|
||||
auto inner = Json::from_json<U>(e);
|
||||
if (!inner) {
|
||||
return std::unexpected{
|
||||
nest(k, std::move(inner).error())};
|
||||
}
|
||||
out.emplace(k, std::move(*inner));
|
||||
}
|
||||
return out;
|
||||
} else if constexpr (Void::Meta::is_duration_v<T>) {
|
||||
if (!j.is_string()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected duration string"}};
|
||||
}
|
||||
auto parsed = Void::Time::parse_duration(j.as_string());
|
||||
if (!parsed) {
|
||||
return std::unexpected{ReflectError{
|
||||
"", "invalid duration '" + j.as_string() + "'"}};
|
||||
}
|
||||
return std::chrono::duration_cast<T>(*parsed);
|
||||
} else if constexpr (Void::Meta::is_time_point_v<T>) {
|
||||
if (!j.is_string()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected time_point string"}};
|
||||
}
|
||||
auto parsed = Void::Time::parse_duration(j.as_string());
|
||||
if (!parsed) {
|
||||
return std::unexpected{ReflectError{
|
||||
"", "invalid time_point '" + j.as_string() + "'"}};
|
||||
}
|
||||
return T{std::chrono::duration_cast<typename T::duration>(
|
||||
*parsed)};
|
||||
} else if constexpr (Void::Meta::is_std_array_v<T>) {
|
||||
using U = typename T::value_type;
|
||||
if (!j.is_array()) {
|
||||
return std::unexpected{ReflectError{"", "expected array"}};
|
||||
}
|
||||
constexpr std::size_t N = std::tuple_size_v<T>;
|
||||
if (j.as_array().size() != N) {
|
||||
return std::unexpected{ReflectError{
|
||||
"", "expected array of length " + std::to_string(N)}};
|
||||
}
|
||||
T out{};
|
||||
std::size_t index = 0;
|
||||
for (const auto& e : j.as_array()) {
|
||||
auto inner = Json::from_json<U>(e);
|
||||
if (!inner) {
|
||||
return std::unexpected{
|
||||
nest("[" + std::to_string(index) + "]",
|
||||
std::move(inner).error())};
|
||||
}
|
||||
out[index] = std::move(*inner);
|
||||
++index;
|
||||
}
|
||||
return out;
|
||||
} else if constexpr (Void::Meta::is_map_v<T>) {
|
||||
using K = typename T::key_type;
|
||||
using MV = typename T::mapped_type;
|
||||
if (!j.is_array()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected array of key/value pairs"}};
|
||||
}
|
||||
T out;
|
||||
std::size_t index = 0;
|
||||
for (const auto& e : j.as_array()) {
|
||||
const auto where = "[" + std::to_string(index) + "]";
|
||||
const Value* kf = e.is_object() ? e.find("key") : nullptr;
|
||||
const Value* vf = e.is_object() ? e.find("value") : nullptr;
|
||||
if (!kf || !vf) {
|
||||
return std::unexpected{nest(
|
||||
where,
|
||||
ReflectError{"", "expected {key, value} object"})};
|
||||
}
|
||||
auto key = Json::from_json<K>(*kf);
|
||||
if (!key) {
|
||||
return std::unexpected{
|
||||
nest(where, std::move(key).error())};
|
||||
}
|
||||
auto val = Json::from_json<MV>(*vf);
|
||||
if (!val) {
|
||||
return std::unexpected{
|
||||
nest(where, std::move(val).error())};
|
||||
}
|
||||
out.emplace(std::move(*key), std::move(*val));
|
||||
++index;
|
||||
}
|
||||
return out;
|
||||
} else if constexpr (Void::Meta::is_variant_v<T>) {
|
||||
if (!j.is_object()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected tagged variant object"}};
|
||||
}
|
||||
const Value* tf = j.find("type");
|
||||
const Value* vf = j.find("value");
|
||||
if (!tf || !tf->is_string()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "variant missing 'type' tag"}};
|
||||
}
|
||||
if (!vf) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "variant missing 'value'"}};
|
||||
}
|
||||
const std::string tag = tf->as_string();
|
||||
std::optional<T> result;
|
||||
std::optional<ReflectError> failure;
|
||||
[&]<std::size_t... Is>(std::index_sequence<Is...>) {
|
||||
(
|
||||
[&] {
|
||||
if (result || failure) {
|
||||
return;
|
||||
}
|
||||
using Alt = std::variant_alternative_t<Is, T>;
|
||||
if (Void::Meta::type_name<Alt>() == tag) {
|
||||
auto inner = Json::from_json<Alt>(*vf);
|
||||
if (!inner) {
|
||||
failure = std::move(inner).error();
|
||||
} else {
|
||||
result = T{std::in_place_index<Is>,
|
||||
std::move(*inner)};
|
||||
}
|
||||
}
|
||||
}(),
|
||||
...);
|
||||
}(std::make_index_sequence<std::variant_size_v<T>>{});
|
||||
if (failure) {
|
||||
return std::unexpected{std::move(*failure)};
|
||||
}
|
||||
if (!result) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "unknown variant type '" + tag + "'"}};
|
||||
}
|
||||
return std::move(*result);
|
||||
} else {
|
||||
static_assert(ReflectableAggregate<T>,
|
||||
"type is not deserializable via Json reflection");
|
||||
#ifdef VOID_HAS_JSON_REFLECTION
|
||||
if (!j.is_object()) {
|
||||
return std::unexpected{ReflectError{"", "expected object"}};
|
||||
}
|
||||
T out{};
|
||||
static constexpr auto members = std::define_static_array(
|
||||
std::meta::nonstatic_data_members_of(
|
||||
^^T, std::meta::access_context::current()));
|
||||
std::optional<ReflectError> failure;
|
||||
template for (constexpr auto m : members) {
|
||||
if (!failure) {
|
||||
constexpr auto name = std::meta::identifier_of(m);
|
||||
using MT = [:std::meta::type_of(m):];
|
||||
if (const Value* field = j.find(name)) {
|
||||
auto inner = Json::from_json<MT>(*field);
|
||||
if (!inner) {
|
||||
failure = nest(name, std::move(inner).error());
|
||||
}
|
||||
else {
|
||||
out.[:m:] = std::move(*inner);
|
||||
}
|
||||
}
|
||||
// absent field: keep the default-initialized value
|
||||
}
|
||||
}
|
||||
if (failure) {
|
||||
return std::unexpected{std::move(*failure)};
|
||||
}
|
||||
return out;
|
||||
if (!j.is_object()) {
|
||||
return std::unexpected{ReflectError{"", "expected object"}};
|
||||
}
|
||||
T out{};
|
||||
std::optional<ReflectError> failure;
|
||||
template for (constexpr auto m : Void::Meta::members_v<T>)
|
||||
{
|
||||
if (!failure) {
|
||||
if constexpr (!Void::Meta::has_annotation<Skip>(m)) {
|
||||
using MT = [:std::meta::type_of(m):];
|
||||
if constexpr (Void::Meta::has_annotation<Flatten>(
|
||||
m)) {
|
||||
// Read the nested object's keys from this
|
||||
// object.
|
||||
auto inner = Json::from_json<MT>(j);
|
||||
if (!inner) {
|
||||
failure = std::move(inner).error();
|
||||
} else {
|
||||
out.[:m:] = std::move(*inner);
|
||||
}
|
||||
} else {
|
||||
constexpr auto rn =
|
||||
Void::Meta::annotation_of<Rename>(m);
|
||||
const std::string key =
|
||||
rn.has_value()
|
||||
? std::string{rn->key.view()}
|
||||
: std::string{
|
||||
std::meta::identifier_of(m)};
|
||||
if (const Value* field = j.find(key)) {
|
||||
auto inner = Json::from_json<MT>(*field);
|
||||
if (!inner) {
|
||||
failure =
|
||||
nest(key, std::move(inner).error());
|
||||
} else {
|
||||
out.[:m:] = std::move(*inner);
|
||||
}
|
||||
} else if constexpr (Void::Meta::has_annotation<
|
||||
Required>(m)) {
|
||||
failure = ReflectError{
|
||||
key, "required field is missing"};
|
||||
}
|
||||
// absent + not required: keep the default value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (failure) {
|
||||
return std::unexpected{std::move(*failure)};
|
||||
}
|
||||
return out;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} // namespace ReflectDetail
|
||||
}
|
||||
}
|
||||
} // namespace ReflectDetail
|
||||
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] std::expected<T, ReflectError> from_json(const Value& j)
|
||||
{
|
||||
return ReflectDetail::leaf_from_json<T>(j);
|
||||
}
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] std::expected<T, ReflectError> from_json(const Value& j)
|
||||
{
|
||||
return ReflectDetail::leaf_from_json<T>(j);
|
||||
}
|
||||
|
||||
// ── versioned envelope + migrations ───────────────────────────────────────────
|
||||
// ── versioned envelope + migrations
|
||||
// ───────────────────────────────────────────
|
||||
|
||||
// Specialize to bump a state type's schema version:
|
||||
// template <> struct Void::Json::StateTraits<MyState> {
|
||||
// static constexpr std::uint32_t version = 3;
|
||||
// };
|
||||
template <typename T>
|
||||
struct StateTraits {
|
||||
static constexpr std::uint32_t version = 1;
|
||||
};
|
||||
// Specialize to bump a state type's schema version:
|
||||
// template <> struct Void::Json::StateTraits<MyState> {
|
||||
// static constexpr std::uint32_t version = 3;
|
||||
// };
|
||||
template <typename T> struct StateTraits {
|
||||
static constexpr std::uint32_t version = 1;
|
||||
};
|
||||
|
||||
// Ordered chain of per-version JSON transforms. Each step upgrades the bare
|
||||
// "data" payload from `from_version` to `from_version + 1`.
|
||||
class MigrationChain
|
||||
{
|
||||
public:
|
||||
using Step = std::function<Value(Value)>;
|
||||
// Ordered chain of per-version JSON transforms. Each step upgrades the bare
|
||||
// "data" payload from `from_version` to `from_version + 1`.
|
||||
class MigrationChain
|
||||
{
|
||||
public:
|
||||
|
||||
MigrationChain& add(std::uint32_t from_version, Step step)
|
||||
{
|
||||
steps_.emplace(from_version, std::move(step));
|
||||
return *this;
|
||||
}
|
||||
using State = std::function<Value(Value)>;
|
||||
|
||||
[[nodiscard]] std::expected<Value, ReflectError>
|
||||
migrate(Value data, std::uint32_t from, std::uint32_t to) const
|
||||
{
|
||||
while (from < to) {
|
||||
auto it = steps_.find(from);
|
||||
if (it == steps_.end()) {
|
||||
return std::unexpected{ReflectError{
|
||||
"__version", "no migration step from version " +
|
||||
std::to_string(from)}};
|
||||
}
|
||||
data = it->second(std::move(data));
|
||||
++from;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
MigrationChain& add(std::uint32_t from_version, State step)
|
||||
{
|
||||
steps_.emplace(from_version, std::move(step));
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool empty() const noexcept { return steps_.empty(); }
|
||||
[[nodiscard]] std::expected<Value, ReflectError>
|
||||
migrate(Value data, std::uint32_t from, std::uint32_t to) const
|
||||
{
|
||||
while (from < to) {
|
||||
auto it = steps_.find(from);
|
||||
if (it == steps_.end()) {
|
||||
return std::unexpected{ReflectError{
|
||||
"__version", "no migration step from version " +
|
||||
std::to_string(from)}};
|
||||
}
|
||||
data = it->second(std::move(data));
|
||||
++from;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::uint32_t, Step> steps_;
|
||||
};
|
||||
[[nodiscard]] bool empty() const noexcept { return steps_.empty(); }
|
||||
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] Value to_versioned_json(const T& v)
|
||||
{
|
||||
auto envelope = object();
|
||||
envelope.as_object().emplace_back(
|
||||
"__version", Value{static_cast<double>(StateTraits<T>::version)});
|
||||
envelope.as_object().emplace_back("data", to_json(v));
|
||||
return envelope;
|
||||
}
|
||||
private:
|
||||
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] std::expected<T, ReflectError>
|
||||
from_versioned_json(const Value& envelope,
|
||||
const MigrationChain& migrations = {})
|
||||
{
|
||||
if (!envelope.is_object()) {
|
||||
return std::unexpected{ReflectError{"", "expected envelope object"}};
|
||||
}
|
||||
const Value* version = envelope.find("__version");
|
||||
const Value* data = envelope.find("data");
|
||||
if (!version || !version->is_number() || !data) {
|
||||
return std::unexpected{
|
||||
ReflectError{"__version", "malformed versioned envelope"}};
|
||||
}
|
||||
const auto from = static_cast<std::uint32_t>(version->as_number());
|
||||
constexpr auto to = StateTraits<T>::version;
|
||||
if (from > to) {
|
||||
return std::unexpected{ReflectError{
|
||||
"__version", "stored version " + std::to_string(from) +
|
||||
" is newer than supported version " +
|
||||
std::to_string(to)}};
|
||||
}
|
||||
if (from == to) {
|
||||
return from_json<T>(*data);
|
||||
}
|
||||
auto migrated = migrations.migrate(*data, from, to);
|
||||
if (!migrated) {
|
||||
return std::unexpected{std::move(migrated).error()};
|
||||
}
|
||||
return from_json<T>(*migrated);
|
||||
}
|
||||
std::map<std::uint32_t, State> steps_;
|
||||
};
|
||||
|
||||
template <Reflectable T> [[nodiscard]] Value to_versioned_json(const T& v)
|
||||
{
|
||||
auto envelope = object();
|
||||
envelope.as_object().emplace_back(
|
||||
"__version", Value{static_cast<double>(StateTraits<T>::version)});
|
||||
envelope.as_object().emplace_back("data", to_json(v));
|
||||
return envelope;
|
||||
}
|
||||
|
||||
template <Reflectable T>
|
||||
[[nodiscard]] std::expected<T, ReflectError>
|
||||
from_versioned_json(const Value& envelope,
|
||||
const MigrationChain& migrations = {})
|
||||
{
|
||||
if (!envelope.is_object()) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected envelope object"}};
|
||||
}
|
||||
const Value* version = envelope.find("__version");
|
||||
const Value* data = envelope.find("data");
|
||||
if (!version || !version->is_number() || !data) {
|
||||
return std::unexpected{
|
||||
ReflectError{"__version", "malformed versioned envelope"}};
|
||||
}
|
||||
const auto from = static_cast<std::uint32_t>(version->as_number());
|
||||
constexpr auto to = StateTraits<T>::version;
|
||||
if (from > to) {
|
||||
return std::unexpected{ReflectError{
|
||||
"__version", "stored version " + std::to_string(from) +
|
||||
" is newer than supported version " +
|
||||
std::to_string(to)}};
|
||||
}
|
||||
if (from == to) {
|
||||
return from_json<T>(*data);
|
||||
}
|
||||
auto migrated = migrations.migrate(*data, from, to);
|
||||
if (!migrated) {
|
||||
return std::unexpected{std::move(migrated).error()};
|
||||
}
|
||||
return from_json<T>(*migrated);
|
||||
}
|
||||
|
||||
} // namespace Void::Json
|
||||
Reference in new issue
Block a user