Skip to content

Add opt-in rfl::NoDuplicateKeys processor for JSON and YAML - #128

Open
clockblocker wants to merge 1 commit into
mainfrom
feat-no-duplicate-keys
Open

clockblocker wants to merge 1 commit into
mainfrom
feat-no-duplicate-keys

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds an opt-in rfl::NoDuplicateKeys processor (matching rfl::NoExtraFields and rfl::EnumNamesOnly) that rejects repeated object/mapping keys during JSON and YAML deserialization while leaving default behavior 100% backwards-compatible and zero-overhead when unused.

  • include/rfl/NoDuplicateKeys.hpp & include/rfl/internal/no_duplicate_keys_v.hpp: Opt-in marker processor + reader_supports_no_duplicate_keys_v<R> trait. Passing rfl::NoDuplicateKeys to a reader without duplicate-key support fails at compile time (static_assert).
  • include/rfl/json/Reader.hpp: Wires supports_no_duplicate_keys = true with zero-copy std::unordered_set<std::string_view> over the yyjson buffer (if constexpr zero overhead when unused). Works across structs (ViewReader / ViewReaderWithDefault), std::map / rfl::Object (MapReader), rfl::Generic (nested arbitrarily deep), rfl::FieldVariant, and rfl::TaggedUnion (including duplicate discriminators).
  • include/rfl/yaml/Reader.hpp: Wires supports_no_duplicate_keys = true in read_object, reporting each repeated key via add_error() (aggregated alongside other field errors and honoring rfl::FailFast).
  • Docs & tests: Updated docs/content/processors.md and added tests/json/test_no_duplicate_keys.cpp and tests/yaml/test_no_duplicate_keys.cpp.

Motivation

  • openmoq/moqx had to hand-roll a separate duplicate-key pre-pass around yaml-cpp in src/config/loader/YamlDuplicateKeys.h (L13–19) specifically because yaml-cpp preserves repeated keys and reflect-cpp silently drops or overwrites them:

    "yaml-cpp keeps every entry of a repeated key; the reflect step then silently drops or keeps the extras. Throws std::runtime_error naming the key and line."

  • Strict JSON/YAML config loaders generally reject duplicate keys rather than relying on last-write-wins or first-write-wins behavior.

Verification

  • Strict standalone headers (-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror): passed; 0 relative-include errors.
  • Compile-fail static_assert verified for unsupported rfl::generic::Reader.
  • tests/json/test_no_duplicate_keys.cpp + tests/yaml/test_no_duplicate_keys.cpp under -fsanitize=undefined -fno-sanitize-recover=all: 8/8 passed.

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.

1 participant