...

Text file src/cuelang.org/go/cue/testdata/comprehensions/nested.txtar

Documentation: cuelang.org/go/cue/testdata/comprehensions

     1-- in.cue --
     2package kube
     3
     4service: "node-exporter": {
     5	spec: {
     6		ports: [{
     7			name:     "metrics"
     8			port:     9100
     9			protocol: "TCP"
    10		}]
    11	}
    12}
    13deployment: "node-exporter": {
    14	spec: template: {
    15		spec: {
    16			containers: [{
    17				ports: [{
    18					containerPort: 9100
    19					name:          "scrape"
    20				}]
    21			}]
    22		}
    23	}
    24}
    25service: [ID=string]: {
    26	spec: {
    27		ports: [...{
    28			port:     int
    29			protocol: *"TCP" | "UDP"
    30			name:     string | *"client"
    31		}]
    32	}
    33}
    34deployment: [ID=string]: {}
    35deployment: [ID=_]: _spec & {}
    36_spec: {
    37	_name: string
    38	spec: template: {
    39		spec: containers: [{name: _name}]
    40	}
    41}
    42_spec: spec: template: spec: containers: [...{
    43	ports: [...{
    44		_export: *true | false // include the port in the service
    45	}]
    46}]
    47for k, v in deployment {
    48	service: "\(k)": {
    49		spec: selector: v.spec.template.metadata.labels
    50		spec: ports: [
    51			for c in v.spec.template.spec.containers
    52			for p in c.ports
    53			if p._export {
    54				let Port = p.containerPort // Port is an alias
    55				port:       *Port | int
    56				targetPort: *Port | int
    57			},
    58		]
    59	}
    60}
    61-- out/eval/stats --
    62Leaks:  0
    63Freed:  45
    64Reused: 26
    65Allocs: 19
    66Retain: 33
    67
    68Unifications: 35
    69Conjuncts:    95
    70Disjuncts:    75
    71-- out/eval --
    72(struct){
    73  service: (struct){
    74    "node-exporter": (struct){
    75      spec: (struct){
    76        ports: (#list){
    77          0: (struct){
    78            name: (string){ "metrics" }
    79            port: (int){ 9100 }
    80            protocol: (string){ "TCP" }
    81            let Port#1 = (int){ 9100 }
    82            targetPort: (int){ |(*(int){ 9100 }, (int){ int }) }
    83          }
    84        }
    85        selector: (_|_){
    86          // [incomplete] service."node-exporter".spec.selector: undefined field: metadata:
    87          //     ./in.cue:48:35
    88        }
    89      }
    90    }
    91  }
    92  deployment: (struct){
    93    "node-exporter": (struct){
    94      spec: (struct){
    95        template: (struct){
    96          spec: (struct){
    97            containers: (#list){
    98              0: (struct){
    99                ports: (#list){
   100                  0: (struct){
   101                    containerPort: (int){ 9100 }
   102                    name: (string){ "scrape" }
   103                    _export(:kube): (bool){ |(*(bool){ true }, (bool){ false }) }
   104                  }
   105                }
   106                name: (string){ string }
   107              }
   108            }
   109          }
   110        }
   111      }
   112      _name(:kube): (string){ string }
   113    }
   114  }
   115  _spec(:kube): (struct){
   116    _name(:kube): (string){ string }
   117    spec: (struct){
   118      template: (struct){
   119        spec: (struct){
   120          containers: (#list){
   121            0: (struct){
   122              name: (string){ string }
   123              ports: (list){
   124              }
   125            }
   126          }
   127        }
   128      }
   129    }
   130  }
   131}
   132-- out/compile --
   133--- in.cue
   134{
   135  service: {
   136    "node-exporter": {
   137      spec: {
   138        ports: [
   139          {
   140            name: "metrics"
   141            port: 9100
   142            protocol: "TCP"
   143          },
   144        ]
   145      }
   146    }
   147  }
   148  deployment: {
   149    "node-exporter": {
   150      spec: {
   151        template: {
   152          spec: {
   153            containers: [
   154              {
   155                ports: [
   156                  {
   157                    containerPort: 9100
   158                    name: "scrape"
   159                  },
   160                ]
   161              },
   162            ]
   163          }
   164        }
   165      }
   166    }
   167  }
   168  service: {
   169    [string]: {
   170      spec: {
   171        ports: [
   172          ...{
   173            port: int
   174            protocol: (*"TCP"|"UDP")
   175            name: (string|*"client")
   176          },
   177        ]
   178      }
   179    }
   180  }
   181  deployment: {
   182    [string]: {}
   183  }
   184  deployment: {
   185    [_]: (〈1;_spec〉 & {})
   186  }
   187  _spec: {
   188    _name: string
   189    spec: {
   190      template: {
   191        spec: {
   192          containers: [
   193            {
   194              name: 〈5;_name〉
   195            },
   196          ]
   197        }
   198      }
   199    }
   200  }
   201  _spec: {
   202    spec: {
   203      template: {
   204        spec: {
   205          containers: [
   206            ...{
   207              ports: [
   208                ...{
   209                  _export: (*true|false)
   210                },
   211              ]
   212            },
   213          ]
   214        }
   215      }
   216    }
   217  }
   218  for k, v in 〈0;deployment〉 {
   219    service: {
   220      "\(〈2;k〉)": {
   221        spec: {
   222          selector: 〈4;v〉.spec.template.metadata.labels
   223        }
   224        spec: {
   225          ports: [
   226            for _, c in 〈5;v〉.spec.template.spec.containers for _, p in 〈0;c〉.ports if 〈0;p〉._export {
   227              let Port#1 = 〈1;p〉.containerPort
   228              port: (*〈0;let Port#1〉|int)
   229              targetPort: (*〈0;let Port#1〉|int)
   230            },
   231          ]
   232        }
   233      }
   234    }
   235  }
   236}

View as plain text