Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

allow negative indices for the element function #15582

Closed
rafalcieslak opened this issue Jul 18, 2017 · 5 comments · Fixed by #35501
Closed

allow negative indices for the element function #15582

rafalcieslak opened this issue Jul 18, 2017 · 5 comments · Fixed by #35501

Comments

@rafalcieslak
Copy link

Since element( list, index ) supports indices that are greater than the length of the list (by wrapping around the list using mod), it would also make sense for it to accept negative values, by wrapping around in the opposite direction.

An especially useful case would be using -1 to get the last element of a list.

@thiagolsfortunato
Copy link

+1

@apparentlymart
Copy link
Contributor

Hi @rafalcieslak! Sorry for the long silence here while we've been mostly heads-down on v0.12.0 development work.

Since the behavior you are describing is complementary with the modulo it takes for positive numbers, I agree that this is a reasonable thing to implement. We require being more explicit when using the native index syntax with square brackets, but the element function is intentionally more "helpful" so that common patterns can be expressed succinctly. (This is a situation where we compromised our usual rule that "explicit is better than implicit" for pragmatic reasons.)

The Terraform team at HashiCorp won't be able to work on this in the near future due to our focus being elsewhere, but we'd be happy to review a pull request if you or someone else has the time and motivation to implement it. Alternatively, if others would also like to see this implemented I'd encourage adding a 👍 upvote reaction to the original issue comment (not to this comment), which we use as one of the inputs to prioritize work for the Terraform team.

Please note that the function implementations in the config package are no longer actually used and will be removed in the near future as part of some post-v0.12 code cleanup. The function implementations now live in the lang/funcs package, where you can find a function called Element which is a wrapper for calling the function definition in the ElementFunc variable.

We're currently in an informal feature freeze period while we finish up the v0.12.0 release so we won't be able to merge a PR for this at least until after v0.12.0 final is out, but we can look at at afterwards.

@darren-recentive
Copy link

Hello @apparentlymart,
With informal feature freeze period for v0.12.0 being over, is there any possibility we could get some resources to help with this feature request? 🙏

@bflad bflad added the functions label Sep 1, 2023
@crw
Copy link
Contributor

crw commented Mar 6, 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!

@apparentlymart apparentlymart changed the title interpolationFuncElement doesn't support negative indices allow negative indices for the element function May 30, 2024
Copy link
Contributor

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants