Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Terrafrom: Can I set variable type = any dict/object

Is there a way to allow any type of dictionary/object as an input variable?

I have a module to create cron expressions with lambda and I’m trying to add a variable to take in a dictionary to pass into the resource call.

I’d like just allow any dictionary of any length. With any type for the keys and values in it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Initially, I tried just:

variable vars {
  type = object
}

But that isn’t allowed.

Right now I just have the type empty, so it will accept anything, but that doesn’t seem like good practice.

>Solution :

Ideally this would be the complex type map(any) to specify it must be a map with any type nested. However, you state that:

I’d like just allow any dictionary of any length. With any type for the keys and values in it.

Unfortunately there is a stipulation that map(any) type for a variable declaration must have a consistent structure across all values for the entries. Therefore, you could use map(any) if the input structure is consistent like:

{ 
  "one" = { "a_key" = "a_value", "another_key" = "another_value" },
  "two" = { "a_key" = "value", "another_key" = "the_value" },
}

However an inconsistent structure:

{ 
  "one" = { "another_key" = "another_value" },
  "two" = { "a_key" = "value", "another_key" = 0 },
}

would force the any type, which is the least restrictive and what you stated you did not want, but it is your only option in that situation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading