Skip to content

cmd/cue: gengotypes should be able to generate Go code for disjunctions #3887

Open
@rogpeppe

Description

@rogpeppe

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.

The cuediscrim package is a step in that direction.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions