Skip to content

Add opt-in rfl::DefaultTag<Discriminator, Tag> processor for rfl::TaggedUnion - #129

Open
clockblocker wants to merge 1 commit into
mainfrom
feat-tagged-union-default-tag
Open

clockblocker wants to merge 1 commit into
mainfrom
feat-tagged-union-default-tag

Conversation

@clockblocker

Copy link
Copy Markdown
Owner

Summary

Adds an opt-in rfl::DefaultTag<_discriminator, _default_tag> processor that supplies a default discriminator tag when a rfl::TaggedUnion<_discriminator, ...> is deserialized from an object whose _discriminator field is absent.

  • include/rfl/DefaultTag.hpp & include/rfl/internal/default_tag.hpp: Supports flat and nested rfl::Processors<...> bundles, and multiple DefaultTag processors with distinct discriminators in the same call.
  • include/rfl/parsing/ParserTaggedUnion.hpp:
    • Falls back only when the discriminator field is missing (present non-string, null, or unknown tags still fail as usual).
    • Parses the matched tag-less alternative directly when defaulted so rfl::NoExtraFields and empty-struct alternatives work cleanly.
    • Writing always emits the explicit discriminator field.
    • Compile-time static_assert validation: rejects unknown _default_tag values (not in PossibleTags), duplicate DefaultTag processors for the same discriminator, and incompatible rfl::NoFieldNames combinations.
  • Docs & tests: Updated docs/content/processors.md and added tests/json/test_default_tag.cpp and tests/yaml/test_default_tag.cpp.

Real-World OSS Motivation

  • NVIDIA/cuda-quantum uses using TargetConfigSchema = rfl::TaggedUnion<"version", v1::TargetConfigV1, v2::TargetConfigV2>; and had to pull in <yaml-cpp/yaml.h> directly to manually parse, mutate the YAML::Node, and pass raw InputVarType into rfl::yaml::read solely to default unversioned configs to v1 in cudaq/lib/Target/Yaml/TargetConfigSchema.cpp (L36–40):
    // Unversioned configurations are schema v1, permanently.
    if (node.IsMap() && !node["version"])
      node["version"] = "1";
    const auto parsed = rfl::yaml::read<TargetConfigSchema, YamlProcessors>(
        rfl::yaml::InputVarType(node), substitutedYamlContent);
    With rfl::DefaultTag<"version", "1"> inside YamlProcessors, that manual yaml-cpp pre-parsing disappears completely.

Verification

  • Strict standalone headers (-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror): passed; 0 relative-include errors.
  • Compile-fail static_assert verified for invalid default tag, duplicate discriminator, and NoFieldNames.
  • tests/json/test_default_tag.cpp + tests/yaml/test_default_tag.cpp under -fsanitize=undefined -fno-sanitize-recover=all: 4/4 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