helper/schema: add float type

This commit is contained in:
Mitchell Hashimoto 2015-01-10 16:04:01 -08:00
parent cf94a79955
commit 1fcd24cf67
8 changed files with 42 additions and 3 deletions

View File

@ -243,6 +243,21 @@ func stringToPrimitive(
return nil, err return nil, err
} }
returnVal = v
case TypeFloat:
if value == "" {
returnVal = 0.0
break
}
if computed {
break
}
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, err
}
returnVal = v returnVal = v
case TypeInt: case TypeInt:
if value == "" { if value == "" {
@ -262,7 +277,7 @@ func stringToPrimitive(
case TypeString: case TypeString:
returnVal = value returnVal = value
default: default:
panic(fmt.Sprintf("Unknown type: %#v", schema.Type)) panic(fmt.Sprintf("Unknown type: %s", schema.Type))
} }
return returnVal, nil return returnVal, nil

View File

@ -80,6 +80,8 @@ func (r *ConfigFieldReader) readField(
switch schema.Type { switch schema.Type {
case TypeBool: case TypeBool:
fallthrough fallthrough
case TypeFloat:
fallthrough
case TypeInt: case TypeInt:
fallthrough fallthrough
case TypeString: case TypeString:
@ -96,7 +98,7 @@ func (r *ConfigFieldReader) readField(
&nestedConfigFieldReader{r}, &nestedConfigFieldReader{r},
address, schema.Elem.(map[string]*Schema)) address, schema.Elem.(map[string]*Schema))
default: default:
panic(fmt.Sprintf("Unknown type: %#v", schema.Type)) panic(fmt.Sprintf("Unknown type: %s", schema.Type))
} }
} }

View File

@ -18,6 +18,7 @@ func TestConfigFieldReader(t *testing.T) {
Config: testConfig(t, map[string]interface{}{ Config: testConfig(t, map[string]interface{}{
"bool": true, "bool": true,
"float": 3.1415,
"int": 42, "int": 42,
"string": "string", "string": "string",

View File

@ -41,6 +41,8 @@ func (r *DiffFieldReader) ReadField(address []string) (FieldReadResult, error) {
switch schema.Type { switch schema.Type {
case TypeBool: case TypeBool:
fallthrough fallthrough
case TypeFloat:
fallthrough
case TypeInt: case TypeInt:
fallthrough fallthrough
case TypeString: case TypeString:

View File

@ -179,6 +179,11 @@ func TestDiffFieldReader(t *testing.T) {
New: "42", New: "42",
}, },
"float": &terraform.ResourceAttrDiff{
Old: "",
New: "3.1415",
},
"string": &terraform.ResourceAttrDiff{ "string": &terraform.ResourceAttrDiff{
Old: "", Old: "",
New: "string", New: "string",

View File

@ -23,6 +23,8 @@ func (r *MapFieldReader) ReadField(address []string) (FieldReadResult, error) {
switch schema.Type { switch schema.Type {
case TypeBool: case TypeBool:
fallthrough fallthrough
case TypeFloat:
fallthrough
case TypeInt: case TypeInt:
fallthrough fallthrough
case TypeString: case TypeString:
@ -36,7 +38,7 @@ func (r *MapFieldReader) ReadField(address []string) (FieldReadResult, error) {
case typeObject: case typeObject:
return readObjectField(r, address, schema.Elem.(map[string]*Schema)) return readObjectField(r, address, schema.Elem.(map[string]*Schema))
default: default:
panic(fmt.Sprintf("Unknown type: %#v", schema.Type)) panic(fmt.Sprintf("Unknown type: %s", schema.Type))
} }
} }

View File

@ -17,6 +17,7 @@ func TestMapFieldReader(t *testing.T) {
Map: BasicMapReader(map[string]string{ Map: BasicMapReader(map[string]string{
"bool": "true", "bool": "true",
"int": "42", "int": "42",
"float": "3.1415",
"string": "string", "string": "string",
"list.#": "2", "list.#": "2",

View File

@ -189,6 +189,7 @@ func testFieldReader(t *testing.T, f func(map[string]*Schema) FieldReader) {
schema := map[string]*Schema{ schema := map[string]*Schema{
// Primitives // Primitives
"bool": &Schema{Type: TypeBool}, "bool": &Schema{Type: TypeBool},
"float": &Schema{Type: TypeFloat},
"int": &Schema{Type: TypeInt}, "int": &Schema{Type: TypeInt},
"string": &Schema{Type: TypeString}, "string": &Schema{Type: TypeString},
@ -265,6 +266,16 @@ func testFieldReader(t *testing.T, f func(map[string]*Schema) FieldReader) {
false, false,
}, },
"float": {
[]string{"float"},
FieldReadResult{
Value: 3.1415,
Exists: true,
Computed: false,
},
false,
},
"int": { "int": {
[]string{"int"}, []string{"int"},
FieldReadResult{ FieldReadResult{