-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lang/funcs: add (console-only) TypeFunction (#28501)
* lang/funcs: add (console-only) TypeFunction The type() function, which is only available for terraform console, prints out the type of a given value. This is mainly intended for debugging - it's nice to be able to print out terraform's understanding of a complex variable. This introduces a new field for Scope: ConsoleMode. When ConsoleMode is true, any additional functions intended for use in the console (only) may be added.
- Loading branch information
1 parent
15b6a16
commit f6af7b4
Showing
8 changed files
with
318 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
layout: "language" | ||
page_title: "type - Functions - Configuration Language" | ||
sidebar_current: "docs-funcs-conversion-type" | ||
description: |- | ||
The type function returns the type of a given value. | ||
--- | ||
|
||
# `type` Function | ||
|
||
-> **Note:** This function is available only in Terraform 1.0 and later. | ||
|
||
`type` retuns the type of a given value. | ||
|
||
Sometimes a Terraform configuration can result in confusing errors regarding | ||
inconsistent types. This function displays terraform's evaluation of a given | ||
value's type, which is useful in understanding this error message. | ||
|
||
This is a special function which is only available in the `terraform console` command. | ||
|
||
## Examples | ||
|
||
Here we have a conditional `output` which prints either the value of `var.list` or a local named `default_list`: | ||
|
||
```hcl | ||
variable "list" { | ||
default = [] | ||
} | ||
locals { | ||
default_list = [ | ||
{ | ||
foo = "bar" | ||
map = { bleep = "bloop" } | ||
}, | ||
{ | ||
beep = "boop" | ||
}, | ||
] | ||
} | ||
output "list" { | ||
value = var.list != [] ? var.list : local.default_list | ||
} | ||
``` | ||
|
||
Applying this configuration results in the following error: | ||
|
||
``` | ||
Error: Inconsistent conditional result types | ||
on main.tf line 18, in output "list": | ||
18: value = var.list != [] ? var.list : local.default_list | ||
|---------------- | ||
| local.default_list is tuple with 2 elements | ||
| var.list is empty tuple | ||
The true and false result expressions must have consistent types. The given | ||
expressions are tuple and tuple, respectively. | ||
``` | ||
|
||
While this error message does include some type information, it can be helpful | ||
to inspect the exact type that Terraform has determined for each given input. | ||
Examining both `var.list` and `local.default_list` using the `type` function | ||
provides more context for the error message: | ||
|
||
``` | ||
> type(var.list) | ||
tuple | ||
> type(local.default_list) | ||
tuple([ | ||
object({ | ||
foo: string, | ||
map: object({ | ||
bleep: string, | ||
}), | ||
}), | ||
object({ | ||
beep: string, | ||
}), | ||
]) | ||
``` |