Home > Software design >  If loop to ignore null value for conditional checks
If loop to ignore null value for conditional checks

Time:12-14

locals {

  acl_out = flatten([
    for opi, clsan in var.appr : [
      for co, coo in clsan : [
        for applo in coo : [
           for op in ( co == "RO" ? ["READ","DESCRIBE"] : ["WRITE"]) : {
             oper = op
             appid_lo = applo
             opic-name = opi
             process = co
             
           }

        ]
      ]

    ]
  ])

}
variable "appr" {
  description = "A complex object "
  type = map(object({
    #displayname = string

      RO  = optional(list(string))
      WO    = optional(list(string)) 

    
  }))
}
  • If co = RO then ["READ","DESCRIBE"]
  • If co = WO then [WRITE]

Above expression fails with null value, If WO not defined/unset in product 2, If statement need to corrected

Input :

Working

appr = {
  product1 = { WO = ["6470"], RO = ["6471","5538"] },
  product2 = { WO = ["5555"], RO = ["6472"]}, 
  product3 = { WO = ["6473"], RO = ["6474"] },
}

Not Working

appr = {
  product1 = { WO = ["6470"], RO = ["6471","5538"] },
  product2 = { RO = ["6472"]},
  product3 = { WO = ["6473"], RO = ["6474"] },
}

Error:

A null value cannot be used as the collection in a 'for' expression.

Tried this way also fails

for op in ( co == "RO" = ["READ","DESCRIBE"] || co == "WO" = ["WRITE"] ) :  { 

Desired Result :


{
   opic-name = "product1"
   oper = "WRITE"
   appid_lo = 6470
},
{
   opic-name = "product1"
   oper = "READ"
   appid_lo = 6471
},
 {
   opic-name = "product1"
   oper = "DESCRIBE"
   appid_lo = 6471
}

and so on

CodePudding user response:

This question was a fun one!

Check if it is null before running the next for

locals {
  acl_out = flatten([
    for opi, clsan in var.appr : [
      for co, coo in clsan : [
        coo == null ? [] : [
        for applo in coo : [
           for op in ( co == "RO" ? ["READ","DESCRIBE"] : ["WRITE"]) : {
             oper = op
             appid_lo = applo
             opic-name = opi
             process = co
           }
        ]
      ]]
    ]
  ])
}

Output for acl_out

acl_out = [
        {
            appid_lo  = "6471"
            oper      = "READ"
            opic-name = "product1"
            process   = "RO"
        },
        {
            appid_lo  = "6471"
            oper      = "DESCRIBE"
            opic-name = "product1"
            process   = "RO"
        },
        {
            appid_lo  = "5538"
            oper      = "READ"
            opic-name = "product1"
            process   = "RO"
        },
        {
            appid_lo  = "5538"
            oper      = "DESCRIBE"
            opic-name = "product1"
            process   = "RO"
        },
        {
            appid_lo  = "6470"
            oper      = "WRITE"
            opic-name = "product1"
            process   = "WO"
        },
        {
            appid_lo  = "6472"
            oper      = "READ"
            opic-name = "product2"
            process   = "RO"
        },
        {
            appid_lo  = "6472"
            oper      = "DESCRIBE"
            opic-name = "product2"
            process   = "RO"
        },
        {
            appid_lo  = "6474"
            oper      = "READ"
            opic-name = "product3"
            process   = "RO"
        },
        {
            appid_lo  = "6474"
            oper      = "DESCRIBE"
            opic-name = "product3"
            process   = "RO"
        },
        {
            appid_lo  = "6473"
            oper      = "WRITE"
            opic-name = "product3"
            process   = "WO"
        },
    ]

Cheers!

  • Related