Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode NUMBER into float32 and float64 fields #210

Merged
merged 1 commit into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
switch n := node.(type) {
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)
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestDecode_interface(t *testing.T) {
false,
map[string]interface{}{
"a": 1.02,
"b": 2,
},
},
{
Expand Down Expand Up @@ -811,6 +812,7 @@ func TestDecode_intString(t *testing.T) {
func TestDecode_float32(t *testing.T) {
var value struct {
A float32 `hcl:"a"`
B float32 `hcl:"b"`
}

err := Decode(&value, testReadFile(t, "float.hcl"))
Expand All @@ -821,11 +823,15 @@ func TestDecode_float32(t *testing.T) {
if got, want := value.A, float32(1.02); 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) {
var value struct {
A float64 `hcl:"a"`
B float64 `hcl:"b"`
}

err := Decode(&value, testReadFile(t, "float.hcl"))
Expand All @@ -836,6 +842,9 @@ func TestDecode_float64(t *testing.T) {
if got, want := value.A, float64(1.02); 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) {
Expand Down
1 change: 1 addition & 0 deletions test-fixtures/float.hcl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
a = 1.02
b = 2
3 changes: 2 additions & 1 deletion test-fixtures/float.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"a": 1.02
"a": 1.02,
"b": 2
}