Generate typed Go union interfaces#1252
Open
qmuntal wants to merge 9 commits intogithub:mainfrom
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Go code generator and the generated Go SDK/RPC types to represent JSON-schema unions as typed, discriminator-driven interfaces (with concrete variant structs and raw fallback variants), and wires the handwritten Go session + tests to use the new Type()-derived event typing and concrete union variants.
Changes:
- Introduces generated JSON (un)marshalling helpers for interface-based unions in new
z*encoding.gofiles for both RPC and session events. - Refactors generated Go RPC/session types from “flattened optional fields” into discriminated union interfaces with concrete variants (and raw fallback for unknown discriminators).
- Updates Go session logic and E2E/unit tests to use
SessionEvent.Type()and type assertions onevent.Data/ concrete RPC union variants.
Show a summary per file
| File | Description |
|---|---|
| test/scenarios/sessions/streaming/go/main.go | Switches streaming scenario to event.Type() and updated response printing. |
| go/zsession_encoding.go | Adds generated session-event JSON encoding/decoding and union unmarshalling helpers (including raw fallback for unknown event/data discriminators). |
| go/session.go | Updates handwritten session code to construct/consume new typed union variants for UI elicitation + permission decisions. |
| go/session_test.go | Adjusts unit tests for event dispatch and capability updates without relying on a mutable SessionEvent.Type field. |
| go/session_event_serialization_test.go | Updates serialization tests to validate Type() is derived from Data and raw unknown events round-trip. |
| go/rpc/zrpc_encoding.go | Adds generated RPC JSON encoding/decoding + union unmarshalling helpers for interface-valued fields. |
| go/rpc/generated_rpc.go | Updates generated RPC types to use discriminated union interfaces + concrete variants instead of flattened structs. |
| go/rpc/generated_rpc_union_test.go | Updates/extends unit tests validating JSON union behavior for elicitation schema + values. |
| go/internal/e2e/testharness/helper.go | Updates E2E helpers to use event.Type() / event.Data type assertions. |
| go/internal/e2e/*.go | Broad E2E updates for event typing, permissions, attachments, streaming fidelity, and union-shaped payload assertions. |
Copilot's findings
- Files reviewed: 28/28 changed files
- Comments generated: 1
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
This PR updates the Go code generator and generated Go SDK types to model JSON-schema unions as typed interfaces instead of flattened structs where the schema has a discriminator.
Changes included:
go/rpc/zrpc_encoding.goandgo/zsession_encoding.go.SessionEvent.Type()derive fromSessionEvent.Dataand preserves unknown session event types throughRawSessionEventData.Type()method.Why this is beneficial
type/kindfields and payload contents.z*encoding.gofiles.Validation
go test ./... -run '^$'No snapshot updates are included in this PR.