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

lang/funcs: added parseint function #22747

Merged
merged 1 commit into from
Sep 17, 2019

Conversation

jeet-parekh
Copy link
Contributor

Added a function to parse a string to an integer for a specified base.

Closes #22584. Also see #22585.

@ghost ghost added the sdkv1 [PRs only] Marks changes that may potentially need to be ported to the plugi nSDK label Sep 10, 2019
@hashicorp-cla
Copy link

hashicorp-cla commented Sep 10, 2019

CLA assistant check
All committers have signed the CLA.

@jeet-parekh jeet-parekh changed the title lang.funcs: added parseint function lang/funcs: added parseint function Sep 10, 2019
Copy link
Contributor

@apparentlymart apparentlymart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @jeet-parekh!

Overall this looks great. I just left a few small bits of feedback inline. If you have any questions about them then please let me know!

lang/funcs/number.go Outdated Show resolved Hide resolved
lang/funcs/number.go Outdated Show resolved Hide resolved
@jeet-parekh
Copy link
Contributor Author

@apparentlymart, I have set the type of the first argument as string. But when I test these changes in the terraform console, it accepts both a number, and a string as the first argument. That's confusing me a bit. Why is that happening?

@apparentlymart
Copy link
Contributor

Hi @jeet-parekh!

The Terraform language automatically converts numbers to strings using base 10 when the context requires a string.

In this particular case I agree it seems a bit strange to allow it, since it probably indicates a user misunderstanding what this function is for or how it works: this function is operating on the individual characters of a string, so populating it with an automatic conversion would be a strange thing to do. It's possible to override that default conversion behavior like this:

  • Change the parameter type of the first parameter to cty.DynamicPseudoType, which will tell the language runtime to just accept any type and not do any automatic conversions.
  • Change the Type field in the function definition to be an inline function (rather than a call to StaticReturnType) and write logic in that function to check that args[0].Type() is cty.String, returning an explicit error if not. The Type function should still ultimately return cty.Number, nil if that check passes, to indicate that the result type will always be a number.

As I was suggesting in my other comments here, we can return this new error using function.NewArgErrorf to allow Terraform to indicate specifically that it's the first argument that is incorrect, doing something like this:

    if !args[0].Type().Equals(cty.String) {
         return cty.Number, function.NewArgErrorf(0, "first argument must be string, not %s", args[0].Type().FriendlyName())
    }

I suggest we retain the automatic type conversion behavior for the second argument (by leaving it specified as cty.Number) because that argument is just a normal parameter interpreted atomically for its value (rather than broken apart and analyzed, as the first argument) and we can therefore remain consistent with the normal language behaviors for that one.

@jeet-parekh
Copy link
Contributor Author

Hi @apparentlymart,

I changed parseint to use math/big.

What do you think about another function inttostr? It would convert a number to a string for a given base.

@apparentlymart
Copy link
Contributor

Hi @jeet-parekh! I'm going to look at these changes now, but I first wanted to address your question:

For the moment I don't think we should add a function for formatting numbers because formatting numbers to bases 2, 8, 10, and 16 is already supported via the format function and I don't think we have a compelling use-case for formatting to any other bases.

If such a use-case does emerge, I expect we'd prefer to address it via enhancements to format rather than via a new function, since that function is the primary way to produce non-default string representations of non-string values and includes related features like zero-padding and rounding.

@apparentlymart
Copy link
Contributor

Thanks @jeet-parekh!

I made some small changes to fix one failing test, to expand slightly the documentation, and to add some additional tests I used to try out some different inputs. I'm going to merge this now. Thanks again for working on this, and thanks for working through this feedback with me. 🎉

@apparentlymart apparentlymart merged commit bcc69c0 into hashicorp:master Sep 17, 2019
@jeet-parekh
Copy link
Contributor Author

@apparentlymart, did you make any changes to my code to make the tests pass? What were the changes? Thanks!

@apparentlymart
Copy link
Contributor

Hi @jeet-parekh,

The test failure was from the TestFunctions test using a number instead of a string.

@aajain-crest
Copy link

Hi @apparentlymart,

When is this change planned to be released?

kmoe added a commit to hashicorp/terraform-plugin-sdk that referenced this pull request Sep 27, 2019
@ghost
Copy link

ghost commented Oct 18, 2019

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 Oct 18, 2019
@appilon appilon removed the sdkv1 [PRs only] Marks changes that may potentially need to be ported to the plugi nSDK label Oct 23, 2019
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