Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@raphael
Copy link
Member

@raphael raphael commented Aug 28, 2025

Summary
Fix generation for OneOf unions involving alias user types, especially when struct:pkg:path places the member in a different package.

Rationale
In Go, an interface that has an unexported method can only be implemented by types in the same package. Previously the union marker method could end up associated with a member type that lives in a different package than the union interface, which makes it impossible for that member to satisfy the interface. In addition, when multiple union branches share the same underlying type, generated type switches can produce duplicate cases.

This change generates a small, per-branch wrapper type in the package where the union interface lives (or a common shared package for shared user-type unions) and attaches the marker method to that wrapper. The wrapper is a distinct type defined locally, so it can satisfy the interface without cross-package unexported methods, and it gives each branch a unique concrete type for type switches. When a union member already lives in the same package and is unique, the marker is attached directly to the member to preserve existing pointer vs value receiver semantics.

Changes

  • Generate wrapper types for union members when they are cross-package, duplicated, or primitives.
  • Attach union marker methods to wrappers; otherwise attach directly to the member.
  • Preserve pointer/value receiver semantics for existing user types.
  • Update gRPC transforms to reference wrappers in both directions where required.
  • Avoid extra whitespace in marker templates to keep golden files stable.

Compatibility
No DSL changes. Only generated code changes; behavior remains the same for unions that did not require wrappers.

Testing
All existing Goa tests pass locally.

…nerate per-branch wrappers when union members are cross-package or duplicate underlying user types\n- Preserve pointer/value receiver semantics for union marker methods\n- Update GRPC transforms to reference correct wrapper types for both directions\n- Trim whitespace in union method template to avoid golden diffs\n\nFixes: alias types in OneOf with struct:pkg:path and duplicate user types
@raphael raphael changed the title codegen(service,grpc): fix union alias handling with wrappers\n\n- Generate per-branch wrappers when union members are cross-package or duplicate underlying user types\n- Preserve pointer/value receiver semantics for union marker methods\n- Update GRPC transforms to reference correct wrapper types for both directions\n- Trim whitespace in union method template to avoid golden diffs\n\nFixes: alias types in OneOf with struct:pkg:path and duplicate user types codegen: fix OneOf alias handling with wrappers Aug 28, 2025
@raphael raphael merged commit 7b0585a into v3 Aug 28, 2025
7 checks passed
@raphael raphael deleted the fix/union-alias-wrappers branch August 28, 2025 17:52
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