Merge pull request #881 from sparkprime/float_fix

helper/schema: add some missing Float cases
This commit is contained in:
Mitchell Hashimoto 2015-01-28 15:19:29 -08:00
commit 0c672aa03b
6 changed files with 145 additions and 5 deletions

View File

@ -82,6 +82,8 @@ func addrToSchema(addr []string, schemaMap map[string]*Schema) []*Schema {
fallthrough
case TypeInt:
fallthrough
case TypeFloat:
fallthrough
case TypeString:
if len(addr) > 0 {
return nil

View File

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

View File

@ -23,10 +23,10 @@ func (r *MapFieldReader) ReadField(address []string) (FieldReadResult, error) {
switch schema.Type {
case TypeBool:
fallthrough
case TypeFloat:
fallthrough
case TypeInt:
fallthrough
case TypeFloat:
fallthrough
case TypeString:
return r.readPrimitive(address, schema)
case TypeList:

View File

@ -78,6 +78,8 @@ func (w *MapFieldWriter) set(addr []string, value interface{}) error {
fallthrough
case TypeInt:
fallthrough
case TypeFloat:
fallthrough
case TypeString:
return w.setPrimitive(addr, value, schema)
case TypeList:
@ -235,8 +237,13 @@ func (w *MapFieldWriter) setPrimitive(
if err := mapstructure.Decode(v, &n); err != nil {
return fmt.Errorf("%s: %s", k, err)
}
set = strconv.FormatInt(int64(n), 10)
case TypeFloat:
var n float64
if err := mapstructure.Decode(v, &n); err != nil {
return fmt.Errorf("%s: %s", k, err)
}
set = strconv.FormatFloat(float64(n), 'G', -1, 64)
default:
return fmt.Errorf("Unknown type: %#v", schema.Type)
}

View File

@ -1,6 +1,7 @@
package schema
import (
"math"
"reflect"
"testing"
@ -615,6 +616,81 @@ func TestResourceDataGet(t *testing.T) {
Value: []interface{}{},
},
// #20 Float zero
{
Schema: map[string]*Schema{
"ratio": &Schema{
Type: TypeFloat,
Optional: true,
Computed: true,
},
},
State: nil,
Diff: nil,
Key: "ratio",
Value: 0.0,
},
// #21 Float given
{
Schema: map[string]*Schema{
"ratio": &Schema{
Type: TypeFloat,
Optional: true,
Computed: true,
},
},
State: &terraform.InstanceState{
Attributes: map[string]string{
"ratio": "0.5",
},
},
Diff: nil,
Key: "ratio",
Value: 0.5,
},
// #22 Float diff
{
Schema: map[string]*Schema{
"ratio": &Schema{
Type: TypeFloat,
Optional: true,
Computed: true,
},
},
State: &terraform.InstanceState{
Attributes: map[string]string{
"ratio": "-0.5",
},
},
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ratio": &terraform.ResourceAttrDiff{
Old: "-0.5",
New: "33.0",
},
},
},
Key: "ratio",
Value: 33.0,
},
}
for i, tc := range cases {
@ -1412,6 +1488,52 @@ func TestResourceDataSet(t *testing.T) {
return v
},
},
// #12: List of floats, set list
{
Schema: map[string]*Schema{
"ratios": &Schema{
Type: TypeList,
Computed: true,
Elem: &Schema{Type: TypeFloat},
},
},
State: nil,
Diff: nil,
Key: "ratios",
Value: []float64{1.0, 2.2, 5.5},
GetKey: "ratios",
GetValue: []interface{}{1.0, 2.2, 5.5},
},
// #12: Set of floats, set list
{
Schema: map[string]*Schema{
"ratios": &Schema{
Type: TypeSet,
Computed: true,
Elem: &Schema{Type: TypeFloat},
Set: func(a interface{}) int {
return int(math.Float64bits(a.(float64)))
},
},
},
State: nil,
Diff: nil,
Key: "ratios",
Value: []float64{1.0, 2.2, 5.5},
GetKey: "ratios",
GetValue: []interface{}{1.0, 2.2, 5.5},
},
}
for i, tc := range cases {

View File

@ -78,6 +78,7 @@ type Schema struct {
//
// TypeBool - bool
// TypeInt - int
// TypeFloat - float64
// TypeString - string
// TypeList - []interface{}
// TypeMap - map[string]interface{}
@ -406,6 +407,8 @@ func (m schemaMap) Input(
fallthrough
case TypeInt:
fallthrough
case TypeFloat:
fallthrough
case TypeString:
value, err = m.inputString(input, k, v)
default:
@ -1084,6 +1087,12 @@ func (m schemaMap) validatePrimitive(
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
case TypeFloat:
// Verify that we can parse this as an int
var n float64
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
case TypeString:
// Verify that we can parse this as a string
var n string