v0.7.0 Latest Latest

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

Go to latest
Published: Dec 6, 2023 License: Apache-2.0 Imports: 4 Imported by: 16



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

CUE constraints can be used to validate Go types as well as fill out missing struct fields that are implied from the constraints and the values already defined by the struct value.



This section is empty.


This section is empty.


func Validate added in v0.2.1

func Validate(x interface{}) error

Validate calls Validate on a default Codec for the type of x.


type Codec

type Codec struct {
	// contains filtered or unexported fields

A Codec decodes and encodes CUE from and to Go values and validates and completes Go values based on CUE templates.

func New

func New[Ctx *cue.Runtime | *cue.Context](ctx Ctx, c *Config) *Codec

New creates a new Codec for the given instance.

It is safe to use the methods of Codec concurrently as long as the given Runtime is not used elsewhere while using Codec. However, only the concurrent use of Decode, Validate, and Complete is efficient.

Note: calling this with a *cue.Runtime value is deprecated.

func (*Codec) Complete

func (c *Codec) Complete(v cue.Value, x interface{}) error

Complete sets previously undefined values in x that can be uniquely determined form the constraints defined by v if validation passes, or returns an error, without modifying anything, otherwise.

Only undefined values are modified. A value is considered undefined if it is pointer type and is nil or if it is a field with a zero value that has a json tag with the omitempty flag.

The given value must be created using the same Runtime with which c was initialized.

Complete does a JSON round trip. This means that data not preserved in such a round trip, such as the location name of a time.Time, is lost after a successful update.

func (*Codec) Decode

func (c *Codec) Decode(x interface{}) (cue.Value, error)

Decode converts x to a CUE value.

If x is of type reflect.Value it will convert the value represented by x.

func (*Codec) Encode

func (c *Codec) Encode(v cue.Value, x interface{}) error

Encode converts v to a Go value.

func (*Codec) ExtractType

func (c *Codec) ExtractType(x interface{}) (cue.Value, error)

ExtractType extracts a CUE value from a Go type.

The type represented by x is converted as the underlying type. Specific values, such as map or slice elements or field values of structs are ignored. If x is of type reflect.Type, the type represented by x is extracted.

Fields of structs can be annoted using additional constrains using the 'cue' field tag. The value of the tag is a CUE expression, which may contain references to the JSON name of other fields in a struct.

type Sum struct {
    A int `cue:"c-b" json:"a,omitempty"`
    B int `cue:"c-a" json:"b,omitempty"`
    C int `cue:"a+b" json:"c,omitempty"`

func (*Codec) Validate

func (c *Codec) Validate(v cue.Value, x interface{}) error

Validate checks whether x satisfies the constraints defined by v.

The given value must be created using the same Runtime with which c was initialized.

type Config

type Config struct {

Config has no options yet, but is defined for future extensibility.

Jump to

Keyboard shortcuts

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