2011-06-27 19:07:28 -04:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package xml
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
2011-06-27 19:07:28 -04:00
|
|
|
"io"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2012-02-07 23:37:25 -05:00
|
|
|
"time"
|
2011-06-27 19:07:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2012-01-23 01:32:07 -02:00
|
|
|
// A generic XML header suitable for use with the output of Marshal.
|
|
|
|
|
// This is not automatically added to any output of this package,
|
|
|
|
|
// it is provided as a convenience.
|
2011-07-29 14:09:07 -04:00
|
|
|
Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
|
2011-06-27 19:07:28 -04:00
|
|
|
)
|
|
|
|
|
|
2012-01-24 01:10:32 -02:00
|
|
|
// Marshal returns the XML encoding of v.
|
2011-06-27 19:07:28 -04:00
|
|
|
//
|
|
|
|
|
// Marshal handles an array or slice by marshalling each of the elements.
|
|
|
|
|
// Marshal handles a pointer by marshalling the value it points at or, if the
|
|
|
|
|
// pointer is nil, by writing nothing. Marshal handles an interface value by
|
|
|
|
|
// marshalling the value it contains or, if the interface value is nil, by
|
2011-08-26 12:29:52 -03:00
|
|
|
// writing nothing. Marshal handles all other data by writing one or more XML
|
|
|
|
|
// elements containing the data.
|
2011-06-27 19:07:28 -04:00
|
|
|
//
|
2011-08-26 12:29:52 -03:00
|
|
|
// The name for the XML elements is taken from, in order of preference:
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
// - the tag on the XMLName field, if the data is a struct
|
|
|
|
|
// - the value of the XMLName field of type xml.Name
|
2011-06-27 19:07:28 -04:00
|
|
|
// - the tag of the struct field used to obtain the data
|
|
|
|
|
// - the name of the struct field used to obtain the data
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
// - the name of the marshalled type
|
2011-06-27 19:07:28 -04:00
|
|
|
//
|
|
|
|
|
// The XML element for a struct contains marshalled elements for each of the
|
|
|
|
|
// exported fields of the struct, with these exceptions:
|
|
|
|
|
// - the XMLName field, described above, is omitted.
|
2012-01-24 21:04:40 -02:00
|
|
|
// - a field with tag "-" is omitted.
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
// - a field with tag "name,attr" becomes an attribute with
|
|
|
|
|
// the given name in the XML element.
|
|
|
|
|
// - a field with tag ",attr" becomes an attribute with the
|
|
|
|
|
// field name in the in the XML element.
|
|
|
|
|
// - a field with tag ",chardata" is written as character data,
|
|
|
|
|
// not as an XML element.
|
|
|
|
|
// - a field with tag ",innerxml" is written verbatim, not subject
|
|
|
|
|
// to the usual marshalling procedure.
|
|
|
|
|
// - a field with tag ",comment" is written as an XML comment, not
|
|
|
|
|
// subject to the usual marshalling procedure. It must not contain
|
|
|
|
|
// the "--" string within it.
|
2012-02-08 01:57:44 -02:00
|
|
|
// - a field with a tag including the "omitempty" option is omitted
|
|
|
|
|
// if the field value is empty. The empty values are false, 0, any
|
|
|
|
|
// nil pointer or interface value, and any array, slice, map, or
|
|
|
|
|
// string of length zero.
|
2012-02-23 01:35:50 -02:00
|
|
|
// - a non-pointer anonymous struct field is handled as if the
|
|
|
|
|
// fields of its value were part of the outer struct.
|
2011-06-27 19:07:28 -04:00
|
|
|
//
|
2011-08-26 12:29:52 -03:00
|
|
|
// If a field uses a tag "a>b>c", then the element c will be nested inside
|
|
|
|
|
// parent elements a and b. Fields that appear next to each other that name
|
2012-02-16 02:01:46 -02:00
|
|
|
// the same parent will be enclosed in one XML element.
|
2011-08-26 12:29:52 -03:00
|
|
|
//
|
2012-02-16 02:01:46 -02:00
|
|
|
// See MarshalIndent for an example.
|
2011-08-26 12:29:52 -03:00
|
|
|
//
|
2011-06-27 19:07:28 -04:00
|
|
|
// Marshal will return an error if asked to marshal a channel, function, or map.
|
2012-01-24 01:10:32 -02:00
|
|
|
func Marshal(v interface{}) ([]byte, error) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
if err := NewEncoder(&b).Encode(v); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-16 02:01:46 -02:00
|
|
|
// MarshalIndent works like Marshal, but each XML element begins on a new
|
|
|
|
|
// indented line that starts with prefix and is followed by one or more
|
|
|
|
|
// copies of indent according to the nesting depth.
|
|
|
|
|
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
enc := NewEncoder(&b)
|
|
|
|
|
enc.prefix = prefix
|
|
|
|
|
enc.indent = indent
|
|
|
|
|
err := enc.marshalValue(reflect.ValueOf(v), nil)
|
|
|
|
|
enc.Flush()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-24 01:10:32 -02:00
|
|
|
// An Encoder writes XML data to an output stream.
|
|
|
|
|
type Encoder struct {
|
|
|
|
|
printer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewEncoder returns a new encoder that writes to w.
|
|
|
|
|
func NewEncoder(w io.Writer) *Encoder {
|
2012-02-16 02:01:46 -02:00
|
|
|
return &Encoder{printer{Writer: bufio.NewWriter(w)}}
|
2012-01-24 01:10:32 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Encode writes the XML encoding of v to the stream.
|
|
|
|
|
//
|
|
|
|
|
// See the documentation for Marshal for details about the conversion
|
|
|
|
|
// of Go values to XML.
|
|
|
|
|
func (enc *Encoder) Encode(v interface{}) error {
|
|
|
|
|
err := enc.marshalValue(reflect.ValueOf(v), nil)
|
|
|
|
|
enc.Flush()
|
2011-06-27 19:07:28 -04:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-24 01:10:32 -02:00
|
|
|
type printer struct {
|
|
|
|
|
*bufio.Writer
|
2012-02-16 02:01:46 -02:00
|
|
|
indent string
|
|
|
|
|
prefix string
|
|
|
|
|
depth int
|
|
|
|
|
indentedIn bool
|
2012-01-24 01:10:32 -02:00
|
|
|
}
|
|
|
|
|
|
2012-02-16 02:01:46 -02:00
|
|
|
// marshalValue writes one or more XML elements representing val.
|
|
|
|
|
// If val was obtained from a struct field, finfo must have its details.
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo) error {
|
2011-06-27 19:07:28 -04:00
|
|
|
if !val.IsValid() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2012-02-08 01:57:44 -02:00
|
|
|
if finfo != nil && finfo.flags&fOmitEmpty != 0 && isEmptyValue(val) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2011-06-27 19:07:28 -04:00
|
|
|
|
|
|
|
|
kind := val.Kind()
|
|
|
|
|
typ := val.Type()
|
|
|
|
|
|
|
|
|
|
// Drill into pointers/interfaces
|
|
|
|
|
if kind == reflect.Ptr || kind == reflect.Interface {
|
|
|
|
|
if val.IsNil() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
return p.marshalValue(val.Elem(), finfo)
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Slices and arrays iterate over the elements. They do not have an enclosing tag.
|
|
|
|
|
if (kind == reflect.Slice || kind == reflect.Array) && typ.Elem().Kind() != reflect.Uint8 {
|
|
|
|
|
for i, n := 0, val.Len(); i < n; i++ {
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
if err := p.marshalValue(val.Index(i), finfo); err != nil {
|
2011-06-27 19:07:28 -04:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
tinfo, err := getTypeInfo(typ)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Precedence for the XML element name is:
|
|
|
|
|
// 1. XMLName field in underlying struct;
|
|
|
|
|
// 2. field name/tag in the struct field; and
|
|
|
|
|
// 3. type name
|
|
|
|
|
var xmlns, name string
|
|
|
|
|
if tinfo.xmlname != nil {
|
|
|
|
|
xmlname := tinfo.xmlname
|
|
|
|
|
if xmlname.name != "" {
|
|
|
|
|
xmlns, name = xmlname.xmlns, xmlname.name
|
|
|
|
|
} else if v, ok := val.FieldByIndex(xmlname.idx).Interface().(Name); ok && v.Local != "" {
|
|
|
|
|
xmlns, name = v.Space, v.Local
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if name == "" && finfo != nil {
|
|
|
|
|
xmlns, name = finfo.xmlns, finfo.name
|
|
|
|
|
}
|
|
|
|
|
if name == "" {
|
|
|
|
|
name = typ.Name()
|
|
|
|
|
if name == "" {
|
|
|
|
|
return &UnsupportedTypeError{typ}
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-16 02:01:46 -02:00
|
|
|
p.writeIndent(1)
|
2011-06-27 19:07:28 -04:00
|
|
|
p.WriteByte('<')
|
|
|
|
|
p.WriteString(name)
|
|
|
|
|
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
if xmlns != "" {
|
|
|
|
|
p.WriteString(` xmlns="`)
|
|
|
|
|
// TODO: EscapeString, to avoid the allocation.
|
|
|
|
|
Escape(p, []byte(xmlns))
|
|
|
|
|
p.WriteByte('"')
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-27 19:07:28 -04:00
|
|
|
// Attributes
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
for i := range tinfo.fields {
|
|
|
|
|
finfo := &tinfo.fields[i]
|
|
|
|
|
if finfo.flags&fAttr == 0 {
|
|
|
|
|
continue
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
2012-01-23 00:50:05 -02:00
|
|
|
fv := val.FieldByIndex(finfo.idx)
|
2012-02-08 01:57:44 -02:00
|
|
|
if finfo.flags&fOmitEmpty != 0 && isEmptyValue(fv) {
|
|
|
|
|
continue
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
}
|
2012-01-23 00:50:05 -02:00
|
|
|
p.WriteByte(' ')
|
|
|
|
|
p.WriteString(finfo.name)
|
|
|
|
|
p.WriteString(`="`)
|
|
|
|
|
if err := p.marshalSimple(fv.Type(), fv); err != nil {
|
|
|
|
|
return err
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
2012-01-23 00:50:05 -02:00
|
|
|
p.WriteByte('"')
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
|
|
|
|
p.WriteByte('>')
|
|
|
|
|
|
2012-01-23 00:50:05 -02:00
|
|
|
if val.Kind() == reflect.Struct {
|
|
|
|
|
err = p.marshalStruct(tinfo, val)
|
|
|
|
|
} else {
|
|
|
|
|
err = p.marshalSimple(typ, val)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-16 02:01:46 -02:00
|
|
|
p.writeIndent(-1)
|
2012-01-23 00:50:05 -02:00
|
|
|
p.WriteByte('<')
|
|
|
|
|
p.WriteByte('/')
|
|
|
|
|
p.WriteString(name)
|
|
|
|
|
p.WriteByte('>')
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-07 23:37:25 -05:00
|
|
|
var timeType = reflect.TypeOf(time.Time{})
|
|
|
|
|
|
2012-01-23 00:50:05 -02:00
|
|
|
func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) error {
|
2012-02-07 23:37:25 -05:00
|
|
|
// Normally we don't see structs, but this can happen for an attribute.
|
|
|
|
|
if val.Type() == timeType {
|
|
|
|
|
p.WriteString(val.Interface().(time.Time).Format(time.RFC3339Nano))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2012-01-23 00:50:05 -02:00
|
|
|
switch val.Kind() {
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2011-12-05 15:48:46 -05:00
|
|
|
p.WriteString(strconv.FormatInt(val.Int(), 10))
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2011-12-05 15:48:46 -05:00
|
|
|
p.WriteString(strconv.FormatUint(val.Uint(), 10))
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2011-12-05 15:48:46 -05:00
|
|
|
p.WriteString(strconv.FormatFloat(val.Float(), 'g', -1, 64))
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.String:
|
2012-01-23 00:50:05 -02:00
|
|
|
// TODO: Add EscapeString.
|
2011-06-27 19:07:28 -04:00
|
|
|
Escape(p, []byte(val.String()))
|
|
|
|
|
case reflect.Bool:
|
2011-12-05 15:48:46 -05:00
|
|
|
p.WriteString(strconv.FormatBool(val.Bool()))
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Array:
|
|
|
|
|
// will be [...]byte
|
|
|
|
|
bytes := make([]byte, val.Len())
|
|
|
|
|
for i := range bytes {
|
|
|
|
|
bytes[i] = val.Index(i).Interface().(byte)
|
|
|
|
|
}
|
|
|
|
|
Escape(p, bytes)
|
|
|
|
|
case reflect.Slice:
|
|
|
|
|
// will be []byte
|
2012-01-23 00:50:05 -02:00
|
|
|
Escape(p, val.Bytes())
|
2011-06-27 19:07:28 -04:00
|
|
|
default:
|
|
|
|
|
return &UnsupportedTypeError{typ}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
var ddBytes = []byte("--")
|
|
|
|
|
|
|
|
|
|
func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
|
2012-02-07 23:37:25 -05:00
|
|
|
if val.Type() == timeType {
|
|
|
|
|
p.WriteString(val.Interface().(time.Time).Format(time.RFC3339Nano))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
s := parentStack{printer: p}
|
|
|
|
|
for i := range tinfo.fields {
|
|
|
|
|
finfo := &tinfo.fields[i]
|
|
|
|
|
if finfo.flags&(fAttr|fAny) != 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
vf := val.FieldByIndex(finfo.idx)
|
|
|
|
|
switch finfo.flags & fMode {
|
|
|
|
|
case fCharData:
|
|
|
|
|
switch vf.Kind() {
|
|
|
|
|
case reflect.String:
|
|
|
|
|
Escape(p, []byte(vf.String()))
|
|
|
|
|
case reflect.Slice:
|
|
|
|
|
if elem, ok := vf.Interface().([]byte); ok {
|
|
|
|
|
Escape(p, elem)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
case fComment:
|
|
|
|
|
k := vf.Kind()
|
|
|
|
|
if !(k == reflect.String || k == reflect.Slice && vf.Type().Elem().Kind() == reflect.Uint8) {
|
|
|
|
|
return fmt.Errorf("xml: bad type for comment field of %s", val.Type())
|
|
|
|
|
}
|
|
|
|
|
if vf.Len() == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2012-02-16 02:01:46 -02:00
|
|
|
p.writeIndent(0)
|
xml: major Go 1 fixup
This CL improves the xml package in the following ways:
- makes its interface match established conventions
- brings Marshal and Unmarshal closer together
- fixes a large number of bugs and adds tests
- improves speed significantly
- organizes and simplifies the code
Fixes #2426.
Fixes #2406.
Fixes #1989.
What follows is a detailed list of those changes.
- All matching is case sensitive without special processing
to the field name or xml tag in an attempt to match them.
Customize the field tag as desired to match the correct XML
elements.
- Flags are ",flag" rather than "flag". The names "attr",
"chardata", etc, may be used to name actual XML elements.
- Overriding of attribute names is possible with "name,attr".
- Attribute fields are marshalled properly if they have
non-string types. Previously they were unmarshalled, but were
ignored at marshalling time.
- Comment fields tagged with ",comment" are marshalled properly,
rather than being marshalled as normal fields.
- The handling of the Any field has been replaced by the ",any"
flag to avoid unexpected results when using the field name for
other purposes, and has also been fixed to interact properly
with name paths. Previously the feature would not function
if any field in the type had a name path in its tag.
- Embedded struct support fixed and cleaned so it works when
marshalling and also when using field paths deeper than one level.
- Conflict reporting on field names have been expanded to cover
all fields. Previously it'd catch only conflicts of paths
deeper than one level. Also interacts correctly with embedded
structs now.
- A trailing '>' is disallowed in xml tags. It used to be
supported for removing the ambiguity between "attr" and "attr>",
but the marshalling support for that was broken, and it's now
unnecessary. Use "name" instead of "name>".
- Fixed docs to point out that a XMLName doesn't have to be
an xml.Name (e.g. a struct{} is a good fit too). The code was
already working like that.
- Fixed asymmetry in the precedence of XML element names between
marshalling and unmarshalling. Marshal would consider the XMLName
of the field type before the field tag, while unmarshalling would
do the opposite. Now both respect the tag of the XMLName field
first, and a nice error message is provided in case an attempt
is made to name a field with its tag in a way that would
conflict with the underlying type's XMLName field.
- Do not marshal broken "<???>" tags when in doubt. Use the type
name, and error out if that's not possible.
- Do not break down unmarshalling if there's an interface{} field
in a struct.
- Significant speed boost due to caching of type metadata and
overall allocation clean ups. The following timings reflect
processing of the the atom test data:
Old:
BenchmarkMarshal 50000 48798 ns/op
BenchmarkUnmarshal 5000 357174 ns/op
New:
BenchmarkMarshal 100000 19799 ns/op
BenchmarkUnmarshal 10000 128525 ns/op
R=cw, gustavo, kevlar, adg, rogpeppe, fullung, christoph, rsc
CC=golang-dev
https://golang.org/cl/5503078
2012-01-13 11:05:19 +01:00
|
|
|
p.WriteString("<!--")
|
|
|
|
|
dashDash := false
|
|
|
|
|
dashLast := false
|
|
|
|
|
switch k {
|
|
|
|
|
case reflect.String:
|
|
|
|
|
s := vf.String()
|
|
|
|
|
dashDash = strings.Index(s, "--") >= 0
|
|
|
|
|
dashLast = s[len(s)-1] == '-'
|
|
|
|
|
if !dashDash {
|
|
|
|
|
p.WriteString(s)
|
|
|
|
|
}
|
|
|
|
|
case reflect.Slice:
|
|
|
|
|
b := vf.Bytes()
|
|
|
|
|
dashDash = bytes.Index(b, ddBytes) >= 0
|
|
|
|
|
dashLast = b[len(b)-1] == '-'
|
|
|
|
|
if !dashDash {
|
|
|
|
|
p.Write(b)
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
panic("can't happen")
|
|
|
|
|
}
|
|
|
|
|
if dashDash {
|
|
|
|
|
return fmt.Errorf(`xml: comments must not contain "--"`)
|
|
|
|
|
}
|
|
|
|
|
if dashLast {
|
|
|
|
|
// "--->" is invalid grammar. Make it "- -->"
|
|
|
|
|
p.WriteByte(' ')
|
|
|
|
|
}
|
|
|
|
|
p.WriteString("-->")
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
case fInnerXml:
|
|
|
|
|
iface := vf.Interface()
|
|
|
|
|
switch raw := iface.(type) {
|
|
|
|
|
case []byte:
|
|
|
|
|
p.Write(raw)
|
|
|
|
|
continue
|
|
|
|
|
case string:
|
|
|
|
|
p.WriteString(raw)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case fElement:
|
|
|
|
|
s.trim(finfo.parents)
|
|
|
|
|
if len(finfo.parents) > len(s.stack) {
|
|
|
|
|
if vf.Kind() != reflect.Ptr && vf.Kind() != reflect.Interface || !vf.IsNil() {
|
|
|
|
|
s.push(finfo.parents[len(s.stack):])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err := p.marshalValue(vf, finfo); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s.trim(nil)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-16 02:01:46 -02:00
|
|
|
func (p *printer) writeIndent(depthDelta int) {
|
|
|
|
|
if len(p.prefix) == 0 && len(p.indent) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if depthDelta < 0 {
|
|
|
|
|
p.depth--
|
|
|
|
|
if p.indentedIn {
|
|
|
|
|
p.indentedIn = false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
p.indentedIn = false
|
|
|
|
|
}
|
|
|
|
|
p.WriteByte('\n')
|
|
|
|
|
if len(p.prefix) > 0 {
|
|
|
|
|
p.WriteString(p.prefix)
|
|
|
|
|
}
|
|
|
|
|
if len(p.indent) > 0 {
|
|
|
|
|
for i := 0; i < p.depth; i++ {
|
|
|
|
|
p.WriteString(p.indent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if depthDelta > 0 {
|
|
|
|
|
p.depth++
|
|
|
|
|
p.indentedIn = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-26 12:29:52 -03:00
|
|
|
type parentStack struct {
|
|
|
|
|
*printer
|
|
|
|
|
stack []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// trim updates the XML context to match the longest common prefix of the stack
|
|
|
|
|
// and the given parents. A closing tag will be written for every parent
|
|
|
|
|
// popped. Passing a zero slice or nil will close all the elements.
|
|
|
|
|
func (s *parentStack) trim(parents []string) {
|
|
|
|
|
split := 0
|
|
|
|
|
for ; split < len(parents) && split < len(s.stack); split++ {
|
|
|
|
|
if parents[split] != s.stack[split] {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for i := len(s.stack) - 1; i >= split; i-- {
|
2012-02-16 02:01:46 -02:00
|
|
|
s.writeIndent(-1)
|
2011-08-26 12:29:52 -03:00
|
|
|
s.WriteString("</")
|
|
|
|
|
s.WriteString(s.stack[i])
|
|
|
|
|
s.WriteByte('>')
|
|
|
|
|
}
|
|
|
|
|
s.stack = parents[:split]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// push adds parent elements to the stack and writes open tags.
|
|
|
|
|
func (s *parentStack) push(parents []string) {
|
|
|
|
|
for i := 0; i < len(parents); i++ {
|
2012-02-16 02:01:46 -02:00
|
|
|
s.writeIndent(1)
|
|
|
|
|
s.WriteByte('<')
|
2011-08-26 12:29:52 -03:00
|
|
|
s.WriteString(parents[i])
|
|
|
|
|
s.WriteByte('>')
|
|
|
|
|
}
|
|
|
|
|
s.stack = append(s.stack, parents...)
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 01:32:07 -02:00
|
|
|
// A MarshalXMLError is returned when Marshal encounters a type
|
2011-06-27 19:07:28 -04:00
|
|
|
// that cannot be converted into XML.
|
|
|
|
|
type UnsupportedTypeError struct {
|
|
|
|
|
Type reflect.Type
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 22:05:34 -04:00
|
|
|
func (e *UnsupportedTypeError) Error() string {
|
2011-06-27 19:07:28 -04:00
|
|
|
return "xml: unsupported type: " + e.Type.String()
|
|
|
|
|
}
|
2012-02-08 01:57:44 -02:00
|
|
|
|
|
|
|
|
func isEmptyValue(v reflect.Value) bool {
|
|
|
|
|
switch v.Kind() {
|
|
|
|
|
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
|
|
|
|
return v.Len() == 0
|
|
|
|
|
case reflect.Bool:
|
|
|
|
|
return !v.Bool()
|
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
|
return v.Int() == 0
|
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
|
return v.Uint() == 0
|
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
|
return v.Float() == 0
|
|
|
|
|
case reflect.Interface, reflect.Ptr:
|
|
|
|
|
return v.IsNil()
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|