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
}
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
case TypeInt:
if value == "" {
@ -262,7 +277,7 @@ func stringToPrimitive(
case TypeString:
returnVal = value
default:
panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
panic(fmt.Sprintf("Unknown type: %s", schema.Type))
}
return returnVal, nil

View File

@ -80,6 +80,8 @@ func (r *ConfigFieldReader) readField(
switch schema.Type {
case TypeBool:
fallthrough
case TypeFloat:
fallthrough
case TypeInt:
fallthrough
case TypeString:
@ -96,7 +98,7 @@ func (r *ConfigFieldReader) readField(
&nestedConfigFieldReader{r},
address, schema.Elem.(map[string]*Schema))
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{}{
"bool": true,
"float": 3.1415,
"int": 42,
"string": "string",

View File

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

View File

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

View File

@ -23,6 +23,8 @@ func (r *MapFieldReader) ReadField(address []string) (FieldReadResult, error) {
switch schema.Type {
case TypeBool:
fallthrough
case TypeFloat:
fallthrough
case TypeInt:
fallthrough
case TypeString:
@ -36,7 +38,7 @@ func (r *MapFieldReader) ReadField(address []string) (FieldReadResult, error) {
case typeObject:
return readObjectField(r, address, schema.Elem.(map[string]*Schema))
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{
"bool": "true",
"int": "42",
"float": "3.1415",
"string": "string",
"list.#": "2",

View File

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