Skip to content

Commit

Permalink
website: "for" expression examples to use canonical formatting
Browse files Browse the repository at this point in the history
The "terraform fmt" command produces a different canonical form than we
were showing in our examples here. Our examples should always reflect the
conventions applied by "terraform fmt" to avoid confusion.

(This particular decision is a pragmatic one because the formatter design
needs to use the same rules for the colon in the ? : conditional operator
as for the colon in "for" expressions.)
  • Loading branch information
apparentlymart committed Mar 11, 2019
1 parent 0a9959c commit 36bb9b4
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions website/docs/configuration/expressions.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ For example, if `var.list` is a list of strings, then the following expression
produces a list of strings with all-uppercase letters:

```hcl
[for s in var.list: upper(s)]
[for s in var.list : upper(s)]
```

This `for` expression iterates over each element of `var.list`, and then
Expand All @@ -511,7 +511,7 @@ it produces. The above example uses `[` and `]`, which produces a tuple. If
expressions must be provided separated by the `=>` symbol:

```hcl
{for s in var.list: s => upper(s)}
{for s in var.list : s => upper(s)}
```

This expression produces an object whose attributes are the original elements
Expand All @@ -522,23 +522,23 @@ from the source collection, which can produce a value with fewer elements than
the source:

```
[for s in var.list: upper(s) if s != ""]
[for s in var.list : upper(s) if s != ""]
```

The source value can also be an object or map value, in which case two
temporary variable names can be provided to access the keys and values
respectively:

```
[for k, v in var.map: length(k) + length(v)]
[for k, v in var.map : length(k) + length(v)]
```

Finally, if the result type is an object (using `{` and `}` delimiters) then
the value result expression can be followed by the `...` symbol to group
together results that have a common key:

```
{for s in var.list: substr(s, 0, 1) => s... if s != ""}
{for s in var.list : substr(s, 0, 1) => s... if s != ""}
```

## Splat Expressions
Expand All @@ -550,7 +550,7 @@ If `var.list` is a list of objects that all have an attribute `id`, then
a list of the ids could be produced with the following `for` expression:

```hcl
[for o in var.list: o.id]
[for o in var.list : o.id]
```

This is equivalent to the following _splat expression:_
Expand All @@ -572,7 +572,7 @@ var.list[*].interfaces[0].name
The above expression is equivalent to the following `for` expression:

```hcl
[for o in var.list: o.interfaces[0].name]
[for o in var.list : o.interfaces[0].name]
```

Splat expressions also have another useful effect: if they are applied to
Expand Down Expand Up @@ -609,7 +609,7 @@ This form has a subtly different behavior, equivalent to the following
`for` expression:

```
[for o in var.list: o.interfaces][0].name
[for o in var.list : o.interfaces][0].name
```

Notice that with the attribute-only splat expression the index operation
Expand Down

0 comments on commit 36bb9b4

Please sign in to comment.