Commit 4730a226 authored by Kamil Kisiel's avatar Kamil Kisiel Committed by Russ Cox

encoding/xml: fix decoding of attributes in to pointer fields.

Fixes #3719.

R=anacrolix, rsc
CC=golang-dev
https://golang.org/cl/7131052
parent d3679726
...@@ -394,6 +394,13 @@ func copyValue(dst reflect.Value, src []byte) (err error) { ...@@ -394,6 +394,13 @@ func copyValue(dst reflect.Value, src []byte) (err error) {
return err == nil return err == nil
} }
if pv := dst; pv.Kind() == reflect.Ptr {
if pv.IsNil() {
pv.Set(reflect.New(pv.Type().Elem()))
}
dst = pv.Elem()
}
// Save accumulated data. // Save accumulated data.
switch t := dst; t.Kind() { switch t := dst; t.Kind() {
case reflect.Invalid: case reflect.Invalid:
......
...@@ -355,3 +355,47 @@ func TestUnmarshalWithoutNameType(t *testing.T) { ...@@ -355,3 +355,47 @@ func TestUnmarshalWithoutNameType(t *testing.T) {
t.Fatalf("have %v\nwant %v", x.Attr, OK) t.Fatalf("have %v\nwant %v", x.Attr, OK)
} }
} }
func TestUnmarshalAttr(t *testing.T) {
type ParamVal struct {
Int int `xml:"int,attr"`
}
type ParamPtr struct {
Int *int `xml:"int,attr"`
}
type ParamStringPtr struct {
Int *string `xml:"int,attr"`
}
x := []byte(`<Param int="1" />`)
p1 := &ParamPtr{}
if err := Unmarshal(x, p1); err != nil {
t.Fatalf("Unmarshal: %s", err)
}
if p1.Int == nil {
t.Fatalf("Unmarshal failed in to *int field")
} else if *p1.Int != 1 {
t.Fatalf("Unmarshal with %s failed:\nhave %#v,\n want %#v", x, p1.Int, 1)
}
p2 := &ParamVal{}
if err := Unmarshal(x, p2); err != nil {
t.Fatalf("Unmarshal: %s", err)
}
if p2.Int != 1 {
t.Fatalf("Unmarshal with %s failed:\nhave %#v,\n want %#v", x, p2.Int, 1)
}
p3 := &ParamStringPtr{}
if err := Unmarshal(x, p3); err != nil {
t.Fatalf("Unmarshal: %s", err)
}
if p3.Int == nil {
t.Fatalf("Unmarshal failed in to *string field")
} else if *p3.Int != "1" {
t.Fatalf("Unmarshal with %s failed:\nhave %#v,\n want %#v", x, p3.Int, 1)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment