Repository navigation
Description
Part of #138
Popularity: 1/7 corpus consumers (slang-server)
Related issues
- Related issues: API/TaggedUnion: no default/fallback tag when discriminator field is omitted #146 (
API/TaggedUnion: no default/fallback tag when discriminator field is omitted), fork Map (Lane 2): JSON / RPC & Format Interop #86 (Map (Lane 2): JSON / RPC & Format Interop).
Observed Downstream Workaround (hudson-trading/slang-server @ 1b413a46)
The Waveform Control Protocol (WCP) uses two-level flat JSON discrimination in the same object: e.g. {"type": "event", "event": "waveforms_loaded", "source": "..."} vs {"type": "response", "command": "..."}.
Because rfl::TaggedUnion only supports a single top-level discriminator key (and cannot flatten or chain a nested TaggedUnion on a second sibling discriminator key), slang-server defines two separate rfl::TaggedUnion aliases, repeats an unused std::string type; field on every event struct, and parses the raw JSON string *message twice whenever "type" == "event":
include/wcp/WcpTypes.h#L24-L27and#L61-L91:struct Event { using Tag = rfl::Literal<"event">; std::string event; }; ... struct WaveformsLoaded { using Tag = rfl::Literal<"waveforms_loaded">; std::string type; std::string source; }; ... using S2CMessage = rfl::TaggedUnion<"type", Response, Event>; using S2CEvent = rfl::TaggedUnion<"event", WaveformsLoaded, GotoDeclaration, AddDrivers, AddLoads>;
src/ast/WcpClient.cpp#L316-L333:auto s2cMessage = rfl::json::read<wcp::S2CMessage>(*message); if (!s2cMessage) { std::cerr << "WCP S2C decode error" << std::endl; continue; } rfl::visit( [&](auto&& msg) { using Type = std::decay_t<decltype(msg)>; if constexpr (std::is_same<Type, wcp::Response>()) { } else if constexpr (std::is_same<Type, wcp::Event>()) { auto s2cEvent = rfl::json::read<wcp::S2CEvent>(*message);
Root Cause & Suggested Direction
rfl::TaggedUnion assumes each alternative is a flat named struct with a single Tag matching one discriminator field; nesting another rfl::TaggedUnion as a flattened alternative (e.g., struct Event { using Tag = rfl::Literal<"event">; rfl::Flatten<S2CEvent> payload; }; or nested TaggedUnion support) is not supported cleanly without re-parsing the input document.
Support nested/flattened rfl::TaggedUnion alternatives (e.g. rfl::Flatten<rfl::TaggedUnion<...>> inside a TaggedUnion member, or tagged alternatives that delegate to an inner TaggedUnion) so multi-level flat discriminated protocols parse in a single pass without redundant tag fields.
Upstream Overlap
- Flatten<TaggedUnion<...>> isn't flattened but instead becomes a "variant_" implicit field. munich-data-engineering/reflect-cpp#449 (closed/wontfix, exact) — "Flatten<TaggedUnion<...>> isn't flattened but instead becomes a
variant_implicit field"; maintainer@liuzicheng1987explicitly replied "This is indeed unsupported. What you can do, is to do the opposite: FlattenPersonintoEmployeeandShareHolder." and closed the issue.
Activity
[-]API/TaggedUnion (slang-server): no nested/chained flat discriminator support forces double JSON parsing and duplicate tag fields in WCP protocol[/-][+]API/TaggedUnion: no nested/chained flat discriminator support forces double JSON parsing and duplicate tag fields in WCP protocol[/+]clockblocker commentedon Nov 29, 2026
Triage decision: closing as
wontfix(not planned) for the upstream adoption track.Rationale: Nested/flattened
rfl::TaggedUnionrequires ~+3.7k LOC across core parsing/schema paths (prototyped in parked fork PR #82, branchnested-tagged-union). Upstreammunich-data-engineering/reflect-cpp#449explicitly closed flattenedTaggedUnionas unsupported; the complexity and long-term maintenance cost are disproportionate for a single protocol (WCP inslang-server).