# Tests that nested for comprehensions within field patterns work correctly.
# The test uses a kubernetes-like structure with services and deployments.
-- in.cue --
package kube

service: "node-exporter": {
	spec: {
		ports: [{
			name:     "metrics"
			port:     9100
			protocol: "TCP"
		}]
	}
}
deployment: "node-exporter": {
	spec: template: {
		spec: {
			containers: [{
				ports: [{
					containerPort: 9100
					name:          "scrape"
				}]
			}]
		}
	}
}
service: [ID=string]: {
	spec: {
		ports: [...{
			port:     int
			protocol: *"TCP" | "UDP"
			name:     string | *"client"
		}]
	}
}
deployment: [ID=string]: {}
deployment: [ID=_]: _spec & {}
_spec: {
	_name: string
	spec: template: {
		spec: containers: [{name: _name}]
	}
}
_spec: spec: template: spec: containers: [...{
	ports: [...{
		_export: *true | false // include the port in the service
	}]
}]
for k, v in deployment {
	service: "\(k)": {
		spec: selector: v.spec.template.metadata.labels
		spec: ports: [
			for c in v.spec.template.spec.containers
			for p in c.ports
			if p._export {
				let Port = p.containerPort // Port is an alias
				port:       *Port | int
				targetPort: *Port | int
			},
		]
	}
}
-- out/compile --
--- in.cue
{
  service: {
    "node-exporter": {
      spec: {
        ports: [
          {
            name: "metrics"
            port: 9100
            protocol: "TCP"
          },
        ]
      }
    }
  }
  deployment: {
    "node-exporter": {
      spec: {
        template: {
          spec: {
            containers: [
              {
                ports: [
                  {
                    containerPort: 9100
                    name: "scrape"
                  },
                ]
              },
            ]
          }
        }
      }
    }
  }
  service: {
    [string]: {
      spec: {
        ports: [
          ...{
            port: int
            protocol: (*"TCP"|"UDP")
            name: (string|*"client")
          },
        ]
      }
    }
  }
  deployment: {
    [string]: {}
  }
  deployment: {
    [_]: (〈1;_spec(:kube)〉 & {})
  }
  _spec(:kube): {
    _name(:kube): string
    spec: {
      template: {
        spec: {
          containers: [
            {
              name: 〈5;_name(:kube)〉
            },
          ]
        }
      }
    }
  }
  _spec(:kube): {
    spec: {
      template: {
        spec: {
          containers: [
            ...{
              ports: [
                ...{
                  _export(:kube): (*true|false)
                },
              ]
            },
          ]
        }
      }
    }
  }
  for k, v in 〈0;deployment〉 {
    service: {
      "\(〈2;k〉)": {
        spec: {
          selector: 〈4;v〉.spec.template.metadata.labels
        }
        spec: {
          ports: [
            for _, c in 〈5;v〉.spec.template.spec.containers for _, p in 〈0;c〉.ports if 〈0;p〉._export(:kube) {
              let Port#1 = 〈1;p〉.containerPort
              port: (*〈0;let Port#1〉|int)
              targetPort: (*〈0;let Port#1〉|int)
            },
          ]
        }
      }
    }
  }
}
