diff --git a/decoder.go b/decoder.go index 0b39c1b..6e75ece 100644 --- a/decoder.go +++ b/decoder.go @@ -89,7 +89,7 @@ func (d *decoder) decode(name string, node ast.Node, result reflect.Value) error switch k.Kind() { case reflect.Bool: return d.decodeBool(name, node, result) - case reflect.Float64: + case reflect.Float32, reflect.Float64: return d.decodeFloat(name, node, result) case reflect.Int, reflect.Int32, reflect.Int64: return d.decodeInt(name, node, result) @@ -143,7 +143,7 @@ func (d *decoder) decodeFloat(name string, node ast.Node, result reflect.Value) return err } - result.Set(reflect.ValueOf(v)) + result.Set(reflect.ValueOf(v).Convert(result.Type())) return nil } } diff --git a/decoder_test.go b/decoder_test.go index 0b82579..38363ad 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -808,6 +808,36 @@ func TestDecode_intString(t *testing.T) { } } +func TestDecode_float32(t *testing.T) { + var value struct { + A float32 `hcl:"a"` + } + + err := Decode(&value, testReadFile(t, "float.hcl")) + if err != nil { + t.Fatalf("err: %s", err) + } + + if got, want := value.A, float32(1.02); got != want { + t.Fatalf("wrong result %#v; want %#v", got, want) + } +} + +func TestDecode_float64(t *testing.T) { + var value struct { + A float64 `hcl:"a"` + } + + err := Decode(&value, testReadFile(t, "float.hcl")) + if err != nil { + t.Fatalf("err: %s", err) + } + + if got, want := value.A, float64(1.02); got != want { + t.Fatalf("wrong result %#v; want %#v", got, want) + } +} + func TestDecode_intStringAliased(t *testing.T) { var value struct { Count time.Duration