1package dynblock
2
3import (
4	"github.com/hashicorp/hcl2/hcl"
5	"github.com/zclconf/go-cty/cty"
6)
7
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.
12//
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.
17type unknownBody struct {
18	template hcl.Body
19}
20
21var _ hcl.Body = unknownBody{}
22
23func (b unknownBody) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostics) {
24	content, diags := b.template.Content(schema)
25	content = b.fixupContent(content)
26
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.
30	return content, diags
31}
32
33func (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
37
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
42}
43
44func (b unknownBody) JustAttributes() (hcl.Attributes, hcl.Diagnostics) {
45	attrs, diags := b.template.JustAttributes()
46	attrs = b.fixupAttrs(attrs)
47
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.
51	return attrs, diags
52}
53
54func (b unknownBody) MissingItemRange() hcl.Range {
55	return b.template.MissingItemRange()
56}
57
58func (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)
67		}
68	}
69
70	return ret
71}
72
73func (b unknownBody) fixupAttrs(got hcl.Attributes) hcl.Attributes {
74	if len(got) == 0 {
75		return nil
76	}
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())
81		ret[name] = &new
82	}
83	return ret
84}
85