You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, if cue exp gengotypes is run on the following code:
package x
#T: #Foo | #Bar
#Foo: {
type!: "foo"
x?: bool
}
#Bar: {
type!: "bar"
x?: int
}
It will produce Go code like this:
package x
type T map[string]any
type Foo struct {
Type string `json:"type"`
X bool `json:"x,omitempty"`
}
type Bar struct {
Type string `json:"type"`
X int64 `json:"x,omitempty"`
}
Although the type T should be linked closely with the Foo and Bar types, it
is not.
It would be better if gengotypes could look at the schemas,
see that they're discriminated with the type field and generate Go code
that looks for the field at json umarshal time and creates the correct
struct type.
As one example of how a reasonably modern Go API does this, see the OpenAI Go API, for example the responses package. This appears to have been generated by https://www.stainless.com/.
Currently, if
cue exp gengotypes
is run on the following code:It will produce Go code like this:
Although the type
T
should be linked closely with theFoo
andBar
types, itis not.
It would be better if
gengotypes
could look at the schemas,see that they're discriminated with the
type
field and generate Go codethat looks for the field at json umarshal time and creates the correct
struct type.
The cuediscrim package is a step in that direction.
The text was updated successfully, but these errors were encountered: