Reviewers: golang-dev_googlegroups.com,
Message:
Hello [email protected],
I'd like you to review this change to
https://code.google.com/p/go
Description:
encoding/xml: fix decoding of attributes in to pointer fields.
Fixes issue 4668.
Please review this at https://codereview.appspot.com/7131052/
Affected files:
M src/pkg/encoding/xml/read.go
M src/pkg/encoding/xml/read_test.go
Index: src/pkg/encoding/xml/read.go
===================================================================
--- a/src/pkg/encoding/xml/read.go
+++ b/src/pkg/encoding/xml/read.go
@@ -394,6 +394,13 @@
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.
switch t := dst; t.Kind() {
case reflect.Invalid:
Index: src/pkg/encoding/xml/read_test.go
===================================================================
--- a/src/pkg/encoding/xml/read_test.go
+++ b/src/pkg/encoding/xml/read_test.go
@@ -355,3 +355,33 @@
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"`
+ }
+
+ 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)
+ }
+}