Skip to content

Conversation

@jmcarp
Copy link
Contributor

@jmcarp jmcarp commented Nov 20, 2025

Note: just a draft submitted for discussion, although we could clean this up and ship it pretty easily if we like the direction.

Problem: for oneOf types with object variants, we represent the set of variables as an any in the sdk. This means we don't get type safety when we unmarshal responses, and users have to cast, check errors, or marshal and unmarshal json to get typed values.

Proposal: represent enums with object variants as interfaces. Each enum is represented by its own interface, and each variant is a struct that implements the interface using a private is{{.Interface}} method. We generate a custom UnmarshalJSON method for each enum type to switch on its discriminator field (often "type", "kind", etc.) and unmarshal into the correct concrete type.

If we like this direction, we can also add helper functions like .As{{.ConcreteType}} to cast to concrete types.

Part of #340.

@jmcarp jmcarp force-pushed the jmcarp/enum-interfaces branch from 4cf3b79 to dcc81e1 Compare November 20, 2025 18:59
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