From 2fb6ae28c66618f3c2f0e9fb29ac874fb45b30c6 Mon Sep 17 00:00:00 2001 From: Isabel Andrade <67916678+beandrad@users.noreply.github.com> Date: Fri, 4 Jun 2021 11:53:43 +0100 Subject: [PATCH] Add `default` variable schema (#53) 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 --- internal/schema/0.12/variable_block.go | 5 ---- internal/schema/0.14/variable_block.go | 5 ---- schema/schema_merge.go | 35 +++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/internal/schema/0.12/variable_block.go b/internal/schema/0.12/variable_block.go index ec88dd1d..cc98cc08 100644 --- a/internal/schema/0.12/variable_block.go +++ b/internal/schema/0.12/variable_block.go @@ -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), }, diff --git a/internal/schema/0.14/variable_block.go b/internal/schema/0.14/variable_block.go index ab5fbd62..a78e4442 100644 --- a/internal/schema/0.14/variable_block.go +++ b/internal/schema/0.14/variable_block.go @@ -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, diff --git a/schema/schema_merge.go b/schema/schema_merge.go index 65943f23..3ed0e009 100644 --- a/schema/schema_merge.go +++ b/schema/schema_merge.go @@ -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" ) @@ -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 {