@@ -16,6 +16,7 @@ type expandBody struct {
1616 original hcl.Body
1717 forEachCtx * hcl.EvalContext
1818 iteration * iteration // non-nil if we're nested inside another "dynamic" block
19+ valueMarks cty.ValueMarks
1920
2021 checkForEach []func (cty.Value , hcl.Expression , * hcl.EvalContext ) hcl.Diagnostics
2122
@@ -125,7 +126,7 @@ func (b *expandBody) extendSchema(schema *hcl.BodySchema) *hcl.BodySchema {
125126}
126127
127128func (b * expandBody ) prepareAttributes (rawAttrs hcl.Attributes ) hcl.Attributes {
128- if len (b .hiddenAttrs ) == 0 && b .iteration == nil {
129+ if len (b .hiddenAttrs ) == 0 && b .iteration == nil && len ( b . valueMarks ) == 0 {
129130 // Easy path: just pass through the attrs from the original body verbatim
130131 return rawAttrs
131132 }
@@ -142,13 +143,24 @@ func (b *expandBody) prepareAttributes(rawAttrs hcl.Attributes) hcl.Attributes {
142143 if b .iteration != nil {
143144 attr := * rawAttr // shallow copy so we can mutate it
144145 attr .Expr = exprWrap {
145- Expression : attr .Expr ,
146- i : b .iteration ,
146+ Expression : attr .Expr ,
147+ i : b .iteration ,
148+ resultMarks : b .valueMarks ,
147149 }
148150 attrs [name ] = & attr
149151 } else {
150- // If we have no active iteration then no wrapping is required.
151- attrs [name ] = rawAttr
152+ // If we have no active iteration then no wrapping is required
153+ // unless we have marks to apply.
154+ if len (b .valueMarks ) != 0 {
155+ attr := * rawAttr // shallow copy so we can mutate it
156+ attr .Expr = exprWrap {
157+ Expression : attr .Expr ,
158+ resultMarks : b .valueMarks ,
159+ }
160+ attrs [name ] = & attr
161+ } else {
162+ attrs [name ] = rawAttr
163+ }
152164 }
153165 }
154166 return attrs
@@ -192,8 +204,9 @@ func (b *expandBody) expandBlocks(schema *hcl.BodySchema, rawBlocks hcl.Blocks,
192204 continue
193205 }
194206
195- if spec .forEachVal .IsKnown () {
196- for it := spec .forEachVal .ElementIterator (); it .Next (); {
207+ forEachVal , marks := spec .forEachVal .Unmark ()
208+ if forEachVal .IsKnown () {
209+ for it := forEachVal .ElementIterator (); it .Next (); {
197210 key , value := it .Element ()
198211 i := b .iteration .MakeChild (spec .iteratorName , key , value )
199212
@@ -202,7 +215,7 @@ func (b *expandBody) expandBlocks(schema *hcl.BodySchema, rawBlocks hcl.Blocks,
202215 if block != nil {
203216 // Attach our new iteration context so that attributes
204217 // and other nested blocks can refer to our iterator.
205- block .Body = b .expandChild (block .Body , i )
218+ block .Body = b .expandChild (block .Body , i , marks )
206219 blocks = append (blocks , block )
207220 }
208221 }
@@ -214,7 +227,10 @@ func (b *expandBody) expandBlocks(schema *hcl.BodySchema, rawBlocks hcl.Blocks,
214227 block , blockDiags := spec .newBlock (i , b .forEachCtx )
215228 diags = append (diags , blockDiags ... )
216229 if block != nil {
217- block .Body = unknownBody {b .expandChild (block .Body , i )}
230+ block .Body = unknownBody {
231+ template : b .expandChild (block .Body , i , marks ),
232+ valueMarks : marks ,
233+ }
218234 blocks = append (blocks , block )
219235 }
220236 }
@@ -226,7 +242,7 @@ func (b *expandBody) expandBlocks(schema *hcl.BodySchema, rawBlocks hcl.Blocks,
226242 // case it contains expressions that refer to our inherited
227243 // iterators, or nested "dynamic" blocks.
228244 expandedBlock := * rawBlock // shallow copy
229- expandedBlock .Body = b .expandChild (rawBlock .Body , b .iteration )
245+ expandedBlock .Body = b .expandChild (rawBlock .Body , b .iteration , nil )
230246 blocks = append (blocks , & expandedBlock )
231247 }
232248 }
@@ -235,11 +251,12 @@ func (b *expandBody) expandBlocks(schema *hcl.BodySchema, rawBlocks hcl.Blocks,
235251 return blocks , diags
236252}
237253
238- func (b * expandBody ) expandChild (child hcl.Body , i * iteration ) hcl.Body {
254+ func (b * expandBody ) expandChild (child hcl.Body , i * iteration , valueMarks cty. ValueMarks ) hcl.Body {
239255 chiCtx := i .EvalContext (b .forEachCtx )
240256 ret := Expand (child , chiCtx )
241257 ret .(* expandBody ).iteration = i
242258 ret .(* expandBody ).checkForEach = b .checkForEach
259+ ret .(* expandBody ).valueMarks = valueMarks
243260 return ret
244261}
245262
0 commit comments