hashicorp / hcl2

Former temporary home for experimental new version of HCL
https://github.com/hashicorp/hcl
Mozilla Public License 2.0
375 stars 64 forks source link

ext/dynblock: Stub out contents when for_each is unknown #101

Closed apparentlymart closed 5 years ago

apparentlymart commented 5 years ago

Previously our behavior for an unknown for_each was to produce a single block whose content was the result of evaluating content with the iterator set to cty.DynamicVal. That produced a reasonable idea of the content, but the number of blocks in the result was still not accurate, and that can present a problem for applications that use unknown values to predict the overall shape of a not-yet-complete structure.

We can't return an unknown block via the HCL API, but to make that situation easier to recognize by callers we'll now go a little further and force all of the leaf attributes in such a block to be unknown values, even if they are constants in the configuration. This allows a calling application that is making predictions to use a single object whose leaves are all unknown as a heuristic to recognize what is effectively an unknown set of blocks.

This is still not a perfect heuristic, but is the best we can do here within the HCL API assumptions. A fundamental assumption of the HCL API is that it's possible to walk the block structure without evaluating any expressions and the dynamic block extension is intentionally subverting that assumption, so some oddities are to be expected. Calling applications that need a fully reliable sense of the final structure should not use the dynamic block extension.

codecov-io commented 5 years ago

Codecov Report

Merging #101 into master will increase coverage by 0.13%. The diff coverage is 82.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #101      +/-   ##
==========================================
+ Coverage   68.71%   68.85%   +0.13%     
==========================================
  Files          97       98       +1     
  Lines       10188    10223      +35     
==========================================
+ Hits         7001     7039      +38     
+ Misses       2861     2858       -3     
  Partials      326      326
Impacted Files Coverage Δ
ext/dynblock/expand_body.go 76.74% <100%> (+5.65%) :arrow_up:
ext/dynblock/unknown_body.go 82.35% <82.35%> (ø)
hclwrite/native_node_sorter.go 100% <0%> (+25%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update ffaa892...f5ff937. Read the comment docs.