Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

cueutils

Go Reference

Provides helpers to work with Cue.

Import: github.com/DavidGamba/dgtools/cueutils

Examples

//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)
	}

	// Print the config values
	opts := cueutils.StringValueOpts{
		Definitions:    true,
		Hidden:         true,
		Attributes:     true,
		Optional:       true,
		ErrorsAsValues: true,
		Concrete:       true,
	}

	v, err := cueutils.StringValue(value, opts)
	if err != nil {
		return err
	}
	fmt.Printf("value:\n%v\n", string(v))