Error when writing un-enumerated named enum values (#2672) - #2675
Open
stephenberry wants to merge 1 commit into
Open
Error when writing un-enumerated named enum values (#2672)#2675stephenberry wants to merge 1 commit into
stephenberry wants to merge 1 commit into
Conversation
A named enum (glz::meta with enumerate(...) or keys/value arrays) whose value is not one of the enumerated values has no string name. The writer previously fell back to emitting the underlying integer, but the string formats (JSON, TOML, YAML, MessagePack) validate enum names on read, so that output could not be read back (issue #2672). Make the writer error with error_code::unexpected_enum by default for those four formats, consistent with Glaze's read-side validation. Binary formats (BEVE, CBOR, BSON) intentionally round-trip integers and are unchanged. Add the inheritable opt-out option `enum_int_fallback` (default false) to restore the previous integer-emitting behavior when desired. Includes regression tests across all four formats plus an option opt-out suite, and documentation in the README and docs/options.md.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2672 — a write/read asymmetry for named enums.
A named enum (
glz::metawithenumerate(...)orkeys/valuearrays) whose value is not one of the enumerated values has no string name. The writer previously fell back to emitting the underlying integer, but the string formats validate enum names on read, so that output could not be read back:Change
The named-enum writer now errors with
error_code::unexpected_enumby default when the value has no registered name, consistent with Glaze's philosophy of validating on read (untrusted input) while treating writes as trusted/computer-generated. Since the name lookup already happens on write, this adds no real cost.Applied to the four formats that share this asymmetry (write name-or-integer-fallback, validate names on read):
unexpected_enumerrorFor JSON the writer's
can_error = falsewas removed so the error propagates through containers and structs (it now defaults totrue, like the chrono writers).Opt-out
Added the inheritable option
enum_int_fallback(defaultfalse) to restore the previous integer-emitting behavior:Tests & docs
tests/reflection/enum_test.cpp: write errors on un-enumerated values across all four formats; error propagates through struct/vector; enumerated values and valid round-trips still work; BEVE still round-trips integers; and anenum_int_fallbackopt-out suite.README.mdanddocs/options.md.All affected suites pass locally: enum_test (50), json_test (686), toml_test (389), yaml_test (668), msgpack_test (59), beve_test (381).
Note (separate, not addressed here)
CSV has a
from<CSV, named_enum>(read) but noto<CSV, named_enum>(write) — writing a named enum via CSV does not compile. That is a distinct pre-existing gap, left out of scope.