]> Cypherpunks repositories - gostls13.git/commitdiff
xml: Support fields not of type []byte when marshaling ",chardata"
authorVega Garcia Luis Alfonso <vegacom@gmail.com>
Wed, 23 Jan 2013 03:13:40 +0000 (22:13 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 23 Jan 2013 03:13:40 +0000 (22:13 -0500)
Fixes #4506.

R=rsc, remyoudompheng
CC=golang-dev
https://golang.org/cl/7106045

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

index 8b2f4173f3a2b1b9885fe50d3d72a0e33e8481a5..383fb26b04396c8e0e2036e5e41e3cbe3c6b57bd 100644 (file)
@@ -279,13 +279,26 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
                vf := finfo.value(val)
                switch finfo.flags & fMode {
                case fCharData:
+                       var scratch [64]byte
                        switch vf.Kind() {
+                       case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+                               Escape(p, strconv.AppendInt(scratch[:0], vf.Int(), 10))
+                       case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+                               Escape(p, strconv.AppendUint(scratch[:0], vf.Uint(), 10))
+                       case reflect.Float32, reflect.Float64:
+                               Escape(p, strconv.AppendFloat(scratch[:0], vf.Float(), 'g', -1, vf.Type().Bits()))
+                       case reflect.Bool:
+                               Escape(p, strconv.AppendBool(scratch[:0], vf.Bool()))
                        case reflect.String:
                                Escape(p, []byte(vf.String()))
                        case reflect.Slice:
                                if elem, ok := vf.Interface().([]byte); ok {
                                        Escape(p, elem)
                                }
+                       case reflect.Struct:
+                               if vf.Type() == timeType {
+                                       Escape(p, []byte(vf.Interface().(time.Time).Format(time.RFC3339Nano)))
+                               }
                        }
                        continue
 
index 2ce7721abd106af09b367fb0464fe9acb7a90fc9..67fcfd9ed565b6534247ae191565045bc2fb7473 100644 (file)
@@ -59,6 +59,36 @@ type Book struct {
        Title   string   `xml:",chardata"`
 }
 
+type Event struct {
+       XMLName struct{} `xml:"event"`
+       Year    int      `xml:",chardata"`
+}
+
+type Movie struct {
+       XMLName struct{} `xml:"movie"`
+       Length  uint     `xml:",chardata"`
+}
+
+type Pi struct {
+       XMLName       struct{} `xml:"pi"`
+       Approximation float32  `xml:",chardata"`
+}
+
+type Universe struct {
+       XMLName struct{} `xml:"universe"`
+       Visible float64  `xml:",chardata"`
+}
+
+type Particle struct {
+       XMLName struct{} `xml:"particle"`
+       HasMass bool     `xml:",chardata"`
+}
+
+type Departure struct {
+       XMLName struct{}  `xml:"departure"`
+       When    time.Time `xml:",chardata"`
+}
+
 type SecretAgent struct {
        XMLName   struct{} `xml:"agent"`
        Handle    string   `xml:"handle,attr"`
@@ -345,6 +375,12 @@ var marshalTests = []struct {
        {Value: &Domain{Name: []byte("google.com&friends")}, ExpectXML: `<domain>google.com&amp;friends</domain>`},
        {Value: &Domain{Name: []byte("google.com"), Comment: []byte(" &friends ")}, ExpectXML: `<domain>google.com<!-- &friends --></domain>`},
        {Value: &Book{Title: "Pride & Prejudice"}, ExpectXML: `<book>Pride &amp; Prejudice</book>`},
+       {Value: &Event{Year: -3114}, ExpectXML: `<event>-3114</event>`},
+       {Value: &Movie{Length: 13440}, ExpectXML: `<movie>13440</movie>`},
+       {Value: &Pi{Approximation: 3.14159265}, ExpectXML: `<pi>3.1415927</pi>`},
+       {Value: &Universe{Visible: 9.3e13}, ExpectXML: `<universe>9.3e+13</universe>`},
+       {Value: &Particle{HasMass: true}, ExpectXML: `<particle>true</particle>`},
+       {Value: &Departure{When: ParseTime("2013-01-09T00:15:00-09:00")}, ExpectXML: `<departure>2013-01-09T00:15:00-09:00</departure>`},
        {Value: atomValue, ExpectXML: atomXml},
        {
                Value: &Ship{