cueutils

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2025 License: MPL-2.0 Imports: 12 Imported by: 4

README

= cueutils

image:https://pkg.go.dev/badge/github.com/DavidGamba/dgtools/cueutils.svg[Go Reference, link="https://pkg.go.dev/github.com/DavidGamba/dgtools/cueutils"]

Provides helpers to work with Cue.

Import: `github.com/DavidGamba/dgtools/cueutils`

== Examples

[source, go]
----
//go:embed schemas/schema.cue
var f embed.FS

	configs := []cueutils.CueConfigFile{}

	// Read embedded schemas or explicit config files
	schemaFilename := "schemas/schema.cue"
	schemaFH, err := f.Open(schemaFilename)
	if err != nil {
		return fmt.Errorf("failed to open '%s': %w", schemaFilename, err)
	}
	defer schemaFH.Close()
	configs = append(configs, cueutils.CueConfigFile{Data: schemaFH, Name: schemaFilename})

	// Read all cue files from a given dir
	dir := "config"

	// Filter to a given package name or set to _ for files without a package
	packageName := "myPackage"

	// Virtual module name to be used for the cue files, can be left blank if there is alreay a module in the dir
	virtualCueModuleName := "my.module"

	// Provide a pointer receiver for evaluated data for mostly debugging purposes
	value := cueutils.NewValue()

	// Unmarshal into local data structure
	d := MyDataStructure{}

	err = cueutils.Unmarshal(configs, dir, packageName, virtualCueModuleName, value, &d)
	if err != nil {
		return fmt.Errorf("failed to unmarshal: %w", err)
	}
----

Documentation

Overview

Package cueutils provides helpers to work with Cue

Index

Constants

This section is empty.

Variables

View Source
var Logger = log.New(io.Discard, "", log.LstdFlags)

Functions

func BuildOverlay added in v0.5.0

func BuildOverlay(configs []CueConfigFile, overlayRootDir, virtualCueModuleName string) (map[string]load.Source, []string, error)

BuildOverlay - Builds an overlay of cue configs files. If virtualCueModuleName is not empty, it will add a module overlay with the given name. Returns an overlay map and the package paths found. Initially the package paths are set to the current directory but hidden files will be added so that they can be read.

func FillPaths added in v0.5.0

func FillPaths(value *cue.Value, fillMap map[string]any) error

Fill multiple paths in a cue.Value with the given data. The fillMap is a map of paths and data to fill.

func GetValue added in v0.5.0

func GetValue(configs []CueConfigFile, dir, packageName, virtualCueModuleName string, value *cue.Value) error

Given a set of cue files, it will aggregate them into a single cue config and update the given cue.Value. This allows for incomplete configuration that can be completed by the caller.

If dir == "" it will default to the current directory. packageName can be set to _ to load files without a package. virtualCueModuleName can be set to a module name to add a module overlay or blank if the dir has a module already. Because CUE doesn't support hidden files, hidden files need to be passed as configs so that they can be loaded as an overlay.

Completing the value can be done in a couple of ways:

Using a go struct:

p := &GoStruct{}
c := cuecontext.New()
targetCue := c.Encode(p)
*value = (*value).Unify(targetCue)

Using value.Fillpath (or cueutils.FillPaths):

data := "some data"
*value = value.FillPath(cue.ParsePath("path.in.cue"), data)

func NewValue

func NewValue() *cue.Value

func Unmarshal

func Unmarshal(configs []CueConfigFile, dir, packageName, virtualCueModuleName string, value *cue.Value, target any) error

Given a set of cue files, it will aggregate them into a single cue config and then Unmarshal it unto the given data structure. If dir == "" it will default to the current directory. packageName can be set to _ to load files without a package. virtualCueModuleName can be set to a module name to add a module overlay or blank if the dir has a module already. Because CUE doesn't support hidden files, hidden files need to be passed as configs. value is a pointer receiver to a cue.Value and can be used on the caller side to print the cue values. Initialize with cueutils.NewValue()

func Validate added in v0.5.0

func Validate(value *cue.Value) error

Validate a cue.Value

Types

type CueConfigFile

type CueConfigFile struct {
	Data io.Reader
	Name string
}

Jump to

Keyboard shortcuts

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