-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best practices for use cases requiring nullary union members #928
Comments
Hi @lunaris. These are great questions, and hopefully my answers don't disappoint you :) For the nullary type, I would use an empty structure:
As an example, the JSON protocols used in AWS would serialize this as:
Union variants are "members" in Smithy, and all members have to target a shape in the model. Since Smithy doesn't have a kind of unit type, you have to pick a shape to target. Here I've made up my own empty unit type. There's nothing special about it, and it gets code-generated as a normal structure. For the optional
edit: Also, if you really wanted a kind of
it might not produce as nice of Haskell code that actually uses |
Thanks for the reply and sorry for the delay in responding! Yes, the "extra constructor" or nested union approaches are what we came up with/what I expect we will use; the challenge is mostly in legacy code we are looking to retrofit Smithy APIs on to. For those interested/coming here later, we are considering a trait-based approach to those APIs. Specifically, we are looking at defining a "unit" trait and an "optional" trait that a code generator can use to generate the @trait
structure unit {}
@trait
structure optional {}
@unit
structure Unit {}
union AnotherUnion {
first: String,
second: Integer,
third: Unit,
@optional
fourth: Double,
} Thanks again for the detailed response and consideration! Happy to close this issue. |
I could see a |
Smithy's
@enum
trait supports use cases in which a value must be one of a known set:which when generating TypeScript code (say) could produce:
Smithy's
union
shapes support use cases in which a value must have a tag from a known set and some associated value (depending on the tag):which again, when generating TypeScript could produce:
TypeScript (and other languages) can also model a hybrid of these two types, where some members/constructors have associated data (as in unions), some don't (as in enumerations), and some have optional values:
Here
third
is a nullary constructor -- it doesn't have an associated value, but it is distinct fromfirst
,second
andfourth
.fourth
has a value which can be null/not present. How might we model this in Smithy? We can't do something like:(where
Empty
is some dummy/to-be-ignored type e.g.structure Empty {}
) since@required
is banned inside unions (since unions cannot have null/not present values). Another option might be to use a trait, such as:but this seems a shame since the fact that
AnotherUnion
is essentially a union is somewhat obscured (also empty unions are forbidden). Does anyone have any thoughts or recommendations on how to approach this? Perhaps this is simply not the way to model this problem? All feedback and critique welcome!The text was updated successfully, but these errors were encountered: