@@ -27,16 +27,19 @@ func (b *Block) CoerceValue(in cty.Value) (cty.Value, error) {
2727}
2828
2929func (b * Block ) coerceValue (in cty.Value , path cty.Path ) (cty.Value , error ) {
30+ convType := b .specType ()
31+ impliedType := convType .WithoutOptionalAttributesDeep ()
32+
3033 switch {
3134 case in .IsNull ():
32- return cty .NullVal (b . ImpliedType () ), nil
35+ return cty .NullVal (impliedType ), nil
3336 case ! in .IsKnown ():
34- return cty .UnknownVal (b . ImpliedType () ), nil
37+ return cty .UnknownVal (impliedType ), nil
3538 }
3639
3740 ty := in .Type ()
3841 if ! ty .IsObjectType () {
39- return cty .UnknownVal (b . ImpliedType () ), path .NewErrorf ("an object is required" )
42+ return cty .UnknownVal (impliedType ), path .NewErrorf ("an object is required" )
4043 }
4144
4245 for name := range ty .AttributeTypes () {
@@ -46,29 +49,32 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
4649 if _ , defined := b .BlockTypes [name ]; defined {
4750 continue
4851 }
49- return cty .UnknownVal (b . ImpliedType () ), path .NewErrorf ("unexpected attribute %q" , name )
52+ return cty .UnknownVal (impliedType ), path .NewErrorf ("unexpected attribute %q" , name )
5053 }
5154
5255 attrs := make (map [string ]cty.Value )
5356
5457 for name , attrS := range b .Attributes {
58+ attrType := impliedType .AttributeType (name )
59+ attrConvType := convType .AttributeType (name )
60+
5561 var val cty.Value
5662 switch {
5763 case ty .HasAttribute (name ):
5864 val = in .GetAttr (name )
5965 case attrS .Computed || attrS .Optional :
60- val = cty .NullVal (attrS . Type )
66+ val = cty .NullVal (attrType )
6167 default :
62- return cty .UnknownVal (b . ImpliedType () ), path .NewErrorf ("attribute %q is required" , name )
68+ return cty .UnknownVal (impliedType ), path .NewErrorf ("attribute %q is required" , name )
6369 }
6470
65- val , err := attrS . coerceValue (val , append ( path , cty. GetAttrStep { Name : name }) )
71+ val , err := convert . Convert (val , attrConvType )
6672 if err != nil {
67- return cty .UnknownVal (b . ImpliedType ()), err
73+ return cty .UnknownVal (impliedType ), append ( path , cty. GetAttrStep { Name : name }). NewError ( err )
6874 }
69-
7075 attrs [name ] = val
7176 }
77+
7278 for typeName , blockS := range b .BlockTypes {
7379 switch blockS .Nesting {
7480
@@ -79,7 +85,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
7985 val := in .GetAttr (typeName )
8086 attrs [typeName ], err = blockS .coerceValue (val , append (path , cty.GetAttrStep {Name : typeName }))
8187 if err != nil {
82- return cty .UnknownVal (b . ImpliedType () ), err
88+ return cty .UnknownVal (impliedType ), err
8389 }
8490 default :
8591 attrs [typeName ] = blockS .EmptyValue ()
@@ -100,7 +106,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
100106 }
101107
102108 if ! coll .CanIterateElements () {
103- return cty .UnknownVal (b . ImpliedType () ), path .NewErrorf ("must be a list" )
109+ return cty .UnknownVal (impliedType ), path .NewErrorf ("must be a list" )
104110 }
105111 l := coll .LengthInt ()
106112
@@ -116,7 +122,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
116122 idx , val := it .Element ()
117123 val , err = blockS .coerceValue (val , append (path , cty.IndexStep {Key : idx }))
118124 if err != nil {
119- return cty .UnknownVal (b . ImpliedType () ), err
125+ return cty .UnknownVal (impliedType ), err
120126 }
121127 elems = append (elems , val )
122128 }
@@ -141,7 +147,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
141147 }
142148
143149 if ! coll .CanIterateElements () {
144- return cty .UnknownVal (b . ImpliedType () ), path .NewErrorf ("must be a set" )
150+ return cty .UnknownVal (impliedType ), path .NewErrorf ("must be a set" )
145151 }
146152 l := coll .LengthInt ()
147153
@@ -157,7 +163,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
157163 idx , val := it .Element ()
158164 val , err = blockS .coerceValue (val , append (path , cty.IndexStep {Key : idx }))
159165 if err != nil {
160- return cty .UnknownVal (b . ImpliedType () ), err
166+ return cty .UnknownVal (impliedType ), err
161167 }
162168 elems = append (elems , val )
163169 }
@@ -182,7 +188,7 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
182188 }
183189
184190 if ! coll .CanIterateElements () {
185- return cty .UnknownVal (b . ImpliedType () ), path .NewErrorf ("must be a map" )
191+ return cty .UnknownVal (impliedType ), path .NewErrorf ("must be a map" )
186192 }
187193 l := coll .LengthInt ()
188194 if l == 0 {
@@ -196,11 +202,11 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
196202 var err error
197203 key , val := it .Element ()
198204 if key .Type () != cty .String || key .IsNull () || ! key .IsKnown () {
199- return cty .UnknownVal (b . ImpliedType () ), path .NewErrorf ("must be a map" )
205+ return cty .UnknownVal (impliedType ), path .NewErrorf ("must be a map" )
200206 }
201207 val , err = blockS .coerceValue (val , append (path , cty.IndexStep {Key : key }))
202208 if err != nil {
203- return cty .UnknownVal (b . ImpliedType () ), err
209+ return cty .UnknownVal (impliedType ), err
204210 }
205211 elems [key .AsString ()] = val
206212 }
@@ -240,11 +246,3 @@ func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
240246
241247 return cty .ObjectVal (attrs ), nil
242248}
243-
244- func (a * Attribute ) coerceValue (in cty.Value , path cty.Path ) (cty.Value , error ) {
245- val , err := convert .Convert (in , a .Type )
246- if err != nil {
247- return cty .UnknownVal (a .Type ), path .NewError (err )
248- }
249- return val , nil
250- }
0 commit comments