Skip to content

Generate typed Go union interfaces#1252

Open
qmuntal wants to merge 9 commits intogithub:mainfrom
qmuntal:go-typed-union-interfaces
Open

Generate typed Go union interfaces#1252
qmuntal wants to merge 9 commits intogithub:mainfrom
qmuntal:go-typed-union-interfaces

Conversation

@qmuntal
Copy link
Copy Markdown
Contributor

@qmuntal qmuntal commented May 11, 2026

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:

  • Adds generator support for interface-based discriminated unions, primitive unions, raw fallback variants, and custom JSON encoding/decoding for interface-valued fields.
  • Regenerates Go RPC and session-event types, moving generated JSON helpers into go/rpc/zrpc_encoding.go and go/zsession_encoding.go.
  • Makes SessionEvent.Type() derive from SessionEvent.Data and preserves unknown session event types through RawSessionEventData.
  • Updates Go session logic, tests, E2E helpers, examples, and image-input docs to use the new concrete union variants and Type() method.

Why this is beneficial

  • Prevents invalid union states that were possible with flattened structs containing many optional fields.
  • Makes Go APIs more schema-like and type-safe: callers can switch on concrete variants instead of inspecting optional fields that may not apply to a given union member.
  • Keeps discriminator values read-only and derived from the concrete type, avoiding mismatches between type/kind fields and payload contents.
  • Improves forward compatibility by preserving unknown union and session event payloads instead of dropping or failing on newer schema variants.
  • Keeps generated type declarations easier to scan by moving bulky JSON encoding logic into generated z*encoding.go files.
  • Supports interface-valued fields in generated types without making SDK callers manually decode raw JSON.

Validation

  • go test ./... -run '^$'
  • Focused Go tests for session behavior and generated RPC union behavior passed before commit.

No snapshot updates are included in this PR.

Copilot AI review requested due to automatic review settings May 11, 2026 10:30
@qmuntal qmuntal requested a review from a team as a code owner May 11, 2026 10:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.go files 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 on event.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

Comment thread go/session.go Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 39/41 changed files
  • Comments generated: 2

Comment thread go/zsession_encoding.go Outdated
Comment thread test/scenarios/prompts/attachments/README.md
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.

2 participants