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

add hexadecimal support for tonumber() #22585

Closed
wants to merge 1 commit into from

Conversation

joe-bowman
Copy link

fixes #22584

add ability for tonumber() to support 0x prefixed hexadecimal strings

e.g.

variable "v1" {
    default = "0x99"
}
output "input-v1" {
    value = tonumber(var.v1)
}

will return:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

input-v1 = 153

@hashicorp-cla
Copy link

hashicorp-cla commented Aug 24, 2019

CLA assistant check
All committers have signed the CLA.

@apparentlymart
Copy link
Contributor

Hi @joe-bowman! Thanks for working on this.

As you've seen in the implementation, tonumber (and the other similar to... functions) are intended as ways to hint to Terraform to perform one of its normal type conversion behaviors. I think it'd be confusing to add this exception only in the case of converting numbers, since numbers are intentionally required to be decimal in normal conversions to reduce the risk inadvertent implicit misbehavior.

What do you think about adding a new function called parseint instead, which would be explicitly about integer parsing, rather than type conversion, and could thus have a more explicit interface for handling numbers of different bases.

For example, it could have the signature parseint(string, base) so that to parse hexadecimal you'd call parseint("ff", 16) and get back 255.

We tend to prefer to avoid "implicit" behaviors like saying that a 0x prefix implies base 16, so my preference would be to instead just make the base argument be required and thus be explicit about which base a particular situation is expecting. However, if you have a specific use-case for accepting strings containing numbers in arbitrary bases defined by prefixes, rather than a configuration being explicit about which base it's expecting, I'd be happy to talk about it further and see if the use-case is common enough to override our default design principle.

@jeet-parekh
Copy link
Contributor

@joe-bowman, I would like to give it a shot if you aren't working on this.

@joe-bowman
Copy link
Author

joe-bowman commented Sep 4, 2019 via email

@jeet-parekh
Copy link
Contributor

Cool, thanks. Will let you know if there's an update.

@danieldreier
Copy link
Contributor

I'm going to close this because the parseint function PR was merged and so obviates this. Thanks @joe-bowman for suggesting this!

@ghost
Copy link

ghost commented Mar 29, 2020

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.

@ghost ghost locked and limited conversation to collaborators Mar 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tonumber() does not support 0x prefixed hexadecimal strings
6 participants