xml: Support fields not of type []byte when marshaling ",chardata"

Fixes #4506.

R=rsc, remyoudompheng
CC=golang-dev
https://golang.org/cl/7106045
This commit is contained in:
Vega Garcia Luis Alfonso 2013-01-22 22:13:40 -05:00 committed by Russ Cox
parent 92e3e15546
commit 14bd52db3f
2 changed files with 49 additions and 0 deletions

View 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