FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com,

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)
+ }
+}

Search Discussions

  • Kamil Kisiel at Jan 17, 2013 at 11:16 pm
    Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com),

    Please take another look.


    https://codereview.appspot.com/7131052/
  • Anacrolix at Jan 17, 2013 at 11:16 pm

    On 2013/01/17 22:03:44, kamil.kisiel wrote:
    Realized I wasn't actually subscribed to golang-dev so my mails were not going
    through. Trying again.
    Thanks for this!

    https://codereview.appspot.com/7131052/
  • Kamil Kisiel at Jan 17, 2013 at 11:22 pm
    Realized I wasn't actually subscribed to golang-dev so my mails were not
    going through. Trying again.

    https://codereview.appspot.com/7131052/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedJan 17, '13 at 11:29a
activeJan 17, '13 at 11:22p
posts4
users2
websitegolang.org

2 users in discussion

Kamil Kisiel: 3 posts Anacrolix: 1 post

People

Translate

site design / logo © 2022 Grokbase