wiwa1978
wiwa1978

Reputation: 2707

Flatten function to contain a list

I have the following variables:

companies = [
  {
    name        = "Company1"
    description = "Description for company 1"
    employees = [
      {
        name   = "Employee1"
      }
    ]
  },
  {
    name        = "Company2"
    description = "Description for company 2"
    employees = [
      {
        name   = "Employee2"
      },
      {
        name   = "Employee3"
      }
    ]
}]

This is the variable definition:

variable "companies" {
  type = list(object({
    name        = string,
    description = string,
    employees = list(object({
      name   = string,
      age    = string,
    }))
  }))
}

I have the following flatten function:

locals {
  nestedlist = flatten([
    for company_key, company_value in var.companies : [
      for employee_key, employee_value in company_value.employees : {
        company_name        =   company_value.name
        company_description =   company_value.description
        employees           =   employee_value["name"]
      }
    ]
  ])
}

This produces:

flatten_output = [
      + {
          + company_description   = "Description for company 1"
          + company_name          = "Company1"
          + employees             = "Employee1"
        },
      + {
          + company_description   = "Description for company 2"
          + company_name          = "Company2"
          + employees             = "Employee2"
        },
      + {
          + company_description   = "Description for company 2"
          + company_name          = "Company2"
          + employees             = "Employee3"
        },
    ]

However I would want the following output:

flatten_output = [
      + {
          + company_description   = "Description for company 1"
          + company_name          = "Company1"
          + employees             = ["Employee1"]
        },
      + {
          + company_description   = "Description for company 2"
          + company_name          = "Company2"
          + employees             = ["Employee2","Employee3"]
        }
    ]

How can I ensure the employees in the output are a list containing all employee names?

Upvotes: 1

Views: 1261

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28864

We just need to restructure the nestedlist for expression to be a list(object), and to use a nested for expression to construct the list of employee names within a key in this object:

locals {
  nestedlist = [
    for company_key, company_value in local.companies : {
      company_name        = company_value.name
      company_description = company_value.description
      employees           = [for employee_key, employee_value in company_value.employees : employee_value["name"]]
    }
  ]
}

this produces:

output = [
  {
    company_description = "Description for company 1"
    company_name        = "Company1"
    employees           = [
      "Employee1",
    ]
  },
  {
    company_description = "Description for company 2"
    company_name        = "Company2"
    employees           = [
      "Employee2",
      "Employee3",
    ]
  },
]

as requested. I would probably also rename nestedlist since it is no longer a nested list.

Upvotes: 2

Related Questions