From a1b1192c9d9717b21660bfb754e2e84fd0900c2e Mon Sep 17 00:00:00 2001 From: Isabel Andrade Date: Tue, 1 Jun 2021 14:06:39 +0100 Subject: [PATCH] Add support to create schema for variables Given a map of module variables, create the corresponding body schema with the corresponding variables as attribute schemas. Related to hashicorp/terraform-ls#50. --- schema/variable_schema.go | 25 ++++++++++ schema/variable_schema_test.go | 89 ++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 schema/variable_schema.go create mode 100644 schema/variable_schema_test.go diff --git a/schema/variable_schema.go b/schema/variable_schema.go new file mode 100644 index 00000000..a25e1694 --- /dev/null +++ b/schema/variable_schema.go @@ -0,0 +1,25 @@ +package schema + +import ( + "github.com/hashicorp/hcl-lang/lang" + "github.com/hashicorp/hcl-lang/schema" + "github.com/hashicorp/terraform-schema/module" +) + +func SchemaForVariables(vars map[string]module.Variable) (*schema.BodySchema, error) { + varSchemas := make(map[string]*schema.AttributeSchema) + + for name, v := range vars { + varSchemas[name] = &schema.AttributeSchema{ + Description: lang.MarkupContent{ + Value: v.Description, + Kind: lang.PlainTextKind, + }, + Expr: schema.ExprConstraints{schema.LiteralTypeExpr{Type: v.Type}}, + } + } + + return &schema.BodySchema{ + Attributes: varSchemas, + }, nil +} diff --git a/schema/variable_schema_test.go b/schema/variable_schema_test.go new file mode 100644 index 00000000..55282a91 --- /dev/null +++ b/schema/variable_schema_test.go @@ -0,0 +1,89 @@ +package schema + +import ( + "fmt" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/hcl-lang/lang" + "github.com/hashicorp/hcl-lang/schema" + "github.com/hashicorp/terraform-schema/module" + "github.com/zclconf/go-cty-debug/ctydebug" + "github.com/zclconf/go-cty/cty" +) + +func TestSchemaForVariables(t *testing.T) { + testCases := []struct { + name string + variables map[string]module.Variable + expectedSchema *schema.BodySchema + }{ + { + "empty schema", + make(map[string]module.Variable), + &schema.BodySchema{Attributes: make(map[string]*schema.AttributeSchema)}, + }, + { + "one attribute schema", + map[string]module.Variable{ + "name": module.Variable{ + Description: "name of the module", + Type: cty.String, + }, + }, + &schema.BodySchema{Attributes: map[string]*schema.AttributeSchema{ + "name": &schema.AttributeSchema{ + Description: lang.MarkupContent{ + Value: "name of the module", + Kind: lang.PlainTextKind, + }, + Expr: schema.ExprConstraints{schema.LiteralTypeExpr{cty.String}}, + }, + }}, + }, + { + "two attribute schema", + map[string]module.Variable{ + "name": module.Variable{ + Description: "name of the module", + Type: cty.String, + }, + "id": module.Variable{ + Description: "id of the module", + Type: cty.Number, + }, + }, + &schema.BodySchema{Attributes: map[string]*schema.AttributeSchema{ + "name": &schema.AttributeSchema{ + Description: lang.MarkupContent{ + Value: "name of the module", + Kind: lang.PlainTextKind, + }, + Expr: schema.ExprConstraints{schema.LiteralTypeExpr{cty.String}}, + }, + "id": &schema.AttributeSchema{ + Description: lang.MarkupContent{ + Value: "id of the module", + Kind: lang.PlainTextKind, + }, + Expr: schema.ExprConstraints{schema.LiteralTypeExpr{cty.Number}}, + }, + }}, + }, + } + + for i, tc := range testCases { + t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) { + actualSchema, err := SchemaForVariables(tc.variables) + + if err != nil { + t.Fatal(err) + } + + diff := cmp.Diff(tc.expectedSchema, actualSchema, ctydebug.CmpOptions) + if diff != "" { + t.Fatalf("unexpected schema %s", diff) + } + }) + } +}