Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jpluscplusm opened this issue Oct 3, 2024 · 1 comment
Assignees
Labels
content idea Idea for a new piece of content howto For content that in the "howto" diataxis quadrant

Comments

@jpluscplusm
Copy link
Collaborator

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.

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.

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

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.

@jpluscplusm jpluscplusm added content idea Idea for a new piece of content howto For content that in the "howto" diataxis quadrant labels Oct 3, 2024
cueckoo pushed a commit to cue-lang/cuelang.org-trybot that referenced this issue Oct 25, 2024
WIP

Fixes cue-lang/docs-and-content#180.

Preview-Path: /docs/concept/working-with-incomplete-cue
Signed-off-by: Paul Jolly <[email protected]>
Change-Id: If2fdd49e42c96cd098a19464038827ad2000e951
Dispatch-Trailer: {"type":"trybot","CL":1203092,"patchset":2,"ref":"refs/changes/92/1203092/2","targetBranch":"master"}
cueckoo pushed a commit to cue-lang/cuelang.org-trybot that referenced this issue Oct 25, 2024
WIP

Fixes cue-lang/docs-and-content#180.

Preview-Path: /docs/concept/working-with-incomplete-cue
Signed-off-by: Paul Jolly <[email protected]>
Change-Id: If2fdd49e42c96cd098a19464038827ad2000e951
Dispatch-Trailer: {"type":"trybot","CL":1203092,"patchset":3,"ref":"refs/changes/92/1203092/3","targetBranch":"master"}
@myitcv myitcv self-assigned this Oct 25, 2024
@myitcv
Copy link
Member

myitcv commented Oct 25, 2024

cueckoo pushed a commit to cue-lang/cuelang.org-trybot that referenced this issue Oct 28, 2024
This adds a concept guide explaining the basics of working with
incomplete CUE using both the cue command and the Go API.

Fixes cue-lang/docs-and-content#180.

Preview-Path: /docs/concept/working-with-incomplete-cue
Signed-off-by: Paul Jolly <[email protected]>
Change-Id: If2fdd49e42c96cd098a19464038827ad2000e951
Dispatch-Trailer: {"type":"trybot","CL":1203092,"patchset":5,"ref":"refs/changes/92/1203092/5","targetBranch":"master"}
cueckoo pushed a commit to cue-lang/cuelang.org-trybot that referenced this issue Oct 28, 2024
This adds a concept guide explaining the basics of working with
incomplete CUE using both the cue command and the Go API.

Fixes cue-lang/docs-and-content#180.

Preview-Path: /docs/concept/working-with-incomplete-cue
Signed-off-by: Paul Jolly <[email protected]>
Change-Id: If2fdd49e42c96cd098a19464038827ad2000e951
Dispatch-Trailer: {"type":"trybot","CL":1203092,"patchset":6,"ref":"refs/changes/92/1203092/6","targetBranch":"master"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content idea Idea for a new piece of content howto For content that in the "howto" diataxis quadrant
Projects
Status: Backlog
Development

No branches or pull requests

2 participants