Merge pull request #768 from hashicorp/f-schema-float

helper/schema: Add TypeFloat
This commit is contained in:
Mitchell Hashimoto 2015-01-10 16:06:49 -08:00
commit 096c9fef1e
11 changed files with 48 additions and 5 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{

View File

@ -31,6 +31,7 @@ const (
TypeInvalid ValueType = iota TypeInvalid ValueType = iota
TypeBool TypeBool
TypeInt TypeInt
TypeFloat
TypeString TypeString
TypeList TypeList
TypeMap TypeMap
@ -47,6 +48,8 @@ func (t ValueType) Zero() interface{} {
return false return false
case TypeInt: case TypeInt:
return 0 return 0
case TypeFloat:
return 0.0
case TypeString: case TypeString:
return "" return ""
case TypeList: case TypeList:

View File

@ -15,6 +15,7 @@ func TestValueType_Zero(t *testing.T) {
}{ }{
{TypeBool, false}, {TypeBool, false},
{TypeInt, 0}, {TypeInt, 0},
{TypeFloat, 0.0},
{TypeString, ""}, {TypeString, ""},
{TypeList, []interface{}{}}, {TypeList, []interface{}{}},
{TypeMap, map[string]interface{}{}}, {TypeMap, map[string]interface{}{}},

View File

@ -4,9 +4,9 @@ package schema
import "fmt" import "fmt"
const _ValueType_name = "TypeInvalidTypeBoolTypeIntTypeStringTypeListTypeMapTypeSettypeObject" const _ValueType_name = "TypeInvalidTypeBoolTypeIntTypeFloatTypeStringTypeListTypeMapTypeSettypeObject"
var _ValueType_index = [...]uint8{0, 11, 19, 26, 36, 44, 51, 58, 68} var _ValueType_index = [...]uint8{0, 11, 19, 26, 35, 45, 53, 60, 67, 77}
func (i ValueType) String() string { func (i ValueType) String() string {
if i < 0 || i+1 >= ValueType(len(_ValueType_index)) { if i < 0 || i+1 >= ValueType(len(_ValueType_index)) {