Decode into non-nil pointer's existing value.
Slight adaptation of Ian Remmler <ian@remmler.org>'s fix in pr #39: > If decoding into a pointer, and the pointer points to a value, decode > into the exitsing value. Otherwise, decode into a new value and point > to it. > Addresses issue #38.
This commit is contained in:
parent
f5f70d661b
commit
c22487c32b
@ -404,6 +404,11 @@ func (d *decoder) decodeMap(name string, node ast.Node, result reflect.Value) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *decoder) decodePtr(name string, node ast.Node, result reflect.Value) error {
|
func (d *decoder) decodePtr(name string, node ast.Node, result reflect.Value) error {
|
||||||
|
// if pointer is not nil, decode into existing value
|
||||||
|
if !result.IsNil() {
|
||||||
|
return d.decode(name, node, result.Elem())
|
||||||
|
}
|
||||||
|
|
||||||
// Create an element of the concrete (non pointer) type and decode
|
// Create an element of the concrete (non pointer) type and decode
|
||||||
// into that. Then set the value of the pointer to this type.
|
// into that. Then set the value of the pointer to this type.
|
||||||
resultType := result.Type()
|
resultType := result.Type()
|
||||||
|
@ -616,6 +616,35 @@ func TestDecode_structurePtr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecode_nonNilStructurePtr(t *testing.T) {
|
||||||
|
type V struct {
|
||||||
|
Key int
|
||||||
|
Foo string
|
||||||
|
DontChange string
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := &V{
|
||||||
|
Key: 42,
|
||||||
|
Foo: "foo",
|
||||||
|
DontChange: "don't change me",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Decode(&actual, testReadFile(t, "flat.hcl"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := &V{
|
||||||
|
Key: 7,
|
||||||
|
Foo: "bar",
|
||||||
|
DontChange: "don't change me",
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Fatalf("Actual: %#v\n\nExpected: %#v", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDecode_structureArray(t *testing.T) {
|
func TestDecode_structureArray(t *testing.T) {
|
||||||
// This test is extracted from a failure in Consul (consul.io),
|
// This test is extracted from a failure in Consul (consul.io),
|
||||||
// hence the interesting structure naming.
|
// hence the interesting structure naming.
|
||||||
|
Loading…
Reference in New Issue
Block a user