Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype: "expandnull" function #28376

Closed
wants to merge 1 commit into from
Closed

Conversation

apparentlymart
Copy link
Contributor

@apparentlymart apparentlymart commented Apr 15, 2021

This is intended to complement the defaults function as part of the optional attributes experiment, to give module authors a way to concisely declare that they don't care to distinguish between null objects/tuples and objects/tuples containing nulls, and that they don't care to distinguish between null collections and empty collections, and thus they can avoid needing to do conditional traversals and focus only on handling the leaf primitive-typed values as being null.

This is just a prototype to see what this might look like and how it will interact with the defaults function. It's not yet ready to be merged because it lacks tests and documentation.

This is one candidate answer to the concern raised in #28344:

terraform {
  experiments      = [module_variable_optional_attrs]
}

variable "my_var" {
  type = object({
    some_config1 = optional(object({
      my_something = optional(bool)
    }))

    deep_config = optional(object({
      deep_1 = optional(object({
        something_1 = string
        something_2 = number
      }))

      deep_2 = optional(object({
        something_1 = string
        something_2 = number
      }))
    }))
  })
}

locals {
  my_var = defaults(expandnull(var.my_var), {
    some_config1 = {
      my_something = true
    }

    deep_config = {
      deep_1 = {
        something_1 = "asdf"
        something_2 = 1
      }

      deep_2 = {
        something_1 = "fdsa"
        something_2 = 5
      }
    }
  })
}

output "my_var" {
  value = local.my_var
}
my_var = {}
Outputs:

my_var = {
  "deep_config" = {
    "deep_1" = {
      "something_1" = "asdf"
      "something_2" = 1
    }
    "deep_2" = {
      "something_1" = "fdsa"
      "something_2" = 5
    }
  }
  "some_config1" = {
    "my_something" = true
  }
}

However, it does come at the expense of adding another very specialized function with a limited use-case, so we'll likely consider other designs too.

This is intended to complement the "defaults" function as part of the
optional attributes experiment, to give module authors a way to concisely
declare that they don't care to distinugish between null objects/tuples
and objects/tuples _containing_ nulls, and that they don't care to
distinguish between null collections and empty collections, and thus they
can avoid needing to do conditional traversals and focus only on handling
the leaf primitive-typed values as being null.

This is just a prototype to see what this might look like and how it will
interact with the "defaults" function. It's not yet ready to be merged
because it lacks tests and documentation.
@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 2, 2023

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants