4 "github.com/hashicorp/hcl2/hcl"
5 "github.com/zclconf/go-cty/cty"
8 // unknownBody is a funny body that just reports everything inside it as
9 // unknown. It uses a given other body as a sort of template for what attributes
10 // and blocks are inside -- including source location information -- but
11 // subsitutes unknown values of unknown type for all attributes.
13 // This rather odd process is used to handle expansion of dynamic blocks whose
14 // for_each expression is unknown. Since a block cannot itself be unknown,
15 // we instead arrange for everything _inside_ the block to be unknown instead,
16 // to give the best possible approximation.
17 type unknownBody struct {
21 var _ hcl.Body = unknownBody{}
23 func (b unknownBody) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostics) {
24 content, diags := b.template.Content(schema)
25 content = b.fixupContent(content)
27 // We're intentionally preserving the diagnostics reported from the
28 // inner body so that we can still report where the template body doesn't
29 // match the requested schema.
33 func (b unknownBody) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) {
34 content, remain, diags := b.template.PartialContent(schema)
35 content = b.fixupContent(content)
36 remain = unknownBody{remain} // remaining content must also be wrapped
38 // We're intentionally preserving the diagnostics reported from the
39 // inner body so that we can still report where the template body doesn't
40 // match the requested schema.
41 return content, remain, diags
44 func (b unknownBody) JustAttributes() (hcl.Attributes, hcl.Diagnostics) {
45 attrs, diags := b.template.JustAttributes()
46 attrs = b.fixupAttrs(attrs)
48 // We're intentionally preserving the diagnostics reported from the
49 // inner body so that we can still report where the template body doesn't
50 // match the requested schema.
54 func (b unknownBody) MissingItemRange() hcl.Range {
55 return b.template.MissingItemRange()
58 func (b unknownBody) fixupContent(got *hcl.BodyContent) *hcl.BodyContent {
59 ret := &hcl.BodyContent{}
60 ret.Attributes = b.fixupAttrs(got.Attributes)
61 if len(got.Blocks) > 0 {
62 ret.Blocks = make(hcl.Blocks, 0, len(got.Blocks))
63 for _, gotBlock := range got.Blocks {
64 new := *gotBlock // shallow copy
65 new.Body = unknownBody{gotBlock.Body} // nested content must also be marked unknown
66 ret.Blocks = append(ret.Blocks, &new)
73 func (b unknownBody) fixupAttrs(got hcl.Attributes) hcl.Attributes {
77 ret := make(hcl.Attributes, len(got))
78 for name, gotAttr := range got {
79 new := *gotAttr // shallow copy
80 new.Expr = hcl.StaticExpr(cty.DynamicVal, gotAttr.Expr.Range())