Skip to content
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

feature: String function for multi replacement with map for values #30260

Open
teddylear opened this issue Dec 23, 2021 · 9 comments
Open

feature: String function for multi replacement with map for values #30260

teddylear opened this issue Dec 23, 2021 · 9 comments
Labels
enhancement functions new new issue not yet triaged

Comments

@teddylear
Copy link

teddylear commented Dec 23, 2021

Description

Ideally with the deprecation of the template provider, there would a string function equivalent for doing a multi replace in a string with a map given for values

Use-cases

Use cases are for module deveoplers that sometimes needs to edit some user input and then inject that into other user input for complex queries. There are probably other use cases as well, but this is the first that comes to mind.

Attempted Solutions

There are some work arounds that people have done with current functions, but they become very complex very quick. One to note is this example from this stackoverflow question. This seems like overkill for something fairly common.

locals {
  input = "Hello {name}, my favorite food is {food} I'd like to eat {food} and sleep."

  map = {
    food = "Sushi"
    name = "Data_sniffer"
  }

  out = join("\n", [
    for line in split("\n", local.input) :
      format(
        replace(line, "/{(${join("|", keys(local.map))})}/", "%s"),
        [
          for value in flatten(regexall("{(${join("|", keys(local.map))})}", line)) :
            lookup(local.map, value)
        ]...
      )
  ])
}

output "test_out" {
  value = local.out
}

Proposal

Ideally there would be a function that takes a string then a map and does a replace based on what's in the map for values in {} if found. For the above example there would be something like this:

locals {
  input = "Hello {name}, my favorite food is {food} I'd like to eat {food} and sleep."

  map = {
    food = "Sushi"
    name = "Data_sniffer"
  }

  out = replaceValues(input, map)
}

output "test_out" {
  value = local.out
}

Please let me know if there are any questions/concerns regarding this. I'm open to any other implementation ideas, just want to make the end user experience easier for this common use case.

@teddylear teddylear added enhancement new new issue not yet triaged labels Dec 23, 2021
@teddylear teddylear changed the title String multi replacement with map for values feature: String function for multi replacement with map for values Dec 23, 2021
@crw
Copy link
Contributor

crw commented Jan 5, 2022

Thanks for the enhancement request! I've added it to the list of things to triage.

@teddylear
Copy link
Author

@crw If you want I can make a sample PR of what this would change would ideally look like to make things easier.

@crw
Copy link
Contributor

crw commented Feb 8, 2022

Thanks for the offer. I think the description above does a pretty good job of describing what is desired, if a sample PR is desired I'll update this thread (or more likely, a dev will join the discussion to elaborate).

@jbardin
Copy link
Member

jbardin commented Mar 4, 2022

See also #26838 for background

@teddylear
Copy link
Author

@jbardin only issue with other proposal is that applies only to templatefile for input, where this proposal would solve problems with templatefile and building complex strings without templatefile.

@cristianrat
Copy link

so no progress, then?

@crw
Copy link
Contributor

crw commented Jul 13, 2022

No, although thanks for the bump. This function would most likely need to be built as a plugin function provider, as opposed to a function built into Terraform. As such it is waiting for the implementation of plugin function providers. Please see:

@crw
Copy link
Contributor

crw commented Mar 7, 2024

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!

@Lillecarl
Copy link

This is implemented in https://registry.terraform.io/providers/random-things/string-functions/latest/docs/functions/multi_replace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement functions new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

6 participants