Skip to content
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

cmd/cue/cmd: add go tag for or expressions #3521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LucasLundJensen
Copy link

@LucasLundJensen LucasLundJensen commented Oct 18, 2024

This adds a new Go tag which allows the use of OR expressions when generating Cue based on Go.

I'm aware that this change might not be the desired way of resolving the lack of having OR expressions available based on tags in cue tags in Go structs.
If that is the case, take this PR as more of a discussion on how to properly implement this.

I've got the need for this feature, as it'll allow me and my team to generate CUE schemas purely based on our Go structs with the desired defaults that we intend to have on those fields.
A more concrete example is the following:

type Module struct {
	Org string `json:"org,omitempty" yaml:"org,omitempty" cue_or:"*\"org_name\""`
}
#Module: {
	org?: string | *"org_name" @go(Org)
}

Where as the only tag available today cue only supports AND expressions.

This adds a new Go tag which allows the use of OR expressions when generating Cue based on Go.

Signed-off-by: Lucas Lund Jensen <[email protected]>
@mvdan
Copy link
Member

mvdan commented Oct 18, 2024

Our thinking in this space is to provide the opposite of cue get go, so that you can define your schemas in CUE as the source of truth, and generate Go types which approximate the shapes well enough so that you can decode CUE values into Go.

This is ultimately the most flexible approach, as you can define as many constraints on the CUE side as you want, without having to somehow fit everything into Go struct field tags. We also find that to be more consistent, because we tend to want to write CUE validation and other logic alongside the schema definition, so it's a bit odd if the schema's source of truth is in a Go file separate from the rest of the CUE.

I'm currently working on this as we need it ourselves - the first version will be experimental and fairly simple, but I believe we can extend it to support most use cases.

@mvdan
Copy link
Member

mvdan commented Oct 18, 2024

And I forgot to say - thank you for contributing!

@LucasLundJensen
Copy link
Author

Our thinking in this space is to provide the opposite of cue get go, so that you can define your schemas in CUE as the source of truth, and generate Go types which approximate the shapes well enough so that you can decode CUE values into Go.

This is ultimately the most flexible approach, as you can define as many constraints on the CUE side as you want, without having to somehow fit everything into Go struct field tags. We also find that to be more consistent, because we tend to want to write CUE validation and other logic alongside the schema definition, so it's a bit odd if the schema's source of truth is in a Go file separate from the rest of the CUE.

I'm currently working on this as we need it ourselves - the first version will be experimental and fairly simple, but I believe we can extend it to support most use cases.

This is exactly the route we wanted to go in the first place, but realized that it wasn't entirely possible yet.
Hence why this is our approach currently, but we'd be excited to switch over to having CUE be our source of truth later on.

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