Merge pull request #210 from hashicorp/decode-number-into-float

Decode NUMBER into float32 and float64 fields
This commit is contained in:
Mitchell Hashimoto 2017-09-14 08:46:24 -07:00 committed by GitHub
commit 68e816d1c7
4 changed files with 13 additions and 2 deletions

View File

@ -137,7 +137,7 @@ func (d *decoder) decodeBool(name string, node ast.Node, result reflect.Value) e
func (d *decoder) decodeFloat(name string, node ast.Node, result reflect.Value) error { func (d *decoder) decodeFloat(name string, node ast.Node, result reflect.Value) error {
switch n := node.(type) { switch n := node.(type) {
case *ast.LiteralType: case *ast.LiteralType:
if n.Token.Type == token.FLOAT { if n.Token.Type == token.FLOAT || n.Token.Type == token.NUMBER {
v, err := strconv.ParseFloat(n.Token.Text, 64) v, err := strconv.ParseFloat(n.Token.Text, 64)
if err != nil { if err != nil {
return err return err

View File

@ -73,6 +73,7 @@ func TestDecode_interface(t *testing.T) {
false, false,
map[string]interface{}{ map[string]interface{}{
"a": 1.02, "a": 1.02,
"b": 2,
}, },
}, },
{ {
@ -811,6 +812,7 @@ func TestDecode_intString(t *testing.T) {
func TestDecode_float32(t *testing.T) { func TestDecode_float32(t *testing.T) {
var value struct { var value struct {
A float32 `hcl:"a"` A float32 `hcl:"a"`
B float32 `hcl:"b"`
} }
err := Decode(&value, testReadFile(t, "float.hcl")) err := Decode(&value, testReadFile(t, "float.hcl"))
@ -821,11 +823,15 @@ func TestDecode_float32(t *testing.T) {
if got, want := value.A, float32(1.02); got != want { if got, want := value.A, float32(1.02); got != want {
t.Fatalf("wrong result %#v; want %#v", got, want) t.Fatalf("wrong result %#v; want %#v", got, want)
} }
if got, want := value.B, float32(2); got != want {
t.Fatalf("wrong result %#v; want %#v", got, want)
}
} }
func TestDecode_float64(t *testing.T) { func TestDecode_float64(t *testing.T) {
var value struct { var value struct {
A float64 `hcl:"a"` A float64 `hcl:"a"`
B float64 `hcl:"b"`
} }
err := Decode(&value, testReadFile(t, "float.hcl")) err := Decode(&value, testReadFile(t, "float.hcl"))
@ -836,6 +842,9 @@ func TestDecode_float64(t *testing.T) {
if got, want := value.A, float64(1.02); got != want { if got, want := value.A, float64(1.02); got != want {
t.Fatalf("wrong result %#v; want %#v", got, want) t.Fatalf("wrong result %#v; want %#v", got, want)
} }
if got, want := value.B, float64(2); got != want {
t.Fatalf("wrong result %#v; want %#v", got, want)
}
} }
func TestDecode_intStringAliased(t *testing.T) { func TestDecode_intStringAliased(t *testing.T) {

View File

@ -1 +1,2 @@
a = 1.02 a = 1.02
b = 2

View File

@ -1,3 +1,4 @@
{ {
"a": 1.02 "a": 1.02,
"b": 2
} }