Skip to content

API/TaggedUnion: no nested/chained flat discriminator support forces double JSON parsing and duplicate tag fields in WCP protocol #187

Not planned

Description

@clockblocker

Part of #138
Popularity: 1/7 corpus consumers (slang-server)

Related issues

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-L27 and #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

Activity

  1. added
    known-downstream-workarounds
    on Nov 29, 2026
  2. changed the title [-]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[/+] on Nov 29, 2026
  3. added
    wontfix
    on Nov 29, 2026
  4. clockblocker commented on Nov 29, 2026

    @clockblocker
    OwnerAuthor

    Triage decision: closing as wontfix (not planned) for the upstream adoption track.

    Rationale: Nested/flattened rfl::TaggedUnion requires ~+3.7k LOC across core parsing/schema paths (prototyped in parked fork PR #82, branch nested-tagged-union). Upstream munich-data-engineering/reflect-cpp#449 explicitly closed flattened TaggedUnion as unsupported; the complexity and long-term maintenance cost are disproportionate for a single protocol (WCP in slang-server).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    known-downstream-workaroundswontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions