Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Optional strings should default to null not "" #768

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

Open
cassamajor opened this issue Apr 22, 2024 · 4 comments
Open

Optional strings should default to null not "" #768

cassamajor opened this issue Apr 22, 2024 · 4 comments
Labels

Comments

@cassamajor
Copy link

Describe the bug

When I ran terraform-docs tfvars hcl ., optional strings with no default value were represented as "".

I did a bit of reading on Type Constraints on Optional Object Type Attributes. Long story short is, a string type with no default value evaluates to tostring(null), not "".

How can we reproduce it?

  1. Clone this repository
  2. cd into the examples/terraform/aws directory.
  3. Run terraform-docs tfvars hcl .

Environment information

terraform-docs version v0.17.0 0.17.0 darwin/amd64
@sashasimkin
Copy link

Was raised here too, but not addressed - the author decided to close the ticket :(

#609

@sashasimkin
Copy link

Hey @khos2ow , sorry for the ping. This is still relevant for my use-case.

Could you please explain the logic behind null -> "" in the HCL tfvars?

@skyzyx
Copy link

skyzyx commented Oct 24, 2024

In Terraform/OpenTofu, nil/null simply means unknown, or more often, not-yet-known. Being "unknown" is a valid state.

In Go, the language is strongly typed, meaning that great lengths are taken to avoid nil. Terraform's interpretation of null is different from Go's interpretation of nil. The "zero type" (variable initialized with a type, but without a value) of a string is "". It is (a) definitely a string (b) with no value.

Keeping nil or null as a sustained value in low-level Terraform required an entirely-new custom type. See Unknown Values and the Dynamic Pseudo-Type for an under-the-hood explanation.

I haven't looked closely enough at the internals of this project (yet) to get a sense of how complex it might be to add support for this, but I've used the hclparse package to do some custom HCL parsing and generation, and it's not for the faint of heart. You are perfectly correct that Terraform interprets the code this way, but a separate tool (e.g., this one) would need to do a non-trivial amount of work to interpret the same value the same way.

My speculation is that, up to this point, the "juice hasn't been worth the squeeze."

Anyway, that's the explanation for how it works in Go, and my unsurprise at null being translated to "" in this project.

@sashasimkin
Copy link

sashasimkin commented Oct 27, 2024

Thanks for the very elaborate explanation! TIL :)

I went in the code and tried to unwrap if there's any special magic done for typing and stumbled upon this: https://github.com/terraform-docs/terraform-docs/blob/master/format/tfvars_hcl.go#L49-L54

If I read it correctly - this condition is specifically handling the case and changing null to "" for whatever reason 😅

I'll try to test this w/o the condition to see if it works ok.

UPD: This is it! Worked like a charm. I'll be making a PR to address that, though still not clear what was the original intent behind null -> "" in this tool.

sashasimkin added a commit to sashasimkin/terraform-docs that referenced this issue Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants