Iterate over list of string using split or strings.Split #1535
-
Hello, We are trying to iterate over the list of strings to generate required AWS providers for Terraform. The most appropriate way to do it probably by using strings.Split or split functions with a But we see different results for these cases export REGIONS="eu-west-1 eu-west-2"
# strings.Split
gomplate -i '{{ strings.Split .Env.REGIONS " " }}'
[ ]
# strings.Split - pipeline
gomplate -i '{{ .Env.REGIONS | strings.Split " " }}'
[eu-west-1 eu-west-2]
# split
gomplate -i '{{ split .Env.REGIONS " " }}'
[eu-west-1 eu-west-2]
# split - pipeline
gomplate -i '{{ .Env.REGIONS | split " " }}'
[ ]
ExampleVariable export REGIONS="eu-west-1 eu-west-2" Template {{ range (split .Env.REGIONS " ") }}
provider "aws_split" {
region = "{{.}}"
alias = "{{.}}"
}
{{ end }}
{{ range (.Env.REGIONS | split " ") }}
provider "aws_split_pipe" {
region = "{{.}}"
alias = "{{.}}"
}
{{ end }}
{{ range (strings.Split .Env.REGIONS " ") }}
provider "aws_strings.Split" {
region = "{{.}}"
alias = "{{.}}"
}
{{ end }}
{{ range (.Env.REGIONS | strings.Split " ") }}
provider "aws_strings.Split_pipe" {
region = "{{.}}"
alias = "{{.}}"
}
{{ end }} Result provider "aws_split" {
region = "eu-west-1"
alias = "eu-west-1"
}
provider "aws_split" {
region = "eu-west-2"
alias = "eu-west-2"
}
provider "aws_split_pipe" {
region = " "
alias = " "
}
provider "aws_strings.Split" {
region = " "
alias = " "
}
provider "aws_strings.Split_pipe" {
region = "eu-west-1"
alias = "eu-west-1"
}
provider "aws_strings.Split_pipe" {
region = "eu-west-2"
alias = "eu-west-2"
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @air3ijai, thanks for taking the time to post this.
No, it works both ways. The pipeline syntax is just syntactic sugar for providing the left-hand side of the I see in your example you've flipped the order of the two arguments, however:
What you actually want is As for the difference between I don't know why I'm going to improve the documentation to make this difference more clear, and also deprecate the |
Beta Was this translation helpful? Give feedback.
-
Hi @hairyhenderson, thank you for the clarification and documentation update! Yes, looks like I missed the order and it works fine for now: export REGIONS="eu-west-1 eu-west-2"
# strings.Split
gomplate -i '{{ strings.Split " " .Env.REGIONS }}'
[eu-west-1 eu-west-2]
# strings.Split - pipeline
gomplate -i '{{ .Env.REGIONS | strings.Split " " }}'
[eu-west-1 eu-west-2] |
Beta Was this translation helpful? Give feedback.
Hi @air3ijai, thanks for taking the time to post this.
No, it works both ways. The pipeline syntax is just syntactic sugar for providing the left-hand side of the
|
as the right-most argument.I see in your example you've flipped the order of the two arguments, however:
What you actually want is
{{ strings.Split " " .Env.REGIONS }}
.As for the difference between
strings.Split
andsplit
, this is unfortunately by design. If you look at the documentation for each, you'll seesplit
is not an alias forstrings.Split
, andsplit
does redirect you to u…