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 2 times, most recently from dcc81e1 to d4d5bb8 Compare November 25, 2025 17:17
@jmcarp jmcarp force-pushed the jmcarp/enum-interfaces branch from b0b6352 to 48fb264 Compare December 1, 2025 16:48
@jmcarp jmcarp force-pushed the jmcarp/enum-interfaces branch from 48fb264 to 3605de0 Compare December 1, 2025 16:49
jmcarp added a commit that referenced this pull request Dec 10, 2025
In thinking through #342, I noticed that the enum type creation logic in
createOneOf is a bit hard to follow, and not tested thoroughly. In advance of
changing that behavior, I wanted to clean up the existing logic and add some
tests. This should make the code more readable and easier to change in the
future.

Relates to #350.
jmcarp added a commit that referenced this pull request Dec 12, 2025
In thinking through #342, I noticed that the enum type creation logic in
createOneOf is a bit hard to follow, and not tested thoroughly. In
advance of changing that behavior, I wanted to clean up the existing
logic and add some tests. This should make the code more readable and
easier to change in the future.

Relates to #350. 
Relates to SSE-123.
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