From e11d1e952f9fd73d47247e064b878d5364db0436 Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Sun, 29 Nov 2026 10:44:22 -0800 Subject: [PATCH] feat(enums): add rfl::EnumNames per-enum wire-name trait (#106) Parent: `feat(enums): add rfl::TransformEnumNames processors (#731)` Adds the opt-in `rfl::EnumNames` specialization trait and `rfl::get_enum_wire_names()` utility so enums with non-identifier wire strings (e.g., `"c++20"`, `"nvq++"`, `"*"`), C++ keyword clashes, or enumerators outside the default reflected range can declare custom wire names across JSON, CLI, YAML, XML, Generic, and JSON Schema without duplicating enumerators as `rfl::Literal<...>`: - Supports static `overrides` (array or borrowed `overrides()` returning `std::span`, with `static_assert` rejecting dangling by-value containers) and per-enum `transform` (`EnumNameTransform` constant or custom callable). - `overrides` takes precedence over `transform`; when only `overrides` is specified, non-overridden enumerators still inherit any `rfl::TransformEnumNames<...>` processor passed to `read`/`write`/`to_schema`. - Zero-overhead `if constexpr` fast path when neither `EnumNames` nor `TransformEnumNames` is active; resolved tables are cached per `(EnumType, EnumNameTransform)`. - Public `rfl::get_enum_wire_names()` accepts variadic processors, and throwing custom `transform` callbacks are caught into `rfl::Error` across both `ParserEnum::read` and direct `rfl::string_to_enum`. Open design question for upstream review: whether the trait should live at `rfl::EnumNames` (matching `rfl::Reflector`) or `rfl::config::enum_names` (matching `rfl::config::enum_range` / `rfl::config::enum_descriptions`). Refs: https://github.com/munich-data-engineering/reflect-cpp/issues/106 (Maintainer: "something along these lines is possible") Downstream receipts: - NVIDIA/cuda-quantum hand-written Reflector tables for non-identifier wire strings (e.g., `c++20`, `nvq++`): https://github.com/NVIDIA/cuda-quantum/blob/81ef20d9d4a1e0912a6dbc68637ece017e8fd324/cudaq/lib/Target/Yaml/TargetConfigYamlAdapters.h#L27-L75 https://github.com/NVIDIA/cuda-quantum/blob/81ef20d9d4a1e0912a6dbc68637ece017e8fd324/cudaq/lib/Target/Yaml/v2/QuakeReferenceSemanticsProfileYaml.h#L16-L32 - openmoq/moqx custom action wire strings (`"*"` / domain verbs) & config enums: https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/auth/Action.h#L24-L102 https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/ConfigResolver.cpp#L35-L48 --- docs/content/enums.md | 23 ++++++ include/rfl.hpp | 1 + include/rfl/EnumNames.hpp | 150 ++++++++++++++++++++++++++++++++++++++ include/rfl/enums.hpp | 44 ++++++++--- 4 files changed, 206 insertions(+), 12 deletions(-) create mode 100644 include/rfl/EnumNames.hpp diff --git a/docs/content/enums.md b/docs/content/enums.md index 5ab5561af..4d972f20a 100644 --- a/docs/content/enums.md +++ b/docs/content/enums.md @@ -215,3 +215,26 @@ constexpr auto enumerator_array = rfl::get_enumerator_array(); In case it's more convenient to get the enumerator values as values of the enum's underlying type rather than values of the enum type itself, there is also `rfl::get_underlying_enumerators()` and `rfl::get_underlying_enumerator_array()`. + +## Custom wire names (`rfl::EnumNames` and `rfl::TransformEnumNames`) + +To customize serialized enum wire names across JSON, CLI, Generic, XML, YAML, and JSON Schema without duplicating enumerators as `rfl::Literal<...>`, specialize `rfl::EnumNames` and/or pass a `rfl::TransformEnumNames<...>` processor (`rfl::EnumSnakeCaseToKebabCase`, `rfl::EnumSnakeCaseToCamelCase`, `rfl::EnumSnakeCaseToPascalCase`, `rfl::EnumCamelCaseToSnakeCase`, `rfl::EnumToLowerCase`, `rfl::EnumToUpperCase`): + +```cpp +enum class OptimizationLevel { no_opt, size_opt, speed_opt, cxx_standard }; + +template <> +struct rfl::EnumNames { + static constexpr rfl::EnumNameTransform transform = + rfl::EnumNameTransform::snake_to_kebab; + static constexpr std::array, 1> + overrides = {{ + {OptimizationLevel::cxx_standard, "c++20"}, + }}; +}; +``` + +- `overrides` (either a static contiguous range or a static `overrides()` returning a borrowed contiguous range such as `std::span>`) takes precedence over `transform`, and also supports enumerators outside the default reflected range. +- `transform` can be either a `rfl::EnumNameTransform` constant or a static callable `transform(std::string_view)` returning `std::string` or `std::string_view`. +- If `rfl::EnumNames` only defines `overrides` (without `transform`), non-overridden enumerators still receive any `rfl::TransformEnumNames<...>` processor passed to `read`, `write`, or `to_schema`. +- Use `rfl::get_enum_wire_names()` to inspect resolved `(wire_name, enumerator)` pairs. diff --git a/include/rfl.hpp b/include/rfl.hpp index 7599c1611..aeaa40d74 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -23,6 +23,7 @@ #include "rfl/DefaultVal.hpp" #include "rfl/Description.hpp" #include "rfl/DurationsAsCount.hpp" +#include "rfl/EnumNames.hpp" #include "rfl/EnumNamesOnly.hpp" #include "rfl/ExtraFields.hpp" #include "rfl/FailFast.hpp" diff --git a/include/rfl/EnumNames.hpp b/include/rfl/EnumNames.hpp new file mode 100644 index 000000000..3a7a2d515 --- /dev/null +++ b/include/rfl/EnumNames.hpp @@ -0,0 +1,150 @@ +#ifndef RFL_ENUMNAMES_HPP_ +#define RFL_ENUMNAMES_HPP_ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Processors.hpp" +#include "TransformEnumNames.hpp" +#include "internal/enum_name_transform_v.hpp" + +namespace rfl { + +/// Pair of `(enumerator, wire_name)` for per-enumerator overrides in +/// `rfl::EnumNames`. +template +using EnumOverride = std::pair; + +/// Specialize `rfl::EnumNames` to customize wire names for enum `E` across +/// JSON, CLI, Generic, YAML, XML, and JSON Schema without duplicating +/// enumerators as `rfl::Literal<...>`. +/// +/// Supported members (individually or combined; `overrides` takes precedence): +/// - `static constexpr EnumNameTransform transform = EnumNameTransform::snake_to_kebab;` +/// OR `static std::string transform(std::string_view _raw_name);` +/// (any return type convertible or explicitly constructible into `std::string`, +/// such as `std::string_view`, is accepted) +/// - `static constexpr std::array, N> overrides = {{ ... }};` +/// OR `static std::span> overrides();` +template +struct EnumNames {}; + +namespace internal { + +template +struct is_std_span : std::false_type {}; + +template +struct is_std_span> : std::true_type {}; + +template +concept EnumOverrideContiguousRange = + (std::is_array_v> && + std::same_as< + std::remove_cv_t>>, + EnumOverride>) || + requires(const std::remove_reference_t& _r) { + { _r.data() } -> std::convertible_to*>; + { _r.size() } -> std::convertible_to; + }; + +template +concept BorrowedEnumOverridesResult = + std::is_lvalue_reference_v || + is_std_span>::value; + +template +concept HasEnumOverridesVar = requires { + requires EnumOverrideContiguousRange::overrides), E>; +}; + +template +concept HasAnyEnumOverridesFn = requires { + requires EnumOverrideContiguousRange::overrides()), E>; +}; + +template +concept HasEnumOverridesFn = + HasAnyEnumOverridesFn && + BorrowedEnumOverridesResult::overrides())>; + +template +concept HasEnumTransformConst = requires { + { EnumNames::transform } -> std::convertible_to; +}; + +template +concept HasEnumTransformFn = requires(std::string_view _s) { + requires std::constructible_from::transform(_s))>; +}; + +template +inline constexpr bool has_custom_enum_names_for_transform_v = + HasEnumOverridesVar> || + HasAnyEnumOverridesFn> || + HasEnumTransformConst> || + HasEnumTransformFn> || + (_transform != EnumNameTransform::none); + +template > +inline constexpr bool has_custom_enum_names_v = + has_custom_enum_names_for_transform_v< + E, enum_name_transform_v>; + +template +inline std::span>> +get_enum_overrides() { + using U = std::remove_cvref_t; + static_assert( + !HasAnyEnumOverridesFn || HasEnumOverridesFn, + "rfl::EnumNames::overrides() must return a borrowed contiguous range " + "(for example, std::span> or const auto&)."); + if constexpr (HasEnumOverridesFn) { + auto&& r = EnumNames::overrides(); + return std::span>(std::data(r), std::size(r)); + } else if constexpr (HasEnumOverridesVar) { + return std::span>(std::data(EnumNames::overrides), + std::size(EnumNames::overrides)); + } else { + return {}; + } +} + +template +inline std::string resolve_enum_wire_name_for_transform( + const E _val, const std::string_view _raw_name) { + using U = std::remove_cvref_t; + for (const auto& [e, wire] : get_enum_overrides()) { + if (e == _val) { + return std::string(wire); + } + } + if constexpr (HasEnumTransformFn) { + return std::string(EnumNames::transform(_raw_name)); + } else if constexpr (HasEnumTransformConst) { + return apply_enum_name_transform(_raw_name, EnumNames::transform); + } else if constexpr (_transform != EnumNameTransform::none) { + return apply_enum_name_transform(_raw_name, _transform); + } else { + return std::string(_raw_name); + } +} + +template > +inline std::string resolve_enum_wire_name(const E _val, + const std::string_view _raw_name) { + return resolve_enum_wire_name_for_transform< + E, enum_name_transform_v>(_val, _raw_name); +} + +} // namespace internal +} // namespace rfl + +#endif // RFL_ENUMNAMES_HPP_ diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp index 2d674f8a3..46c16df1e 100644 --- a/include/rfl/enums.hpp +++ b/include/rfl/enums.hpp @@ -9,6 +9,7 @@ #include #include +#include "EnumNames.hpp" #include "Processors.hpp" #include "Result.hpp" #include "TransformEnumNames.hpp" @@ -62,15 +63,6 @@ constexpr auto get_enum_range() { namespace internal { -template -inline constexpr bool has_custom_enum_names_for_transform_v = - (_transform != EnumNameTransform::none); - -template -inline constexpr bool has_custom_enum_names_v = - has_custom_enum_names_for_transform_v< - std::remove_cvref_t, enum_name_transform_v>; - template std::vector>> build_enum_wire_names_with_transform() { @@ -79,7 +71,23 @@ build_enum_wire_names_with_transform() { constexpr auto enumerators = get_enumerator_array(); out.reserve(enumerators.size()); for (const auto& [raw_name, val] : enumerators) { - out.emplace_back(apply_enum_name_transform(raw_name, _transform), val); + out.emplace_back( + resolve_enum_wire_name_for_transform(val, raw_name), + val); + } + if constexpr (has_custom_enum_names_for_transform_v) { + for (const auto& [val, wire] : get_enum_overrides()) { + bool already_present = false; + for (const auto& entry : out) { + if (entry.second == val) { + already_present = true; + break; + } + } + if (!already_present) { + out.emplace_back(std::string(wire), val); + } + } } return out; } @@ -272,8 +280,9 @@ Result> string_to_enum_with_transform_noexcept( } // namespace internal -// Converts an enum value to its wire string representation, honoring any -// `rfl::TransformEnumNames<...>` processor passed in `Ps...`. +// Converts an enum value to its wire string representation, honoring +// `rfl::EnumNames` and any `rfl::TransformEnumNames<...>` processor +// passed in `Ps...`. template std::string enum_to_string(const EnumType _enum) { return internal::enum_to_string_with_transform< @@ -281,6 +290,17 @@ std::string enum_to_string(const EnumType _enum) { internal::enum_name_transform_v>>(_enum); } +// Returns all declared enumerators (plus any explicit `rfl::EnumNames` +// overrides for values outside the reflected range) paired with their resolved +// wire names. +template +std::vector>> +get_enum_wire_names() { + return internal::get_cached_enum_wire_names_with_transform< + std::remove_cvref_t, + internal::enum_name_transform_v>>(); +} + // Converts a string to a value of the given enum type. // // By default, numeric values are accepted in addition to the declared