Skip to content

Commit

Permalink
Add support to create schema for variables (#49)
Browse files Browse the repository at this point in the history
Given a map of module variables, create the corresponding body
schema with the corresponding variables as attribute schemas.

Related to hashicorp/terraform-ls#50.
  • Loading branch information
beandrad authored Jun 1, 2021
1 parent 85a7417 commit f0b01c2
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
25 changes: 25 additions & 0 deletions schema/variable_schema.go
Original file line number Diff line number Diff line change
@@ -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
}
89 changes: 89 additions & 0 deletions schema/variable_schema_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit f0b01c2

Please sign in to comment.