]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: fix decoding of attributes in to pointer fields.
authorKamil Kisiel <kamil@kamilkisiel.net>
Fri, 18 Jan 2013 22:07:34 +0000 (17:07 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 18 Jan 2013 22:07:34 +0000 (17:07 -0500)
Fixes #3719.

R=anacrolix, rsc
CC=golang-dev
https://golang.org/cl/7131052

src/pkg/encoding/xml/read.go
src/pkg/encoding/xml/read_test.go

index 7a06a29b95af94b2e4af4a6cb57fbeb247a32d31..6bc23e1226668e305426bf58f843fb16b5d5ddb5 100644 (file)
@@ -394,6 +394,13 @@ func copyValue(dst reflect.Value, src []byte) (err error) {
                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 8df09b3cceea01a9e7a34452a5ed4328bee4dd47..b45e2f0e61e3c87f8dc01c8a500ab6b786c8e7b5 100644 (file)
@@ -355,3 +355,47 @@ func TestUnmarshalWithoutNameType(t *testing.T) {
                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)
+       }
+}