Version: v0.5.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2023 License: Apache-2.0 Imports: 11 Imported by: 1



Package gocode defines functions for extracting CUE definitions from Go code and generating Go code from CUE values.

This package is used for offline processing. For converting Go values to and from CUE at runtime, use the gocodec package.



This section is empty.


This section is empty.


func Generate

func Generate(pkgPath string, inst *cue.Instance, c *Config) (b []byte, err error)

Generate generates Go code for the given instance in the directory of the given package.

Generate converts top-level declarations to corresponding Go code. By default, it will only generate validation functions of methods for exported top-level declarations. The behavior can be altered with the @go attribute.

The go attribute has the following form @go(<name>{,<option>}), where option is either a key-value pair or a flag. The name maps the CUE name to an alternative Go name. The special value '-' is used to indicate the field should be ignored for any Go generation.

The following options are supported:

type=<gotype>    The Go type as which this value should be interpreted.
                 This defaults to the type with the (possibly overridden)
                 name of the field.
validate=<name>  Alternative name for the validation function or method
                 Setting this to the empty string disables generation.
complete=<name>  Alternative name for the validation function or method.
                 Setting this to the empty string disables generation.
func             Generate as a function instead of a method.

Selection and Naming

Generate will not generate any code for fields that have no go attribute and that are not exported or for which there is no namesake Go type. If the go attribute has the special value '-' as its name it will be dropped as well. In all other cases Generate will generate Go code, even if the resulting code will not compile. For instance, Generate will generate Go code even if the user defines a Go type in the attribute that does not exist.

If a field selected for generation and the go name matches that the name of the Go type, the corresponding validate and complete code are generated as methods by default. If not, it will be generated as a function. The default function name is the default operation name with the Go name as a suffix.

Caveats Currently not supported:

  • option to generate Go structs (or automatically generate if undefined)
  • for type option to refer to types outside the package.


type Config

type Config struct {
	// Prefix is used as a prefix to all generated variables. It defaults to
	// cuegen.
	Prefix string

	// ValidateName defines the default name for validation methods or prefix
	// for validation functions. The default is "Validate". Set to "-" to
	// disable generating validators.
	ValidateName string

	// CompleteName defines the default name for complete methods or prefix
	// for complete functions. The default is "-" (disabled).
	CompleteName string

	// The cue.Runtime variable name to use for initializing Codecs.
	// A new Runtime is created by default.
	RuntimeVar string

Config defines options for generation Go code.


Path Synopsis
Package codec converts Go to and from CUE and validates Go values based on CUE constraints.
Package codec converts Go to and from CUE and validates Go values based on CUE constraints.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL