189 lines
5.6 KiB
C++
189 lines
5.6 KiB
C++
#pragma once
|
|
|
|
#include <Void/Configuration/Config.hpp>
|
|
#include <Void/Core/Meta.hpp>
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <chrono>
|
|
#include <format>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
#if __has_include(<meta>)
|
|
#include <meta>
|
|
#define VOID_HAS_CONFIG_REFLECTION 1
|
|
#endif
|
|
|
|
// ── Reflection config binding
|
|
// ───────────────────────────────────────────────── Fill an aggregate struct
|
|
// from a Config using P2996 reflection: each data member binds to the dotted
|
|
// key matching its name (under an optional prefix); nested aggregates extend
|
|
// the prefix. The default-initialized member values double as the schema's
|
|
// defaults — a missing key keeps the default, a present key that fails
|
|
// conversion is an error.
|
|
//
|
|
// struct LogSettings {
|
|
// std::string level = "info";
|
|
// bool async = true;
|
|
// std::chrono::milliseconds flush_every{250};
|
|
// };
|
|
// auto s = Void::Configuration::bind<LogSettings>(cfg, "log");
|
|
//
|
|
// Supported member types: everything Config::get_as<T> accepts (string,
|
|
// bool, arithmetic, enums, chrono durations), std::optional<leaf>,
|
|
// std::vector<leaf> (keys "name.0", "name.1", ...), nested aggregates.
|
|
|
|
namespace Void::Configuration
|
|
{
|
|
|
|
// ── field annotations ─────────────────────────────────────────────────
|
|
// Annotate aggregate members to control binding:
|
|
// struct Settings {
|
|
// [[= Configuration::Key{"log_level"}]] std::string level = "info";
|
|
// [[= Configuration::Required{}]] std::string host;
|
|
// };
|
|
struct Key {
|
|
Void::Meta::Str name;
|
|
};
|
|
struct Required {
|
|
};
|
|
struct Secret {
|
|
};
|
|
struct RestartRequired {
|
|
};
|
|
struct Deprecated {
|
|
Void::Meta::Str message;
|
|
};
|
|
struct IntRange {
|
|
std::int64_t min{0};
|
|
std::int64_t max{0};
|
|
};
|
|
struct RealRange {
|
|
double min{0.0};
|
|
double max{0.0};
|
|
};
|
|
|
|
#ifdef VOID_HAS_CONFIG_REFLECTION
|
|
|
|
namespace BindDetail
|
|
{
|
|
// Leaf / container shape detection comes from Void::Meta (single
|
|
// source); Config additionally treats chrono durations as leaves.
|
|
template <typename T>
|
|
concept Leaf = std::is_same_v<T, std::string> ||
|
|
std::is_same_v<T, bool> || std::is_arithmetic_v<T> ||
|
|
std::is_enum_v<T> || Void::Meta::is_duration_v<T>;
|
|
|
|
template <typename T>
|
|
concept BindableAggregate =
|
|
std::is_aggregate_v<T> && !std::is_array_v<T> &&
|
|
std::is_default_constructible_v<T>;
|
|
|
|
[[nodiscard]] inline std::string join(std::string_view prefix,
|
|
std::string_view name)
|
|
{
|
|
if (prefix.empty()) {
|
|
return std::string{name};
|
|
}
|
|
return std::string{prefix} + "." + std::string{name};
|
|
}
|
|
|
|
template <BindableAggregate T>
|
|
Void::Result::Result<T, Void::Result::Error>
|
|
bind_into(const Config& cfg, std::string_view prefix)
|
|
{
|
|
T out{};
|
|
std::optional<Void::Result::Error> failure;
|
|
|
|
template for (constexpr auto m : Void::Meta::members_v<T>)
|
|
{
|
|
if (!failure) {
|
|
using MT = [:std::meta::type_of(m):];
|
|
// Key segment: a [[= Configuration::Key("…")]] override,
|
|
// else the field identifier.
|
|
constexpr auto ko = Void::Meta::annotation_of<Key>(m);
|
|
const std::string seg =
|
|
ko.has_value()
|
|
? std::string{ko->name.view()}
|
|
: std::string{std::meta::identifier_of(m)};
|
|
const auto key = join(prefix, seg);
|
|
[[maybe_unused]] constexpr bool required =
|
|
Void::Meta::has_annotation<Required>(m);
|
|
|
|
if constexpr (BindableAggregate<MT> && !Leaf<MT>) {
|
|
auto nested = bind_into<MT>(cfg, key);
|
|
if (!nested.has_value()) {
|
|
failure = std::move(nested).error();
|
|
} else {
|
|
out.[:m:] = std::move(*nested);
|
|
}
|
|
} else if constexpr (Void::Meta::is_optional_v<MT>) {
|
|
using U = typename MT::value_type;
|
|
if (cfg.contains(key)) {
|
|
auto v = cfg.get_as<U>(key);
|
|
if (!v.has_value()) {
|
|
failure = std::move(v).error();
|
|
} else {
|
|
out.[:m:] = std::move(*v);
|
|
}
|
|
}
|
|
} else if constexpr (Void::Meta::is_vector_v<MT>) {
|
|
using U = typename MT::value_type;
|
|
MT items;
|
|
for (std::size_t i = 0;; ++i) {
|
|
const auto item_key = key + "." + std::to_string(i);
|
|
if (!cfg.contains(item_key)) {
|
|
break;
|
|
}
|
|
auto v = cfg.get_as<U>(item_key);
|
|
if (!v.has_value()) {
|
|
failure = std::move(v).error();
|
|
break;
|
|
}
|
|
items.push_back(std::move(*v));
|
|
}
|
|
if (!failure && !items.empty()) {
|
|
out.[:m:] = std::move(items);
|
|
}
|
|
} else {
|
|
static_assert(Leaf<MT>,
|
|
"member type not bindable from Config");
|
|
if (cfg.contains(key)) {
|
|
auto v = cfg.get_as<MT>(key);
|
|
if (!v.has_value()) {
|
|
failure = std::move(v).error();
|
|
} else {
|
|
out.[:m:] = std::move(*v);
|
|
}
|
|
} else if constexpr (required) {
|
|
failure = Void::Result::Error{
|
|
"missing required config key '" + key + "'"};
|
|
}
|
|
// absent + not required: keep the default member value
|
|
}
|
|
}
|
|
}
|
|
|
|
if (failure) {
|
|
return Void::Result::fail(std::move(*failure));
|
|
}
|
|
return Void::Result::ok(std::move(out));
|
|
}
|
|
} // namespace BindDetail
|
|
|
|
/// Bind cfg's keys under `prefix` into a default-constructed T.
|
|
template <BindDetail::BindableAggregate T>
|
|
[[nodiscard]] Void::Result::Result<T, Void::Result::Error>
|
|
bind(const Config& cfg, std::string_view prefix = {})
|
|
{
|
|
return BindDetail::bind_into<T>(cfg, prefix);
|
|
}
|
|
|
|
#endif // VOID_HAS_CONFIG_REFLECTION
|
|
|
|
} // namespace Void::Configuration
|