next: Conditional Fields

Field Comprehensions

CUE also supports comprehensions for fields.

One cannot refer to generated fields with references. Instead, one must use indexing.

fieldcomp.cue

import "strings"

#a: [ "Barcelona", "Shanghai", "Munich" ]

for k, v in #a {
    "\( strings.ToLower(v) )": {
        pos:     k + 1
        name:    v
        nameLen: len(v)
    }
}

$ cue eval -c fieldcomp.cue

barcelona: {
    pos:     1
    name:    "Barcelona"
    nameLen: 9
}
shanghai: {
    pos:     2
    name:    "Shanghai"
    nameLen: 8
}
munich: {
    pos:     3
    name:    "Munich"
    nameLen: 6
}