]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: allow embedded non-structs
authorRuss Cox <rsc@golang.org>
Tue, 12 Mar 2013 03:58:20 +0000 (23:58 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 12 Mar 2013 03:58:20 +0000 (23:58 -0400)
The old code just assumed that the only thing
you can embed is a struct. Not true.

Fixes #3803.

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/7743043

src/pkg/encoding/xml/marshal_test.go
src/pkg/encoding/xml/typeinfo.go

index 3a190def6c1c21e38a4121f9a5bb6449bf887200..1373e01d89941e7658e957351ae29cc6c5f7c28f 100644 (file)
@@ -266,6 +266,12 @@ type Plain struct {
        V interface{}
 }
 
+type MyInt int
+
+type EmbedInt struct {
+       MyInt
+}
+
 // Unless explicitly stated as such (or *Plain), all of the
 // tests below are two-way tests. When introducing new tests,
 // please try to make them two-way as well to ensure that
@@ -790,6 +796,12 @@ var marshalTests = []struct {
                },
                UnmarshalOnly: true,
        },
+       {
+               ExpectXML: `<EmbedInt><MyInt>42</MyInt></EmbedInt>`,
+               Value: &EmbedInt{
+                       MyInt: 42,
+               },
+       },
 }
 
 func TestMarshal(t *testing.T) {
index bbeb28d87eaa35239687fa8a170332606614b519..f9c559c04d3b998d617ad496f67df5f33f5fc086 100644 (file)
@@ -70,20 +70,19 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
                                if t.Kind() == reflect.Ptr {
                                        t = t.Elem()
                                }
-                               if t.Kind() != reflect.Struct {
-                                       continue
-                               }
-                               inner, err := getTypeInfo(t)
-                               if err != nil {
-                                       return nil, err
-                               }
-                               for _, finfo := range inner.fields {
-                                       finfo.idx = append([]int{i}, finfo.idx...)
-                                       if err := addFieldInfo(typ, tinfo, &finfo); err != nil {
+                               if t.Kind() == reflect.Struct {
+                                       inner, err := getTypeInfo(t)
+                                       if err != nil {
                                                return nil, err
                                        }
+                                       for _, finfo := range inner.fields {
+                                               finfo.idx = append([]int{i}, finfo.idx...)
+                                               if err := addFieldInfo(typ, tinfo, &finfo); err != nil {
+                                                       return nil, err
+                                               }
+                                       }
+                                       continue
                                }
-                               continue
                        }
 
                        finfo, err := structFieldInfo(typ, &f)