Skip to content

Commit

Permalink
Add default variable schema (#53)
Browse files Browse the repository at this point in the history
To support completion/hover/highlighting for variable `default`.

As part of this change, the `default` attribute was removed from the
variable schema block so that it can be set by the decoder.

Related to hashicorp/terraform-ls#537
  • Loading branch information
beandrad authored Jun 4, 2021
1 parent 88259b2 commit 2fb6ae2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
5 changes: 0 additions & 5 deletions internal/schema/0.12/variable_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ func variableBlockSchema(v *version.Version) *schema.BlockSchema {
IsOptional: true,
Description: lang.Markdown("Type constraint restricting the type of value to accept, e.g. `string` or `list(string)`"),
},
"default": {
Expr: schema.ExprConstraints{},
IsOptional: true,
Description: lang.Markdown("Default value to use when variable is not explicitly set"),
},
},
Blocks: make(map[string]*schema.BlockSchema, 0),
},
Expand Down
5 changes: 0 additions & 5 deletions internal/schema/0.14/variable_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ var variableBlockSchema = &schema.BlockSchema{
IsOptional: true,
Description: lang.Markdown("Type constraint restricting the type of value to accept, e.g. `string` or `list(string)`"),
},
"default": {
Expr: schema.ExprConstraints{},
IsOptional: true,
Description: lang.Markdown("Default value to use when variable is not explicitly set"),
},
"sensitive": {
Expr: schema.LiteralTypeOnly(cty.Bool),
IsOptional: true,
Expand Down
35 changes: 34 additions & 1 deletion schema/schema_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-registry-address"
tfaddr "github.com/hashicorp/terraform-registry-address"
"github.com/hashicorp/terraform-schema/module"
)

Expand Down Expand Up @@ -166,9 +166,42 @@ func (m *SchemaMerger) SchemaForModule(meta *module.Meta) (*schema.BodySchema, e
}
}

if _, ok := mergedSchema.Blocks["variable"]; ok {
mergedSchema.Blocks["variable"].Labels = []*schema.LabelSchema{
{
Name: "name",
IsDepKey: true,
Description: lang.PlainText("Variable name"),
},
}
mergedSchema.Blocks["variable"].DependentBody = variableDependentBody(meta.Variables)
}
return mergedSchema, nil
}

func variableDependentBody(vars map[string]module.Variable) map[schema.SchemaKey]*schema.BodySchema {
depBodies := make(map[schema.SchemaKey]*schema.BodySchema)

for name, mVar := range vars {
depKeys := schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: name},
},
}
depBodies[schema.NewSchemaKey(depKeys)] = &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"default": {
Expr: schema.ExprConstraints{schema.LiteralTypeExpr{Type: mVar.Type}},
IsOptional: true,
Description: lang.Markdown("Default value to use when variable is not explicitly set"),
},
},
}
}

return depBodies
}

type ProviderReferences map[module.ProviderRef]tfaddr.Provider

func (pr ProviderReferences) ReferencesOfProvider(addr tfaddr.Provider) []module.ProviderRef {
Expand Down

0 comments on commit 2fb6ae2

Please sign in to comment.