-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Conversation
There was a problem hiding this 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!
@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? |
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:
As I was suggesting in my other comments here, we can return this new error using 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 |
Hi @apparentlymart, I changed What do you think about another function |
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 If such a use-case does emerge, I expect we'd prefer to address it via enhancements to |
93bed4d
to
b3154e8
Compare
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, did you make any changes to my code to make the tests pass? What were the changes? Thanks! |
Hi @jeet-parekh, The test failure was from the |
Hi @apparentlymart, When is this change planned to be released? |
lang/funcs: parseint function (cherry-pick hashicorp/terraform#22747)
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. |
Added a function to parse a string to an integer for a specified base.
Closes #22584. Also see #22585.