fix a crash if a non-pointer is given to Decode
/cc @sethvargo
This commit is contained in:
parent
cd87a48c3c
commit
09d7815762
@ -1,6 +1,7 @@
|
|||||||
package hcl
|
package hcl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
@ -27,8 +28,13 @@ func Decode(out interface{}, in string) error {
|
|||||||
// DecodeObject is a lower-level version of Decode. It decodes a
|
// DecodeObject is a lower-level version of Decode. It decodes a
|
||||||
// raw Object into the given output.
|
// raw Object into the given output.
|
||||||
func DecodeObject(out interface{}, n *hcl.Object) error {
|
func DecodeObject(out interface{}, n *hcl.Object) error {
|
||||||
|
val := reflect.ValueOf(out)
|
||||||
|
if val.Kind() != reflect.Ptr {
|
||||||
|
return errors.New("result must be a pointer")
|
||||||
|
}
|
||||||
|
|
||||||
var d decoder
|
var d decoder
|
||||||
return d.decode("root", n, reflect.ValueOf(out).Elem())
|
return d.decode("root", n, val.Elem())
|
||||||
}
|
}
|
||||||
|
|
||||||
type decoder struct {
|
type decoder struct {
|
||||||
|
@ -425,6 +425,14 @@ func TestDecode_structureMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecode_interfaceNonPointer(t *testing.T) {
|
||||||
|
var value interface{}
|
||||||
|
err := Decode(value, testReadFile(t, "basic_int_string.hcl"))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("should error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDecode_intString(t *testing.T) {
|
func TestDecode_intString(t *testing.T) {
|
||||||
var value struct {
|
var value struct {
|
||||||
Count int
|
Count int
|
||||||
|
Loading…
Reference in New Issue
Block a user