-
Hi, I am using golang api to fill some values in cue. I would like to fill any values for an instance and later validate once the cue is complete. Please refer to below code. The code throws error as "" does not comply to regex.
output
|
Beta Was this translation helpful? Give feedback.
Answered by
verdverm
Mar 10, 2023
Replies: 1 comment 2 replies
-
You want to use the https://cuetorials.com/go-api/loading/ package main
import (
"fmt"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
)
func main() {
// We need a cue.Context for building after loading
ctx := cuecontext.New()
// The entrypoints are the same as the files you'd specify at the command line
entrypoints := []string{"hello.cue"}
// Load Cue files into Cue build.Instances slice
// the second arg is a configuration object, we'll see this later
bis := load.Instances(entrypoints, nil)
// Loop over the instances, typically there is only one
for _, bi := range bis {
// check for errors on the instance
// these are typically parsing errors
if bi.Err != nil {
fmt.Println("Error during load:", bi.Err)
continue
}
// Use cue.Context.BuildInstance to turn
// a build.Instance into a cue.Value
value := ctx.BuildInstance(bi)
if value.Err() != nil {
fmt.Println("Error during build:", value.Err())
continue
}
// Validate the value
err := value.Validate()
if err != nil {
fmt.Println("Error during validation:", err)
continue
}
// Print the value
fmt.Println("value:", value)
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
sahroshan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You want to use the
cue/load
packagehttps://cuetorials.com/go-api/loading/