cueutils

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 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"


	// 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, 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 NewValue

func NewValue() *cue.Value

func Unmarshal

func Unmarshal(configs []CueConfigFile, dir, packageName 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. 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.

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