cue-lang / docs-and-content

A place to discuss, plan, and track documentation on cuelang.org
6 stars 1 forks source link

Unifying incomplete CUE and concrete data, using the `cue` command and the Go API #180

Open jpluscplusm opened 3 weeks ago

jpluscplusm commented 3 weeks ago

Slack thread: https://cuelang.slack.com/archives/CLT3ULF6C/p1718013879866749

Original Slack thread conclusion #### myitcv In general, CUE can deal with references to fields that don't exist (yet) e.g. ```CUE x: _ y: x.f ``` This is referred to as incomplete CUE. Such CUE cannot be exported, but it can be evaluated. It can also be made complete by unifying it with a value that makes it complete. ```CUE step1: { x: _ y: x.f } step2: { x: f: 5 } res: step1 & step2 ``` Here, step1 could be the result of a first load, for example step2 could come from anywhere else res is the result of unifying the two. Here's a quick demo ```txtar go mod tidy go run . cmp stdout stdout.golden -- go.mod -- module mod.example go 1.22.3 require cuelang.org/go v0.9.2 -- main.go -- package main import ( "fmt" "log" "cuelang.org/go/cue" "cuelang.org/go/cue/cuecontext" "cuelang.org/go/cue/load" "cuelang.org/go/encoding/yaml" ) func main() { ctx := cuecontext.New() // Load the CUE package in the current directory. // It contains only one file, x.cue. This is step1. bis := load.Instances([]string{"."}, nil) step1 := ctx.BuildInstance(bis[0]) fmt.Printf("step1: %v\n", step1) // Load x.yaml as step2. step2File, err := yaml.Extract("x.yaml", nil) if err != nil { log.Fatal(err) } step2 := ctx.BuildFile(step2File) fmt.Printf("step2: %v\n", step2) // Ensure the result of unifying the two steps is // valid and concrete, i.e. can be exported as data. res := step1.Unify(step2) if err := res.Validate(cue.Concrete(true)); err != nil { log.Fatal(err) } fmt.Printf("res: %v\n", res) } -- x.cue -- package x x: _ y: x.f -- x.yaml -- x: f: 5 -- stdout.golden -- step1: { x: _ y: x.f } step2: { x: { f: 5 } } res: { x: { f: 5 } y: 5 } ``` The idea being that we can show how to work with incomplete values and unification (the simplest of pipelines) to make things concrete.
myitcv commented 5 days ago

WIP CL https://review.gerrithub.io/c/cue-lang/cuelang.org/+/1203092