Skip to content

Commit

Permalink
Skip inferring module inputs for default values (#289)
Browse files Browse the repository at this point in the history
* Bump hcl-lang to caa96d6

* Skip inferring module inputs from default values

* Fix test cases

* Use any for module inputs that have no type
  • Loading branch information
dbanck authored Nov 2, 2023
1 parent 18e2fd7 commit 535af8a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 72 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hc-install v0.6.1
github.com/hashicorp/hcl-lang v0.0.0-20231005141915-402c82e5ccb1
github.com/hashicorp/hcl-lang v0.0.0-20231102085329-caa96d65d15b
github.com/hashicorp/hcl/v2 v2.19.1
github.com/hashicorp/terraform-exec v0.19.0
github.com/hashicorp/terraform-json v0.17.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hc-install v0.6.1 h1:IGxShH7AVhPaSuSJpKtVi/EFORNjO+OYVJJrAtGG2mY=
github.com/hashicorp/hc-install v0.6.1/go.mod h1:0fW3jpg+wraYSnFDJ6Rlie3RvLf1bIqVIkzoon4KoVE=
github.com/hashicorp/hcl-lang v0.0.0-20231005141915-402c82e5ccb1 h1:GEjoNvn9WaZDMpUtNs0VB+FtP/ndclhr10Zcmkraax4=
github.com/hashicorp/hcl-lang v0.0.0-20231005141915-402c82e5ccb1/go.mod h1:67DxtN9WnM5dTdDPFn79G2Azgs9b5/8yOKP5LU2XBIc=
github.com/hashicorp/hcl-lang v0.0.0-20231102085329-caa96d65d15b h1:j8TNMurAPPN05V2/FXOo16c6RpNKY57ZV7iqwO0vAOc=
github.com/hashicorp/hcl-lang v0.0.0-20231102085329-caa96d65d15b/go.mod h1:f8Trmd1qEAs00+y4etS5uTDqYhU3Z/pxJgzsTwWXWM8=
github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI=
github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/hashicorp/terraform-exec v0.19.0 h1:FpqZ6n50Tk95mItTSS9BjeOVUb4eg81SpgVtZNNtFSM=
Expand Down Expand Up @@ -137,8 +137,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
3 changes: 1 addition & 2 deletions internal/schema/0.12/variable_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func variableBlockSchema(v *version.Version) *schema.BlockSchema {
ScopeId: refscope.VariableScope,
AsReference: true,
AsTypeOf: &schema.BlockAsTypeOf{
AttributeExpr: "type",
AttributeValue: "default",
AttributeExpr: "type",
},
},
SemanticTokenModifiers: lang.SemanticTokenModifiers{tokmod.Variable},
Expand Down
3 changes: 1 addition & 2 deletions internal/schema/0.14/variable_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func variableBlockSchema() *schema.BlockSchema {
ScopeId: refscope.VariableScope,
AsReference: true,
AsTypeOf: &schema.BlockAsTypeOf{
AttributeExpr: "type",
AttributeValue: "default",
AttributeExpr: "type",
},
},
SemanticTokenModifiers: lang.SemanticTokenModifiers{tokmod.Variable},
Expand Down
11 changes: 6 additions & 5 deletions schema/module_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ func schemaForDeclaredDependentModuleBlock(module module.DeclaredModuleCall, mod
}

typ := input.Type
defaultType := input.Default.Type()
if typ == cty.DynamicPseudoType && (defaultType != cty.DynamicPseudoType && defaultType != cty.NilType) {
typ = defaultType
if typ == cty.NilType {
typ = cty.DynamicPseudoType
}

aSchema.Constraint = convertAttributeTypeToConstraint(typ)

attributes[input.Name] = aSchema
Expand Down Expand Up @@ -111,8 +109,11 @@ func schemaForDependentModuleBlock(module module.InstalledModuleCall, modMeta *m
attributes := make(map[string]*schema.AttributeSchema, 0)

for name, modVar := range modMeta.Variables {
varType := modVar.Type
if varType == cty.NilType {
varType = cty.DynamicPseudoType
}
aSchema := moduleVarToAttribute(modVar)
varType := typeOfModuleVar(modVar)
aSchema.Constraint = convertAttributeTypeToConstraint(varType)
aSchema.OriginForTarget = &schema.PathTarget{
Address: schema.Address{
Expand Down
6 changes: 3 additions & 3 deletions schema/module_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestSchemaForDependentModuleBlock_basic(t *testing.T) {
},
},
"another_var": {
Constraint: schema.AnyExpression{OfType: cty.String},
Constraint: schema.AnyExpression{OfType: cty.DynamicPseudoType},
IsOptional: true,
OriginForTarget: &schema.PathTarget{
Address: schema.Address{
Expand All @@ -117,7 +117,7 @@ func TestSchemaForDependentModuleBlock_basic(t *testing.T) {
},
Constraints: schema.Constraints{
ScopeId: "variable",
Type: cty.String,
Type: cty.DynamicPseudoType,
},
},
},
Expand Down Expand Up @@ -512,7 +512,7 @@ func TestSchemaForDeclaredDependentModuleBlock_basic(t *testing.T) {
IsRequired: true,
},
"foo_var": {
Constraint: schema.AnyExpression{OfType: cty.Number},
Constraint: schema.AnyExpression{OfType: cty.DynamicPseudoType},
IsOptional: true,
},
"another_var": {
Expand Down
13 changes: 1 addition & 12 deletions schema/variable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func SchemaForVariables(vars map[string]module.Variable, modPath string) (*schem

for name, modVar := range vars {
aSchema := moduleVarToAttribute(modVar)
varType := typeOfModuleVar(modVar)
varType := modVar.Type
aSchema.Constraint = schema.LiteralType{Type: varType}
aSchema.OriginForTarget = &schema.PathTarget{
Address: schema.Address{
Expand Down Expand Up @@ -60,14 +60,3 @@ func moduleVarToAttribute(modVar module.Variable) *schema.AttributeSchema {

return aSchema
}

func typeOfModuleVar(modVar module.Variable) cty.Type {
if (modVar.Type == cty.DynamicPseudoType || modVar.Type == cty.NilType) &&
modVar.DefaultValue != cty.NilVal {
// infer type from default value if one is not specified
// or when it's "any"
return modVar.DefaultValue.Type()
}

return modVar.Type
}
43 changes: 0 additions & 43 deletions schema/variable_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,49 +94,6 @@ func TestSchemaForVariables(t *testing.T) {
},
}},
},
{
"attribute with type from default value",
map[string]module.Variable{
"name": {
Description: "name of the module",
Type: cty.DynamicPseudoType,
DefaultValue: cty.StringVal("default"),
},
"id": {
Description: "id of the module",
Type: cty.NilType,
DefaultValue: cty.NumberIntVal(42),
},
},
&schema.BodySchema{Attributes: map[string]*schema.AttributeSchema{
"name": {
Description: lang.MarkupContent{
Value: "name of the module",
Kind: lang.PlainTextKind,
},
IsOptional: true,
Constraint: schema.LiteralType{Type: cty.String},
OriginForTarget: &schema.PathTarget{
Address: schema.Address{schema.StaticStep{Name: "var"}, schema.AttrNameStep{}},
Path: lang.Path{Path: "./local", LanguageID: "terraform"},
Constraints: schema.Constraints{ScopeId: "variable", Type: cty.String},
},
},
"id": {
Description: lang.MarkupContent{
Value: "id of the module",
Kind: lang.PlainTextKind,
},
Constraint: schema.LiteralType{Type: cty.Number},
IsOptional: true,
OriginForTarget: &schema.PathTarget{
Address: schema.Address{schema.StaticStep{Name: "var"}, schema.AttrNameStep{}},
Path: lang.Path{Path: "./local", LanguageID: "terraform"},
Constraints: schema.Constraints{ScopeId: "variable", Type: cty.Number},
},
},
}},
},
}

modPath := "./local"
Expand Down

0 comments on commit 535af8a

Please sign in to comment.