Merge pull request #768 from hashicorp/f-schema-float
helper/schema: Add TypeFloat
This commit is contained in:
commit
096c9fef1e
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ func TestConfigFieldReader(t *testing.T) {
|
|||
|
||||
Config: testConfig(t, map[string]interface{}{
|
||||
"bool": true,
|
||||
"float": 3.1415,
|
||||
"int": 42,
|
||||
"string": "string",
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -179,6 +179,11 @@ func TestDiffFieldReader(t *testing.T) {
|
|||
New: "42",
|
||||
},
|
||||
|
||||
"float": &terraform.ResourceAttrDiff{
|
||||
Old: "",
|
||||
New: "3.1415",
|
||||
},
|
||||
|
||||
"string": &terraform.ResourceAttrDiff{
|
||||
Old: "",
|
||||
New: "string",
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -31,6 +31,7 @@ const (
|
|||
TypeInvalid ValueType = iota
|
||||
TypeBool
|
||||
TypeInt
|
||||
TypeFloat
|
||||
TypeString
|
||||
TypeList
|
||||
TypeMap
|
||||
|
@ -47,6 +48,8 @@ func (t ValueType) Zero() interface{} {
|
|||
return false
|
||||
case TypeInt:
|
||||
return 0
|
||||
case TypeFloat:
|
||||
return 0.0
|
||||
case TypeString:
|
||||
return ""
|
||||
case TypeList:
|
||||
|
|
|
@ -15,6 +15,7 @@ func TestValueType_Zero(t *testing.T) {
|
|||
}{
|
||||
{TypeBool, false},
|
||||
{TypeInt, 0},
|
||||
{TypeFloat, 0.0},
|
||||
{TypeString, ""},
|
||||
{TypeList, []interface{}{}},
|
||||
{TypeMap, map[string]interface{}{}},
|
||||
|
|
|
@ -4,9 +4,9 @@ package schema
|
|||
|
||||
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 {
|
||||
if i < 0 || i+1 >= ValueType(len(_ValueType_index)) {
|
||||
|
|
Loading…
Reference in New Issue