Support decoding HCL floats into Go float32

This commit is contained in:
Jelte Fennema 2017-05-05 10:58:37 +02:00 committed by Martin Atkins
parent a4b07c25de
commit 392dba7d90
2 changed files with 32 additions and 2 deletions

View File

@ -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
}
}

View File

@ -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