Support decoding HCL floats into Go float32
This commit is contained in:
parent
a4b07c25de
commit
392dba7d90
@ -89,7 +89,7 @@ func (d *decoder) decode(name string, node ast.Node, result reflect.Value) error
|
|||||||
switch k.Kind() {
|
switch k.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
return d.decodeBool(name, node, result)
|
return d.decodeBool(name, node, result)
|
||||||
case reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
return d.decodeFloat(name, node, result)
|
return d.decodeFloat(name, node, result)
|
||||||
case reflect.Int, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int32, reflect.Int64:
|
||||||
return d.decodeInt(name, node, result)
|
return d.decodeInt(name, node, result)
|
||||||
@ -143,7 +143,7 @@ func (d *decoder) decodeFloat(name string, node ast.Node, result reflect.Value)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Set(reflect.ValueOf(v))
|
result.Set(reflect.ValueOf(v).Convert(result.Type()))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
func TestDecode_intStringAliased(t *testing.T) {
|
||||||
var value struct {
|
var value struct {
|
||||||
Count time.Duration
|
Count time.Duration
|
||||||
|
Loading…
Reference in New Issue
Block a user