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

core: Support explicit variable type declaration #4795

Merged
merged 2 commits into from
Jan 25, 2016
Merged

Conversation

jen20
Copy link
Contributor

@jen20 jen20 commented Jan 22, 2016

This commit adds support for declaring variable types in Terraform configuration. Historically, the type has been inferred from the default value, defaulting to string if no default was supplied. This has caused users to devise workarounds if they wanted to declare a map but provide values from a .tfvars file (for example).

The new syntax adds the "type" key to variable blocks:

variable "i_am_a_string" {
    type = "string"
}

variable "i_am_a_map" {
    type = "map"
}

This commit does not extend the type system to include bools, integers or floats - the only two types available are maps and strings.

Validation is performed if a default value is provided in order to ensure that the default value type matches the declared type.

In the case that a type is not declared, the old logic is used for determining the type. This allows backwards compatiblity with previous Terraform configuration.

@phinze
Copy link
Contributor

phinze commented Jan 22, 2016

I hear there's one more unit test incoming for that last case, but this code LGTM!

Also could use some docs before merge.

@jen20 jen20 changed the title [WIP] core: Support explicit variable type declaration core: Support explicit variable type declaration Jan 22, 2016
@apparentlymart
Copy link
Contributor

There is an issue filed out there somewhere where the user wanted to use a map of maps of string. Do you think we should prepare for the potential future case of having more complex types in variables by saying something like map<string> instead of just map?

Honestly, part of me is thinking "YAGNI" on this, and certainly later we could decide that map is a backward-compatibility alias for map<string>, if the future of use-cases like that doesn't feel so clear right now. But on the other hand, given that the use-cases for that are already documented in the bug tracker...

@jen20
Copy link
Contributor Author

jen20 commented Jan 22, 2016

@apparentlymart Interesting question. Personally my feeling here is that trying to introduce some form of generic type system is going to be overkill. I guess the use cases for something like that are for e.g. AMIs per region per environment. I am definitely willing to be convinced here though: any thoughts @phinze or @mitchellh?

@@ -115,6 +133,7 @@ The full syntax is:

```
variable NAME {
[type = TYPE]
[default = DEFAULT]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: tabs/spaces weird here

@phinze
Copy link
Contributor

phinze commented Jan 22, 2016

Nested maps are a good thought to bring up, @apparentlymart, but I think YAGNI / incremental progress is the best strategy for now. We can revisit nested maps after first-class list support is done, which is the current target feature for this chain of implementation work from @jen20. 👍

@phinze
Copy link
Contributor

phinze commented Jan 22, 2016

@jen20 Seems like we might want a test for a sane error message in the case of a type typo (type-o !?):

variable "typeo_no_default" {
  type = "stringg"
}
variable "typeo_with_default" {
  type = "stringg"
  default = "cheese"
}

@phinze
Copy link
Contributor

phinze commented Jan 22, 2016

(On review I believe it should yield the "Variable '%s': must be a string or a map" message, but might as well cover the case in a unit test.)

@jen20
Copy link
Contributor Author

jen20 commented Jan 24, 2016

@phinze Yes that (was) correct. However, I've moved the variable type check into the loader, at the same point as where we validate the default in order to catch errors earlier. Consequently these kinds of errors now get picked up in the config loading tests rather than context validation, which is better in my opinion. I've added a loader test asserting the correct error is received in this case, and squashed all the commits and fixed up some of the spacing weirdness in the docs.

This commit adds support for declaring variable types in Terraform
configuration. Historically, the type has been inferred from the default
value, defaulting to string if no default was supplied. This has caused
users to devise workarounds if they wanted to declare a map but provide
values from a .tfvars file (for example).

The new syntax adds the "type" key to variable blocks:

```
variable "i_am_a_string" {
    type = "string"
}

variable "i_am_a_map" {
    type = "map"
}
```

This commit does _not_ extend the type system to include bools, integers
or floats - the only two types available are maps and strings.

Validation is performed if a default value is provided in order to
ensure that the default value type matches the declared type.

In the case that a type is not declared, the old logic is used for
determining the type. This allows backwards compatiblity with previous
Terraform configuration.
// If an explicit type is declared, ensure it is valid
if v.DeclaredType != "" {
if _, ok := typeStringMap[v.DeclaredType]; !ok {
return fmt.Errorf("Variable '%s' must be of type string or map", v.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the DeclaredType here, can we include it in the error message? I've found that helps immensely with typo discovery.

@phinze
Copy link
Contributor

phinze commented Jan 25, 2016

One last nit, then this LGTM!

If a variable type which is invalid (e.g. "stringg") is declared, we now
include the invalid type description in the error message to make it
easier to track down the source of the error in the source file.
@jen20
Copy link
Contributor Author

jen20 commented Jan 25, 2016

Good call @phinze, should make it easier to grep through source. Will merge pending Travis.

jen20 added a commit that referenced this pull request Jan 25, 2016
core: Support explicit variable type declaration
@jen20 jen20 merged commit 327f114 into master Jan 25, 2016
@jen20 jen20 deleted the f-variable-types branch January 25, 2016 16:37
@ghost
Copy link

ghost commented Apr 28, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants