Comprehension



CUE’s comprehensions have several nuances that you want to be aware of.

Checking Against Bottom

You can check if a value is present, or more specifically concrete, by comparing it to bottom. Note, this is an imperfect comparision and CUE intends to add more builtins to fix this.

input.cue

a: {
	p?: int
}

// if b is concrete, add config
if a.b != _|_ {
	b: "found"
}
// you can also check optional fields
if a.p != _|_ {
	p: a.p & >1024
}

// if c is not concrete, add config
if a.c == _|_ {
	c: "missing"
}

// you cannot set a value if it is not present
// a: {
// 	d: _
// 	if d == _|_ {
// 		d: "default"
// 	}
// }

// use preference marks (defaults) for that instead
a: {
	d: _ | *"default"
}

cue export input.cue

{
    "c": "missing",
    "a": {
        "d": "default"
    }
}

Cross-field Checks

You can perform cross-field checks or add constraints to another field based on the value of another.

  • if base
  • disjunction based, where might we be able to do something with conditions that we cannot with disjunctions (if field is missing…?)

Field Comprehension Fields

CUE only loops over regular fields during field comprehension.

… code …

If you want to iterate over optional, hidden, or definition fields you can use the Go API … link …

Loops and Conditions

  • looping, if in loop, vs in result

Multiple, Inline For Loops

  • looping, multiple inline
We'll never share your email with anyone else.
2024 Hofstadter, Inc