Skip to content

How to extract values from an array definition #2710

Answered by myitcv
gamma-khan asked this question in Q&A
Discussion options

You must be logged in to vote

This should do the trick:


go mod tidy
go run .
cmp stdout stdout.golden

-- go.mod --
module example

go 1.21.3

require cuelang.org/go v0.6.0

-- main.go --
package main

import (
	"fmt"
	"log"

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

func main() {

	ctx := cuecontext.New()

	cueString := `
	#def:{ foobar:[ ... #FOO | #BAR] }
	#FOO: "foo" | "FOO"
	#BAR: "bar" | "BAR"
	`
	v := ctx.CompileString(cueString)

	// The use of cue.AnyIndex "selects" the constraint
	field := v.LookupPath(cue.MakePath(cue.Def("def"), cue.Str("foobar"), cue.AnyIndex))

	var disjunctionElements []cue.Value
	var work cue.Value
	todo := []cue.Value{field}
	for len(todo) > 0 {
		work, todo = todo[0]…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@gamma-khan
Comment options

@myitcv
Comment options

@gamma-khan
Comment options

Answer selected by gamma-khan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants