cue

package
v0.0.0-...-c3373f0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2024 License: Apache-2.0, MIT Imports: 3 Imported by: 3

Documentation

Overview

Example
// Package cue provides a set of functions for loading and manipulating CUE.
package main

import (
	"fmt"

	"cuelang.org/go/cue"
	"cuelang.org/go/cue/cuecontext"
)

var src = `
{
	a: {
		b: 1
		c: 2
	}
	d: [
		{
			e: 3
		}
	]
}
`

func main() {
	ctx := cuecontext.New()
	v, err := Compile(ctx, []byte(src))
	if err != nil {
		panic(err)
	}

	if err := Validate(v, cue.Concrete(true)); err != nil {
		panic(err)
	}

	// Replace a.b with 2
	v, err = Replace(ctx, v, "a.b", ctx.CompileString("2"))
	fmt.Printf("a.b: %v\n", v.LookupPath(cue.ParsePath("a.b")))

	// Replace d[0].e with 4
	v, err = Replace(ctx, v, "d[0].e", ctx.CompileString("4"))
	fmt.Printf("d[0].e: %v\n", v.LookupPath(cue.ParsePath("d[0].e")))

	// Delete a.c
	v, err = Delete(ctx, v, "a.c")
	fmt.Printf("a: %v\n", v.LookupPath(cue.ParsePath("a")))

}
Output:

a.b: 2
d[0].e: 4
a: {
	b: 2
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compile

func Compile(ctx *cue.Context, contents []byte) (cue.Value, error)

Compile compiles the given CUE contents and returns the resulting value. If the contents are invalid, an error is returned.

func Delete

func Delete(ctx *cue.Context, v cue.Value, path string) (cue.Value, error)

delete deletes the field at the given path from the given value. The path must point to either a struct field or a list index.

func Replace

func Replace(ctx *cue.Context, v cue.Value, path string, replace cue.Value) (cue.Value, error)

replace replaces the value at the given path with the given value. The path must point to either a struct field or a list index.

func Validate

func Validate(c cue.Value, opts ...cue.Option) error

Validate validates the given CUE value. If the value is invalid, an error is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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