github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/doc/tutorial/basics/6_expressions/50_fieldcomp.txt (about)

     1  cue eval -c fieldcomp.cue
     2  cmp stdout expect-stdout-cue
     3  
     4  -- frontmatter.toml --
     5  title = "Field Comprehensions"
     6  description = ""
     7  
     8  -- text.md --
     9  CUE also supports comprehensions for fields.
    10  
    11  One cannot refer to generated fields with references.
    12  Instead, one must use indexing.
    13  
    14  -- fieldcomp.cue --
    15  import "strings"
    16  
    17  #a: [ "Barcelona", "Shanghai", "Munich" ]
    18  
    19  for k, v in #a {
    20      "\( strings.ToLower(v) )": {
    21          pos:     k + 1
    22          name:    v
    23          nameLen: len(v)
    24      }
    25  }
    26  
    27  -- expect-stdout-cue --
    28  barcelona: {
    29      name:    "Barcelona"
    30      pos:     1
    31      nameLen: 9
    32  }
    33  shanghai: {
    34      name:    "Shanghai"
    35      pos:     2
    36      nameLen: 8
    37  }
    38  munich: {
    39      name:    "Munich"
    40      pos:     3
    41      nameLen: 6
    42  }