-
Notifications
You must be signed in to change notification settings - Fork 591
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
gohcl: Unable to decode block to *hcl.Block field. #505
Comments
Hi @jmalloc! Thanks for reporting this. Your theory as to why this didn't work makes sense to me. Honestly, this decoding into Other software I've built or seen built with type Example struct {
Foo string `hcl:"foo,label"`
Bar string `hcl:"bar,label"`
Config hcl.Body `hcl:",remain"`
} The above then tells I think the above raises a reason why decoding into Off the top of my head then I don't think decoding directly into |
Thanks for the detailed response! I don't quite understand the difference in feasibility between decoding into an To offer some insight into my use case: I have a block with a known schema that contains a "sub" block with a schema defined by an as-yet-un-loaded plugin. I was hoping to defer validation of that inner block (including its labels) until a later phase when the plugin can be loaded. Perhaps I'm just running up against the limitations of I will happily come up with another solution if it turns out that decoding into Thanks again! |
Hi @jmalloc, Thanks for that extra information. Given the use-case you described, I think an approach like I described in my previous comment could work for you today, as long as the number of labels on the nested block will be known before loading the plugin. This would be a similar design as in various other HCL-based languages, like Terraform and Packer's languages, where the block labels specify (directly or indirectly) which plugin to use. This would also be possible using the lower-level API, and that's how Terraform in particular does it. But I don't think the low-level API should be needed in this case, because of |
Thanks again, you have been very helpful! Based on this I've reengineered so that the plugin is only responsible for the schema of the body. Would you a PR for correction of the docs (and removal of that unreachable code path, assuming it is indeed unreachable)? |
Hi @jmalloc! Thanks for following up about what worked. If you would be willing, I'd be happy to review and merge a change which changes "may be of type *hcl.Block or hcl.Body" to just say "may be of type hcl.Body", and adjust the grammatical number of the rest of the sentence to refer to "this type" rather than "these types". If you can also see some codepaths that are easy to delete without significant restructuring then that'd be great too! I've not looked too closely yet but I'd agree that it seems like removing the |
It seems that blocks can not actually be decoded to I noticed there were no tests for this, so I added one (to ensure my changes didn't break this behaviour) and it fails quite early in Upon reflection it makes sense that this is not allowed; doing so would discard label information entirely. Unless you feel I've overlooked something I'll remove both parts from the documentation along with the the obviously unused paths from |
I think I have discovered a bug in
gohcl
(I am using v2.11.1). Could you please confirm if this is a legitimate issue, or if I am misusing the package.The
gohcl
package docs state:However, attempting to decode a block into a field of type
*hcl.Block
produces an error (unless the block is empty).Analysis
As best I can tell the issue is with the interplay between these two lines, though there are likely other culprits:
decodeBlockToValue()
performs the check to see if the field can be assigned a*hcl.Block
valuedecodeBlockToValue()
for fields that are pointers. Specifically, it dereferences the pointer usingv.Elem()
, hence passing a value of typehcl.Block
not*hcl.Block
, causing the conditional on line 266 to fail.The net result is that the field is treated like any other user-defined struct that has no
hcl
field tags, which is why this error only occurs if the block in the.hcl
file is not empty.Reproducing the issue
The following code can be used to reproduce the issue:
It produces the following output:
This archive contains the
main.go
file above andgo.mod
/go.sum
files to help reproduce the issue.Thanks in advance for your help, and for making this fantastic system open source 👍
The text was updated successfully, but these errors were encountered: