config: vars must be unique

This commit is contained in:
Mitchell Hashimoto 2016-08-25 14:51:49 -07:00
parent 099293b690
commit fbf06e2a59
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 20 additions and 4 deletions

View File

@ -239,6 +239,12 @@ func (c *Config) Validate() error {
vars := c.InterpolatedVariables()
varMap := make(map[string]*Variable)
for _, v := range c.Variables {
if _, ok := varMap[v.Name]; ok {
errs = append(errs, fmt.Errorf(
"Variable '%s': duplicate found. Variable names must be unique.",
v.Name))
}
varMap[v.Name] = v
}

View File

@ -447,6 +447,13 @@ func TestConfigValidate_varDefaultInterpolate(t *testing.T) {
}
}
func TestConfigValidate_varDup(t *testing.T) {
c := testConfig(t, "validate-var-dup")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_varMultiExactNonSlice(t *testing.T) {
c := testConfig(t, "validate-var-multi-exact-non-slice")
if err := c.Validate(); err != nil {

View File

@ -29,6 +29,7 @@ func (t *hclConfigurable) Config() (*Config, error) {
}
type hclVariable struct {
Name string `hcl:",key"`
Default interface{}
Description string
DeclaredType string `hcl:"type"`
@ -36,7 +37,7 @@ func (t *hclConfigurable) Config() (*Config, error) {
}
var rawConfig struct {
Variable map[string]*hclVariable
Variable []*hclVariable
}
// Top-level item should be the object list
@ -56,7 +57,7 @@ func (t *hclConfigurable) Config() (*Config, error) {
config := new(Config)
if len(rawConfig.Variable) > 0 {
config.Variables = make([]*Variable, 0, len(rawConfig.Variable))
for k, v := range rawConfig.Variable {
for _, v := range rawConfig.Variable {
// Defaults turn into a slice of map[string]interface{} and
// we need to make sure to convert that down into the
// proper type for Config.
@ -72,7 +73,7 @@ func (t *hclConfigurable) Config() (*Config, error) {
}
newVar := &Variable{
Name: k,
Name: v.Name,
DeclaredType: v.DeclaredType,
Default: v.Default,
Description: v.Description,

View File

@ -2,7 +2,7 @@ variable "foo" {
default = "bar"
}
variable "foo" {
variable "foo2" {
default = {
foo = "bar"
}

View File

@ -0,0 +1,2 @@
variable "foo" {}
variable "foo" {}