Skip to content

Accept std::string_view in rfl::string_to_enum - #802

Open
clockblocker wants to merge 1 commit into
munich-data-engineering:mainfrom
clockblocker:enum-string-view
Open

clockblocker wants to merge 1 commit into
munich-data-engineering:mainfrom
clockblocker:enum-string-view

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

AI-authored below this line.

Closes #565

Problem

rfl::string_to_enum required const std::string&, forcing callers holding a std::string_view or substring slice to allocate a temporary std::string. Its numeric fallback also relied on std::stoi, which silently accepted trailing junk ("1abc"), threw exceptions on failure, and could not parse 64-bit enum values outside int range.

using namespace std::string_view_literals;

enum class Color { red = 1, green = 2, blue = 3 };
enum class Flags64 : uint64_t { flag_a = 1ULL << 0 };

// Before:
//   string_to_enum<Color>("green"sv);                 // does not compile without std::string alloc
//   string_to_enum<Flags64>("flag_a|1099511627776");  // Error (std::stoi overflow)
//   string_to_enum<Color>("1abc");                    // Ok: Color::red (trailing junk ignored)

// After:
auto c = rfl::string_to_enum<Color>("green"sv);                 // Ok: 0 allocations
auto f = rfl::string_to_enum<Flags64>("flag_a|1099511627776"sv); // Ok: full 64-bit flag support
auto x = rfl::string_to_enum<Color>("1abc"sv);                  // Error: rejects trailing junk

Summary

  • Updates rfl::string_to_enum and the C++20/C++26 internal enum lookup helpers to take std::string_view, remaining fully backwards-compatible with std::string and const char* callers.
  • Replaces std::stoi and allocating | flag splitting with zero-allocation std::string_view slicing and full-width std::from_chars, and drops the unused <sstream> include from include/rfl/enums.hpp.

Real-world motivation

Project Reference Context
NVIDIA/cuda-quantum Target/Reflection.h#L61-L65 Hand-loops over rfl::get_enumerator_array<E>() just to parse a std::string_view without allocating.
munich-data-engineering/sqlgen Parser_default.hpp#L52-L57 Calls rfl::string_to_enum directly when parsing enum row values.

Testing

  • Verified with the existing test suite plus standalone regression tests in clockblocker/refl-cpp-workbench/queue-tests/generic/test_string_to_enum.cpp covering std::string_view literals, non-null-terminated substring slices, 64-bit flag enums, full-width numeric fallback, trailing-junk rejection, enum_names_only, and std::string / const char* compatibility (kept out of the PR diff to keep it minimal; happy to pull them in if preferred).

clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 29, 2026
…neering#731)

Adds opt-in whole-enum wire-name casing transform processors
(`rfl::TransformEnumNames<...>` plus aliases `EnumSnakeCaseToKebabCase`,
`EnumSnakeCaseToCamelCase`, `EnumSnakeCaseToPascalCase`,
`EnumCamelCaseToSnakeCase`, `EnumToLowerCase`, `EnumToUpperCase`) across read,
write, `rfl::enum_to_string`, `rfl::string_to_enum`, and JSON Schema generation.
Resolved wire-name tables are cached once per `(EnumType, EnumNameTransform)`.

Parent: depends on `feat(enums): accept std::string_view in rfl::string_to_enum (munich-data-engineering#565)`
(open upstream PR munich-data-engineering#802). Per-enum overrides/trait (`rfl::EnumNames<E>`, #106)
is split into a separate child commit on top of this processor seam.

Judge polish applied:
- Moves non-template `apply_enum_name_transform` out-of-line into
  `src/rfl/TransformEnumNames.cpp` (`RFL_API`, wired via `src/reflectcpp.cpp`).
- Places `enum_name_transform_v` in `include/rfl/internal/enum_name_transform_v.hpp`
  (mirroring `enum_names_only_v.hpp` with `std::remove_pointer_t`).
- Adds `EnumCamelCaseToSnakeCase` for parity with `rfl::CamelCaseToSnakeCase` (munich-data-engineering#588).
- Accepts variadic `class... Ps` on `rfl::enum_to_string` and `rfl::string_to_enum`,
  strips `std::remove_cvref_t<EnumType>`, and guards `config::enum_descriptions`
  so untransformed enums avoid static heap caches.

Refs: munich-data-engineering#731
(Maintainer: "My first instinct would be a processor, but I need to give it some thought.")
Downstream receipts:
- NVIDIA/cuda-quantum maintains 4 hand-written `rfl::Reflector<E>` switch tables
  for `snake_case` -> `kebab-case` YAML enums:
  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/QuakeUnmappedProfileYaml.h#L16-L53
- openmoq/moqx duplicates `enum class QuicStack` as `rfl::Literal` + switch mapper:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/loader/ParsedConfig.h#L291
- munich-data-engineering/sqlgen post-processes `enum_to_string` across all 4 SQL backends:
  https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/src/sqlgen/sqlite/to_sql.cpp#L338-L340
- games-on-whales/wolf lowercases `enum_to_string`:
  https://github.com/games-on-whales/wolf/blob/ac2461f68feda41fdc7a7c8e812b9ab4b80520b7/src/moonlight-server/api/openapi.cpp#L92
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 29, 2026
…neering#731)

Adds opt-in whole-enum wire-name casing transform processors
(`rfl::TransformEnumNames<...>` plus aliases `EnumSnakeCaseToKebabCase`,
`EnumSnakeCaseToCamelCase`, `EnumSnakeCaseToPascalCase`,
`EnumCamelCaseToSnakeCase`, `EnumToLowerCase`, `EnumToUpperCase`) across read,
write, `rfl::enum_to_string`, `rfl::string_to_enum`, and JSON Schema generation.
Resolved wire-name tables are cached once per `(EnumType, EnumNameTransform)`.

Parent: depends on `feat(enums): accept std::string_view in rfl::string_to_enum (munich-data-engineering#565)`
(open upstream PR munich-data-engineering#802). Per-enum overrides/trait (`rfl::EnumNames<E>`, #106)
is split into a separate child commit on top of this processor seam.

Judge polish applied:
- Moves non-template `apply_enum_name_transform` out-of-line into
  `src/rfl/TransformEnumNames.cpp` (`RFL_API`, wired via `src/reflectcpp.cpp`).
- Places `enum_name_transform_v` in `include/rfl/internal/enum_name_transform_v.hpp`
  (mirroring `enum_names_only_v.hpp` with `std::remove_pointer_t`).
- Adds `EnumCamelCaseToSnakeCase` for parity with `rfl::CamelCaseToSnakeCase` (munich-data-engineering#588).
- Accepts variadic `class... Ps` on `rfl::enum_to_string` and `rfl::string_to_enum`,
  strips `std::remove_cvref_t<EnumType>`, and guards `config::enum_descriptions`
  so untransformed enums avoid static heap caches.

Refs: munich-data-engineering#731
(Maintainer: "My first instinct would be a processor, but I need to give it some thought.")
Downstream receipts:
- NVIDIA/cuda-quantum maintains 4 hand-written `rfl::Reflector<E>` switch tables
  for `snake_case` -> `kebab-case` YAML enums:
  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/QuakeUnmappedProfileYaml.h#L16-L53
- openmoq/moqx duplicates `enum class QuicStack` as `rfl::Literal` + switch mapper:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/loader/ParsedConfig.h#L291
- munich-data-engineering/sqlgen post-processes `enum_to_string` across all 4 SQL backends:
  https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/src/sqlgen/sqlite/to_sql.cpp#L338-L340
- games-on-whales/wolf lowercases `enum_to_string`:
  https://github.com/games-on-whales/wolf/blob/ac2461f68feda41fdc7a7c8e812b9ab4b80520b7/src/moonlight-server/api/openapi.cpp#L92
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 29, 2026
…ata-engineering#565)

Updates `rfl::string_to_enum` and `rfl::internal::enums::from_string` (C++20
and C++26) to accept `std::string_view` instead of `const std::string&`,
replaces the exception-throwing `std::stoi` numeric fallback with allocation-free
`std::from_chars` enforcing full-string consumption, and drops unused `<sstream>`.

Upstream PR: munich-data-engineering#802
Closes: munich-data-engineering#565
Downstream receipts:
- NVIDIA/cuda-quantum hand-loops `rfl::get_enumerator_array<E>()` just to parse
  a `std::string_view`:
  https://github.com/NVIDIA/cuda-quantum/blob/81ef20d9d4a1e0912a6dbc68637ece017e8fd324/cudaq/lib/Target/Reflection.h#L61-L65
- munich-data-engineering/sqlgen calls `rfl::string_to_enum` directly:
  https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/include/sqlgen/parsing/Parser_default.hpp#L52-L57
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 29, 2026
…neering#731)

Adds opt-in whole-enum wire-name casing transform processors
(`rfl::TransformEnumNames<...>` plus aliases `EnumSnakeCaseToKebabCase`,
`EnumSnakeCaseToCamelCase`, `EnumSnakeCaseToPascalCase`,
`EnumCamelCaseToSnakeCase`, `EnumToLowerCase`, `EnumToUpperCase`) across read,
write, `rfl::enum_to_string`, `rfl::string_to_enum`, and JSON Schema generation.
Resolved wire-name tables are cached once per `(EnumType, EnumNameTransform)`.

Parent: depends on `feat(enums): accept std::string_view in rfl::string_to_enum (munich-data-engineering#565)`
(open upstream PR munich-data-engineering#802). Per-enum overrides/trait (`rfl::EnumNames<E>`, #106)
is split into a separate child commit on top of this processor seam.

Judge polish applied:
- Moves non-template `apply_enum_name_transform` out-of-line into
  `src/rfl/TransformEnumNames.cpp` (`RFL_API`, wired via `src/reflectcpp.cpp`).
- Places `enum_name_transform_v` in `include/rfl/internal/enum_name_transform_v.hpp`
  (mirroring `enum_names_only_v.hpp` with `std::remove_pointer_t`).
- Adds `EnumCamelCaseToSnakeCase` for parity with `rfl::CamelCaseToSnakeCase` (munich-data-engineering#588).
- Accepts variadic `class... Ps` on `rfl::enum_to_string` and `rfl::string_to_enum`,
  strips `std::remove_cvref_t<EnumType>`, and guards `config::enum_descriptions`
  so untransformed enums avoid static heap caches.

Refs: munich-data-engineering#731
(Maintainer: "My first instinct would be a processor, but I need to give it some thought.")
Downstream receipts:
- NVIDIA/cuda-quantum maintains 4 hand-written `rfl::Reflector<E>` switch tables
  for `snake_case` -> `kebab-case` YAML enums:
  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/QuakeUnmappedProfileYaml.h#L16-L53
- openmoq/moqx duplicates `enum class QuicStack` as `rfl::Literal` + switch mapper:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/loader/ParsedConfig.h#L291
- munich-data-engineering/sqlgen post-processes `enum_to_string` across all 4 SQL backends:
  https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/src/sqlgen/sqlite/to_sql.cpp#L338-L340
- games-on-whales/wolf lowercases `enum_to_string`:
  https://github.com/games-on-whales/wolf/blob/ac2461f68feda41fdc7a7c8e812b9ab4b80520b7/src/moonlight-server/api/openapi.cpp#L92
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Nov 30, 2026
…neering#731)

Adds opt-in whole-enum wire-name casing transform processors
(`rfl::TransformEnumNames<...>` plus aliases `EnumSnakeCaseToKebabCase`,
`EnumSnakeCaseToCamelCase`, `EnumSnakeCaseToPascalCase`,
`EnumCamelCaseToSnakeCase`, `EnumToLowerCase`, `EnumToUpperCase`) across read,
write, `rfl::enum_to_string`, `rfl::string_to_enum`, and JSON Schema generation.
Resolved wire-name tables are cached once per `(EnumType, EnumNameTransform)`.

Parent: depends on `feat(enums): accept std::string_view in rfl::string_to_enum (munich-data-engineering#565)`
(open upstream PR munich-data-engineering#802). Per-enum overrides/trait (`rfl::EnumNames<E>`, #106)
is split into a separate child commit on top of this processor seam.

Judge polish applied:
- Moves non-template `apply_enum_name_transform` out-of-line into
  `src/rfl/TransformEnumNames.cpp` (`RFL_API`, wired via `src/reflectcpp.cpp`).
- Places `enum_name_transform_v` in `include/rfl/internal/enum_name_transform_v.hpp`
  (mirroring `enum_names_only_v.hpp` with `std::remove_pointer_t`).
- Adds `EnumCamelCaseToSnakeCase` for parity with `rfl::CamelCaseToSnakeCase` (munich-data-engineering#588).
- Accepts variadic `class... Ps` on `rfl::enum_to_string` and `rfl::string_to_enum`,
  strips `std::remove_cvref_t<EnumType>`, and guards `config::enum_descriptions`
  so untransformed enums avoid static heap caches.

Refs: munich-data-engineering#731
(Maintainer: "My first instinct would be a processor, but I need to give it some thought.")
Downstream receipts:
- NVIDIA/cuda-quantum maintains 4 hand-written `rfl::Reflector<E>` switch tables
  for `snake_case` -> `kebab-case` YAML enums:
  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/QuakeUnmappedProfileYaml.h#L16-L53
- openmoq/moqx duplicates `enum class QuicStack` as `rfl::Literal` + switch mapper:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/loader/ParsedConfig.h#L291
- munich-data-engineering/sqlgen post-processes `enum_to_string` across all 4 SQL backends:
  https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/src/sqlgen/sqlite/to_sql.cpp#L338-L340
- games-on-whales/wolf lowercases `enum_to_string`:
  https://github.com/games-on-whales/wolf/blob/ac2461f68feda41fdc7a7c8e812b9ab4b80520b7/src/moonlight-server/api/openapi.cpp#L92
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Dec 1, 2026
…ata-engineering#565)

Updates `rfl::string_to_enum` and `rfl::internal::enums::from_string` (C++20
and C++26) to accept `std::string_view` instead of `const std::string&`,
replaces the exception-throwing `std::stoi` numeric fallback with allocation-free
`std::from_chars` enforcing full-string consumption, and drops unused `<sstream>`.

Upstream PR: munich-data-engineering#802
Closes: munich-data-engineering#565
Downstream receipts:
- NVIDIA/cuda-quantum hand-loops `rfl::get_enumerator_array<E>()` just to parse
  a `std::string_view`:
  https://github.com/NVIDIA/cuda-quantum/blob/81ef20d9d4a1e0912a6dbc68637ece017e8fd324/cudaq/lib/Target/Reflection.h#L61-L65
- munich-data-engineering/sqlgen calls `rfl::string_to_enum` directly:
  https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/include/sqlgen/parsing/Parser_default.hpp#L52-L57
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Dec 1, 2026
…neering#731)

Adds opt-in whole-enum wire-name casing transform processors
(`rfl::TransformEnumNames<...>` plus aliases `EnumSnakeCaseToKebabCase`,
`EnumSnakeCaseToCamelCase`, `EnumSnakeCaseToPascalCase`,
`EnumCamelCaseToSnakeCase`, `EnumToLowerCase`, `EnumToUpperCase`) across read,
write, `rfl::enum_to_string`, `rfl::string_to_enum`, and JSON Schema generation.
Resolved wire-name tables are cached once per `(EnumType, EnumNameTransform)`.

Parent: depends on `feat(enums): accept std::string_view in rfl::string_to_enum (munich-data-engineering#565)`
(open upstream PR munich-data-engineering#802). Per-enum overrides/trait (`rfl::EnumNames<E>`, #106)
is split into a separate child commit on top of this processor seam.

Judge polish applied:
- Moves non-template `apply_enum_name_transform` out-of-line into
  `src/rfl/TransformEnumNames.cpp` (`RFL_API`, wired via `src/reflectcpp.cpp`).
- Places `enum_name_transform_v` in `include/rfl/internal/enum_name_transform_v.hpp`
  (mirroring `enum_names_only_v.hpp` with `std::remove_pointer_t`).
- Adds `EnumCamelCaseToSnakeCase` for parity with `rfl::CamelCaseToSnakeCase` (munich-data-engineering#588).
- Accepts variadic `class... Ps` on `rfl::enum_to_string` and `rfl::string_to_enum`,
  strips `std::remove_cvref_t<EnumType>`, and guards `config::enum_descriptions`
  so untransformed enums avoid static heap caches.

Refs: munich-data-engineering#731
(Maintainer: "My first instinct would be a processor, but I need to give it some thought.")
Downstream receipts:
- NVIDIA/cuda-quantum maintains 4 hand-written `rfl::Reflector<E>` switch tables
  for `snake_case` -> `kebab-case` YAML enums:
  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/QuakeUnmappedProfileYaml.h#L16-L53
- openmoq/moqx duplicates `enum class QuicStack` as `rfl::Literal` + switch mapper:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/loader/ParsedConfig.h#L291
- munich-data-engineering/sqlgen post-processes `enum_to_string` across all 4 SQL backends:
  https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/src/sqlgen/sqlite/to_sql.cpp#L338-L340
- games-on-whales/wolf lowercases `enum_to_string`:
  https://github.com/games-on-whales/wolf/blob/ac2461f68feda41fdc7a7c8e812b9ab4b80520b7/src/moonlight-server/api/openapi.cpp#L92
clockblocker added a commit to clockblocker/reflect-cpp that referenced this pull request Dec 2, 2026
…neering#731)

Adds opt-in whole-enum wire-name casing transform processors
(`rfl::TransformEnumNames<...>` plus aliases `EnumSnakeCaseToKebabCase`,
`EnumSnakeCaseToCamelCase`, `EnumSnakeCaseToPascalCase`,
`EnumCamelCaseToSnakeCase`, `EnumToLowerCase`, `EnumToUpperCase`) across read,
write, `rfl::enum_to_string`, `rfl::string_to_enum`, and JSON Schema generation.
Resolved wire-name tables are cached once per `(EnumType, EnumNameTransform)`.

Parent: depends on `feat(enums): accept std::string_view in rfl::string_to_enum (munich-data-engineering#565)`
(open upstream PR munich-data-engineering#802). Per-enum overrides/trait (`rfl::EnumNames<E>`, #106)
is split into a separate child commit on top of this processor seam.

Judge polish applied:
- Moves non-template `apply_enum_name_transform` out-of-line into
  `src/rfl/TransformEnumNames.cpp` (`RFL_API`, wired via `src/reflectcpp.cpp`).
- Places `enum_name_transform_v` in `include/rfl/internal/enum_name_transform_v.hpp`
  (mirroring `enum_names_only_v.hpp` with `std::remove_pointer_t`).
- Adds `EnumCamelCaseToSnakeCase` for parity with `rfl::CamelCaseToSnakeCase` (munich-data-engineering#588).
- Accepts variadic `class... Ps` on `rfl::enum_to_string` and `rfl::string_to_enum`,
  strips `std::remove_cvref_t<EnumType>`, and guards `config::enum_descriptions`
  so untransformed enums avoid static heap caches.

Refs: munich-data-engineering#731
(Maintainer: "My first instinct would be a processor, but I need to give it some thought.")
Downstream receipts:
- NVIDIA/cuda-quantum maintains 4 hand-written `rfl::Reflector<E>` switch tables
  for `snake_case` -> `kebab-case` YAML enums:
  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/QuakeUnmappedProfileYaml.h#L16-L53
- openmoq/moqx duplicates `enum class QuicStack` as `rfl::Literal` + switch mapper:
  https://github.com/openmoq/moqx/blob/60bab7f13925acbaf578670223e660d71e891547/src/config/loader/ParsedConfig.h#L291
- munich-data-engineering/sqlgen post-processes `enum_to_string` across all 4 SQL backends:
  https://github.com/munich-data-engineering/sqlgen/blob/265b0a174b661c5905b874f97b064aecb4cded70/src/sqlgen/sqlite/to_sql.cpp#L338-L340
- games-on-whales/wolf lowercases `enum_to_string`:
  https://github.com/games-on-whales/wolf/blob/ac2461f68feda41fdc7a7c8e812b9ab4b80520b7/src/moonlight-server/api/openapi.cpp#L92

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

string_to_enum does not support std::string_view

1 participant