hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.66k stars 9.41k forks source link

Simple string Concatenation #24614

Open ncrothe opened 4 years ago

ncrothe commented 4 years ago

I would like a simple string concatenation function (or better: operand) for Terraform syntax. Ideally, something like "string1" + "string2"

Current Terraform Version

0.12.24

Use-cases

We have so many parameters that look something like "prefix-${var.name}". The string templating language serves it's purpose, but readability would be improved with an additional simpler way of concatenating strings. E.g. "prefix-"+var.name.

Attempted Solutions

I am aware that the resulting string can be achieved with various means like string template, join with empty delimiter (or "-" in the example above) as well as format("%s%s","prefix-",var.name).

Proposal

Preferred: Extend the + operator to allow string concatenation. Alternatively: Add a function like strconcat(strings...) that concatenates all given strings.

crw commented 4 months ago

Thank you for your continued interest in this issue.

Terraform version 1.8 launches with support of provider-defined functions. It is now possible to implement your own functions! We would love to see this implemented as a provider-defined function.

Please see the provider-defined functions documentation to learn how to implement functions in your providers. If you are new to provider development, learn how to create a new provider with the Terraform Plugin Framework. If you have any questions, please visit the Terraform Plugin Development category in our official forum.

We hope this feature unblocks future function development and provides more flexibility for the Terraform community. Thank you for your continued support of Terraform!

ncrothe commented 4 months ago

Hi Craig, while I appreciate having the option, this particular feature makes zero sense as a provider-specific implementation and should IMO be tackled at the core Terraform level.

crw commented 4 months ago

Yes, apologies you are correct! I will delete that comment and continue to track this.

apparentlymart commented 1 month ago

The + operator cannot be overloaded to support string concatenation because it's already defined as trying to convert string arguments to numbers and then adding those numbers together if successful, such as in "1" + "2".

The function formulation of this seems only marginally more concise than the join function with an empty string as the delimiter, as already mentioned. Therefore it doesn't seem justified to add a more specialized variant that would differ only in forcing the delimiter to be the empty string. :thinking:

I understand that this request came from a subjective preference favoring concatenation syntax over the other options, but since Terraform already offers at least three different ways to concatenate strings together, and most authors seem happy to use the template interpolation form, I don't feel like this addition would be impactful enough to justify the cost of implementing and maintaining another function or operator. :confounded: