Skip to content

Walking cue.Value and Optional Fields #2783

Answered by myitcv
Nalum asked this question in Q&A
Discussion options

You must be logged in to vote

@Nalum thanks for raising. Take a look at this example:

go mod tidy
go run .
cmp stdout stdout.golden

-- go.mod --
module mod.com

go 1.21.4

require cuelang.org/go v0.7.0
-- main.go --
package main

import (
	"fmt"
	"log"

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

const src = `
x?: string
y!: string
z: string
`

func main() {
	ctx := cuecontext.New()
	v := ctx.CompileString(src)
	fields, err := v.Fields(
		cue.Optional(true),
		cue.Hidden(true),
		cue.Definitions(true),
	)
	if err != nil {
		log.Fatal(err)
	}
	for fields.Next() {
		fmt.Printf("found field %v\n", fields.Selector())
	}
}
-- stdout.golden --
found field x?
found field y!
found field z

We will also be addin…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Nalum
Comment options

@jpluscplusm
Comment options

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