Skip to content

Commit 24b88b7

Browse files
Liam Cervantealisdair
andauthored
Reverse the order of conversion/defaults, and update HCL with more flexible defaults package (#32454)
* Add failing test case for the given issue * pause * don't use local when sending PR for review * go get github.com/hashicorp/hcl/v2@v2.16.0 * Update go.mod --------- Co-authored-by: Alisdair McDiarmid <alisdair@users.noreply.github.com>
1 parent fc8fed0 commit 24b88b7

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ require (
4343
github.com/hashicorp/go-uuid v1.0.3
4444
github.com/hashicorp/go-version v1.6.0
4545
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
46-
github.com/hashicorp/hcl/v2 v2.15.0
46+
github.com/hashicorp/hcl/v2 v2.16.0
4747
github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2
4848
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
4949
github.com/hashicorp/terraform-svchost v0.0.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
395395
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws=
396396
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
397397
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
398-
github.com/hashicorp/hcl/v2 v2.15.0 h1:CPDXO6+uORPjKflkWCCwoWc9uRp+zSIPcCQ+BrxV7m8=
399-
github.com/hashicorp/hcl/v2 v2.15.0/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng=
398+
github.com/hashicorp/hcl/v2 v2.16.0 h1:MPq1q615H+9wBAdE3EbwEd6imSohElrIguuasbQruB0=
399+
github.com/hashicorp/hcl/v2 v2.16.0/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng=
400400
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d h1:9ARUJJ1VVynB176G1HCwleORqCaXm/Vx0uUi0dL26I0=
401401
github.com/hashicorp/jsonapi v0.0.0-20210826224640-ee7dae0fb22d/go.mod h1:Yog5+CPEM3c99L1CL2CFCYoSzgWm5vTU58idbRUaLik=
402402
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=

internal/terraform/eval_variable.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ func prepareFinalInputVariableValue(addr addrs.AbsInputVariableInstance, raw *In
9090
given = defaultVal // must be set, because we checked above that the variable isn't required
9191
}
9292

93+
// Apply defaults from the variable's type constraint to the converted value,
94+
// unless the converted value is null. We do not apply defaults to top-level
95+
// null values, as doing so could prevent assigning null to a nullable
96+
// variable.
97+
if cfg.TypeDefaults != nil && !given.IsNull() {
98+
given = cfg.TypeDefaults.Apply(given)
99+
}
100+
93101
val, err := convert.Convert(given, convertTy)
94102
if err != nil {
95103
log.Printf("[ERROR] prepareFinalInputVariableValue: %s has unsuitable type\n got: %s\n want: %s", addr, given.Type(), convertTy)
@@ -132,14 +140,6 @@ func prepareFinalInputVariableValue(addr addrs.AbsInputVariableInstance, raw *In
132140
return cty.UnknownVal(cfg.Type), diags
133141
}
134142

135-
// Apply defaults from the variable's type constraint to the converted value,
136-
// unless the converted value is null. We do not apply defaults to top-level
137-
// null values, as doing so could prevent assigning null to a nullable
138-
// variable.
139-
if cfg.TypeDefaults != nil && !val.IsNull() {
140-
val = cfg.TypeDefaults.Apply(val)
141-
}
142-
143143
// By the time we get here, we know:
144144
// - val matches the variable's type constraint
145145
// - val is definitely not cty.NilVal, but might be a null value if the given was already null.

internal/terraform/eval_variable_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ func TestPrepareFinalInputVariableValue(t *testing.T) {
180180
}
181181
]
182182
}
183+
// https://github.com/hashicorp/terraform/issues/32396
184+
// This variable was originally introduced to test the behaviour of the
185+
// dynamic type constraint. You should be able to set primitive types in
186+
// the list consistently.
187+
variable "list_with_nested_collections_dynamic_with_default" {
188+
type = list(
189+
object({
190+
name = optional(string, "default")
191+
taints = optional(list(map(any)), [])
192+
})
193+
)
194+
}
183195
`
184196
cfg := testModuleInline(t, map[string]string{
185197
"main.tf": cfgSrc,
@@ -510,6 +522,39 @@ func TestPrepareFinalInputVariableValue(t *testing.T) {
510522
cty.UnknownVal(cty.String),
511523
``,
512524
},
525+
{
526+
"list_with_nested_collections_dynamic_with_default",
527+
cty.TupleVal([]cty.Value{
528+
cty.ObjectVal(map[string]cty.Value{
529+
"name": cty.StringVal("default"),
530+
}),
531+
cty.ObjectVal(map[string]cty.Value{
532+
"name": cty.StringVal("complex"),
533+
"taints": cty.ListVal([]cty.Value{
534+
cty.MapVal(map[string]cty.Value{
535+
"key": cty.StringVal("my_key"),
536+
"value": cty.StringVal("my_value"),
537+
}),
538+
}),
539+
}),
540+
}),
541+
cty.ListVal([]cty.Value{
542+
cty.ObjectVal(map[string]cty.Value{
543+
"name": cty.StringVal("default"),
544+
"taints": cty.ListValEmpty(cty.Map(cty.String)),
545+
}),
546+
cty.ObjectVal(map[string]cty.Value{
547+
"name": cty.StringVal("complex"),
548+
"taints": cty.ListVal([]cty.Value{
549+
cty.MapVal(map[string]cty.Value{
550+
"key": cty.StringVal("my_key"),
551+
"value": cty.StringVal("my_value"),
552+
}),
553+
}),
554+
}),
555+
}),
556+
``,
557+
},
513558

514559
// complex types
515560

@@ -714,6 +759,39 @@ func TestPrepareFinalInputVariableValue(t *testing.T) {
714759
}),
715760
``,
716761
},
762+
{
763+
"list_with_nested_collections_dynamic_with_default",
764+
cty.TupleVal([]cty.Value{
765+
cty.ObjectVal(map[string]cty.Value{
766+
"name": cty.StringVal("default"),
767+
}),
768+
cty.ObjectVal(map[string]cty.Value{
769+
"name": cty.StringVal("complex"),
770+
"taints": cty.ListVal([]cty.Value{
771+
cty.MapVal(map[string]cty.Value{
772+
"key": cty.StringVal("my_key"),
773+
"value": cty.StringVal("my_value"),
774+
}),
775+
}),
776+
}),
777+
}),
778+
cty.ListVal([]cty.Value{
779+
cty.ObjectVal(map[string]cty.Value{
780+
"name": cty.StringVal("default"),
781+
"taints": cty.ListValEmpty(cty.Map(cty.String)),
782+
}),
783+
cty.ObjectVal(map[string]cty.Value{
784+
"name": cty.StringVal("complex"),
785+
"taints": cty.ListVal([]cty.Value{
786+
cty.MapVal(map[string]cty.Value{
787+
"key": cty.StringVal("my_key"),
788+
"value": cty.StringVal("my_value"),
789+
}),
790+
}),
791+
}),
792+
}),
793+
``,
794+
},
717795

718796
// sensitive
719797
{

0 commit comments

Comments
 (0)