Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage TypeConstraintWithDefaults when parsing default values #184

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions earlydecoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/hashicorp/hcl/v2/hclsyntax"
tfaddr "github.com/hashicorp/terraform-registry-address"
"github.com/hashicorp/terraform-schema/backend"
Expand Down Expand Up @@ -811,6 +812,42 @@ variable "name" {
},
nil,
},
{
"variables with optional type values",
`
variable "name" {
type = object({
foo = optional(string, "food")
bar = optional(number)
})
jpogran marked this conversation as resolved.
Show resolved Hide resolved
}`,
&module.Meta{
Path: path,
ProviderReferences: map[module.ProviderRef]tfaddr.Provider{},
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{
"name": {
Type: cty.Object(map[string]cty.Type{
"foo": cty.String,
"bar": cty.Number,
}),
TypeDefaults: &typeexpr.Defaults{
Type: cty.Object(map[string]cty.Type{
"foo": cty.String,
"bar": cty.Number,
}),
DefaultValues: map[string]cty.Value{
"foo": cty.StringVal("food"),
},
},
},
},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
{
"empty output",
`
Expand Down
6 changes: 5 additions & 1 deletion earlydecoder/load_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ func loadModuleFromFile(file *hcl.File, mod *decodedModule) hcl.Diagnostics {
diags = append(diags, valDiags...)
}
varType := cty.DynamicPseudoType
var defaults *typeexpr.Defaults
if attr, defined := content.Attributes["type"]; defined {
varType, valDiags = typeexpr.TypeConstraint(attr.Expr)
// varType, valDiags = typeexpr.TypeConstraint(attr.Expr)
jpogran marked this conversation as resolved.
Show resolved Hide resolved
varType, defaults, valDiags = typeexpr.TypeConstraintWithDefaults(attr.Expr)
diags = append(diags, valDiags...)
}
if attr, defined := content.Attributes["sensitive"]; defined {
Expand All @@ -239,6 +241,7 @@ func loadModuleFromFile(file *hcl.File, mod *decodedModule) hcl.Diagnostics {
val = cty.DynamicVal
}
}

defaultValue = val
}
}
Expand All @@ -247,6 +250,7 @@ func loadModuleFromFile(file *hcl.File, mod *decodedModule) hcl.Diagnostics {
Description: description,
IsSensitive: isSensitive,
DefaultValue: defaultValue,
TypeDefaults: defaults,
}
case "output":
content, _, contentDiags := block.Body.PartialContent(outputSchema)
Expand Down
3 changes: 3 additions & 0 deletions module/variable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package module

import (
"github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/zclconf/go-cty/cty"
)

Expand All @@ -15,4 +16,6 @@ type Variable struct {
// DefaultValue represents default value if one is defined
// and is decodable without errors, else cty.NilVal
DefaultValue cty.Value

TypeDefaults *typeexpr.Defaults
jpogran marked this conversation as resolved.
Show resolved Hide resolved
}