Merge pull request #7132 from hashicorp/b-empty-variable-types
core: Fix detection of empty list/map defaults
This commit is contained in:
commit
f35812c475
|
@ -891,7 +891,8 @@ func (v *Variable) ValidateTypeAndDefault() error {
|
|||
}
|
||||
|
||||
if v.inferTypeFromDefault() != v.Type() {
|
||||
return fmt.Errorf("'%s' has a default value which is not of type '%s'", v.Name, v.DeclaredType)
|
||||
return fmt.Errorf("'%s' has a default value which is not of type '%s' (got '%s')",
|
||||
v.Name, v.DeclaredType, v.inferTypeFromDefault().Printable())
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -924,13 +925,13 @@ func (v *Variable) inferTypeFromDefault() VariableType {
|
|||
return VariableTypeString
|
||||
}
|
||||
|
||||
var m map[string]string
|
||||
var m map[string]interface{}
|
||||
if err := hilmapstructure.WeakDecode(v.Default, &m); err == nil {
|
||||
v.Default = m
|
||||
return VariableTypeMap
|
||||
}
|
||||
|
||||
var l []string
|
||||
var l []interface{}
|
||||
if err := hilmapstructure.WeakDecode(v.Default, &l); err == nil {
|
||||
v.Default = l
|
||||
return VariableTypeList
|
||||
|
|
|
@ -2,6 +2,7 @@ package config
|
|||
|
||||
import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -85,6 +86,31 @@ func TestConfigCount_var(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConfig_emptyCollections(t *testing.T) {
|
||||
c := testConfig(t, "empty-collections")
|
||||
if len(c.Variables) != 3 {
|
||||
t.Fatalf("bad: expected 3 variables, got %d", len(c.Variables))
|
||||
}
|
||||
for _, variable := range c.Variables {
|
||||
switch variable.Name {
|
||||
case "empty_string":
|
||||
if variable.Default != "" {
|
||||
t.Fatalf("bad: wrong default %q for variable empty_string", variable.Default)
|
||||
}
|
||||
case "empty_map":
|
||||
if !reflect.DeepEqual(variable.Default, map[string]interface{}{}) {
|
||||
t.Fatalf("bad: wrong default %#v for variable empty_map", variable.Default)
|
||||
}
|
||||
case "empty_list":
|
||||
if !reflect.DeepEqual(variable.Default, []interface{}{}) {
|
||||
t.Fatalf("bad: wrong default %#v for variable empty_list", variable.Default)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("Unexpected variable: %s", variable.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigValidate(t *testing.T) {
|
||||
c := testConfig(t, "validate-good")
|
||||
if err := c.Validate(); err != nil {
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
variable "empty_string" {
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "empty_list" {
|
||||
default = []
|
||||
}
|
||||
|
||||
variable "empty_map" {
|
||||
default = {}
|
||||
}
|
Loading…
Reference in New Issue