Associative Lists



CUE does not have a set type. The proposed method is to use associative lists.

An associative list is a list that defines a key for each of its values, effectively turning it into a map. In CUE terms, elements with the same key are unified and collapsed onto a single element as if it were a struct.

In the following, we have

  • original.cue contains two lists we’d like to unify
  • results.cue contains the desired results
  • syntax.cue contains the CUE to make this happen

The syntax uses the @.field to define the key used to determine which values should be unified to produce the result.

original.cue

a: [{
	name: "foo"
	val1: 2
}, {
	name: "bar"
	val1: 3
}]

a: [{
	name: "foo"
	val2: 4
}]

result.cue

a: [{
	name: "foo"
	val1: 2
	val2: 4
}, {
	name: "bar"
	val1: 3
}]

syntax.cue

a: [@.name]: {...}

You can find more details on GitHub:

Today, you can use structs to achieve unique elements to or from a list. See the unique list pattern for more details

We'll never share your email with anyone else.
2024 Hofstadter, Inc