-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Conversation
I hear there's one more unit test incoming for that last case, but this code LGTM! Also could use some docs before merge. |
09dc9db
to
6db1089
Compare
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 Honestly, part of me is thinking "YAGNI" on this, and certainly later we could decide that |
@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] |
There was a problem hiding this comment.
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
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. 👍 |
@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"
} |
(On review I believe it should yield the |
6db1089
to
bb2293a
Compare
@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.
bb2293a
to
cb6cb8b
Compare
// 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) |
There was a problem hiding this comment.
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.
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.
Good call @phinze, should make it easier to grep through source. Will merge pending Travis. |
core: Support explicit variable type declaration
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. |
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:
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.