Some mathematical looking operators
have secondary uses.
nonnum-ops.cue
// repeat strings and listss: 3*"for he's a jolly good fellow\n"+"which nobody can deny"l: 5* ["eye"]// concat listsa: ["a", "b", "d"] + [1, 2, 4]
cue eval nonnum-ops.cue
s: """ for he's a jolly good fellow for he's a jolly good fellow for he's a jolly good fellow which nobody can deny """l: ["eye", "eye", "eye", "eye", "eye"]a: ["a", "b", "d", 1, 2, 4]
String Expressions
Regular Expressions
Cue supports regular expression constraints with the =~ and !~ operators.
You can also interpolate field names. (as we will see shortly)
Comprehensions
CUE has several forms of comprehension to add or extend values.
More details and advanced cases can be found in
deep-dives/comprehension.
List Comprehensions
Cue has list comprehensions to dynamically create lists.
You can iterate over both lists and struct fields.
The form is [ for key, val in <iterable> [condition] { production } ]
* key is the index for lists and the label for fields
list-comp.cue
nums: [1, 2, 3, 4, 5, 6]sqrd: [ for _, n in nums {n * n}]even: [ for _, n in nums ifmod(n, 2) ==0 {n}]listOfStructs: [ for p, n in nums { pos: p val: n}]extractVals: [ for p, S in listOfStructs {S.val}]
CUE’s if is different from other languages.
It is a comprehension rather than a branching mechanism.
That is why we refer to it as
conditional fields, guarded fields, or another form of field comprehension.
Some important differences:
there is no else statement, you only include config when statements are true
there is no short-circuiting for multiple checks, all conditions will be evaluated
guards.cue
app: { name: string tech: string mem: intif tech =="react" { tier: "frontend" }if tech !="react" { tier: "backend" }if mem <1Gi { footprint: "small" }if mem >=1Gi&& mem <4Gi { footprint: "medium" }if mem >=4Gi { footprint: "large" }}// This will result in an error because CUE evaluates all conditions// without short-circuiting, meaning it will still try to access app.field// if app.field != _|_ && app.field == true {// foo: true// }// Use nested guards to check multiple conditionsif app.field !=_|_ {if app.field ==true { foo: true }}