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: add (console-only) TypeFunction #28501

Merged
merged 2 commits into from Apr 23, 2021
Merged

Conversation

mildwonkey
Copy link
Contributor

The type() function, which is only available for terraform console,
prints out a string representation of the type of a given value. This is mainly intended for
debugging - it's handy to be able to directly inspect terraform's understanding
of a complex variable.

This introduces a new field for lang.Scope: ConsoleMode. When ConsoleMode is true, any additional functions intended for use in the console (only) may be added.

(with bonus go mody tidy cleanup)

The type() function, which is only available for terraform console,
prints out the type of a given value. This is mainly intended for
debugging - it's nice to be able to print out terraform's understanding
of a complex variable.

This introduces a new field for Scope: ConsoleMode. When ConsoleMode is true, any additional functions intended for use in the console (only) may be added.
@mildwonkey mildwonkey requested a review from a team April 23, 2021 13:22
@codecov
Copy link

codecov bot commented Apr 23, 2021

Codecov Report

Merging #28501 (2f3f353) into main (15b6a16) will increase coverage by 0.02%.
The diff coverage is 74.66%.

Impacted Files Coverage Δ
lang/functions.go 94.96% <0.00%> (-1.39%) ⬇️
lang/scope.go 100.00% <ø> (ø)
repl/format.go 96.33% <25.00%> (-2.73%) ⬇️
lang/funcs/conversion.go 83.63% <79.41%> (-6.84%) ⬇️
command/console.go 32.69% <100.00%> (+0.65%) ⬆️
internal/providercache/dir.go 67.34% <0.00%> (-6.13%) ⬇️

Copy link
Member

@alisdair alisdair left a comment

Choose a reason for hiding this comment

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

This is great! Thanks for coming back to it.

I have a couple of minor suggestions inline about empty object/tuple cases, but otherwise ✅

case ty.IsObjectType():
atys := ty.AttributeTypes()
if len(atys) == 0 {
b.WriteString("object{}")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
b.WriteString("object{}")
b.WriteString("object({})")

Lines up with the configuration for an empty object type constructor:

variable "foo" {
  type = object({})
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great catch, thank you, those are both much better. I made those changes and added more test cases to go with 'em.

case ty.IsTupleType():
etys := ty.TupleElementTypes()
if len(etys) == 0 {
b.WriteString("tuple")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
b.WriteString("tuple")
b.WriteString("tuple([])")

Same method as above, the type constructor syntax for this is:

variable "foo" {
  type = tuple([])
}

@mildwonkey mildwonkey merged commit f6af7b4 into main Apr 23, 2021
@mildwonkey mildwonkey deleted the mildwonkey/type-func branch April 23, 2021 14:29
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, 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 May 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants