forked from hashicorp/terraform-schema
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to create schema for variables (hashicorp#49)
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
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |