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"
|
2013-08-14 18:52:09 -04:00
|
|
|
"encoding"
|
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
|
|
|
"fmt"
|
2011-06-27 19:07:28 -04:00
|
|
|
"io"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
2012-12-10 10:59:15 -05:00
|
|
|
// field name in the XML element.
|
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 ",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-05-16 23:21:31 -03:00
|
|
|
// - an 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
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
// Marshaler is the interface implemented by objects that can marshal
|
|
|
|
|
// themselves into valid XML elements.
|
|
|
|
|
//
|
|
|
|
|
// MarshalXML encodes the receiver as zero or more XML elements.
|
|
|
|
|
// By convention, arrays or slices are typically encoded as a sequence
|
|
|
|
|
// of elements, one per entry.
|
|
|
|
|
// Using start as the element tag is not required, but doing so
|
|
|
|
|
// will enable Unmarshal to match the XML elements to the correct
|
|
|
|
|
// struct field.
|
|
|
|
|
// One common implementation strategy is to construct a separate
|
|
|
|
|
// value with a layout corresponding to the desired XML and then
|
|
|
|
|
// to encode it using e.EncodeElement.
|
|
|
|
|
// Another common strategy is to use repeated calls to e.EncodeToken
|
|
|
|
|
// to generate the XML output one token at a time.
|
|
|
|
|
// The sequence of encoded tokens must make up zero or more valid
|
|
|
|
|
// XML elements.
|
|
|
|
|
type Marshaler interface {
|
|
|
|
|
MarshalXML(e *Encoder, start StartElement) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MarshalerAttr is the interface implemented by objects that can marshal
|
|
|
|
|
// themselves into valid XML attributes.
|
|
|
|
|
//
|
|
|
|
|
// MarshalXMLAttr returns an XML attribute with the encoded value of the receiver.
|
|
|
|
|
// Using name as the attribute name is not required, but doing so
|
|
|
|
|
// will enable Unmarshal to match the attribute to the correct
|
|
|
|
|
// struct field.
|
|
|
|
|
// If MarshalXMLAttr returns the zero attribute Attr{}, no attribute
|
|
|
|
|
// will be generated in the output.
|
|
|
|
|
// MarshalXMLAttr is used only for struct fields with the
|
|
|
|
|
// "attr" option in the field tag.
|
|
|
|
|
type MarshalerAttr interface {
|
|
|
|
|
MarshalXMLAttr(name Name) (Attr, error)
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2013-01-30 07:57:20 -08:00
|
|
|
enc.Indent(prefix, indent)
|
2012-06-25 16:00:35 -04:00
|
|
|
if err := enc.Encode(v); err != nil {
|
2012-02-16 02:01:46 -02:00
|
|
|
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 {
|
2013-08-14 14:58:28 -04:00
|
|
|
p printer
|
2012-01-24 01:10:32 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewEncoder returns a new encoder that writes to w.
|
|
|
|
|
func NewEncoder(w io.Writer) *Encoder {
|
2013-08-14 14:58:28 -04:00
|
|
|
e := &Encoder{printer{Writer: bufio.NewWriter(w)}}
|
|
|
|
|
e.p.encoder = e
|
|
|
|
|
return e
|
2012-01-24 01:10:32 -02:00
|
|
|
}
|
|
|
|
|
|
2013-01-30 07:57:20 -08:00
|
|
|
// Indent sets the encoder to generate XML in which each 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 (enc *Encoder) Indent(prefix, indent string) {
|
2013-08-14 14:58:28 -04:00
|
|
|
enc.p.prefix = prefix
|
|
|
|
|
enc.p.indent = indent
|
2013-01-30 07:57:20 -08:00
|
|
|
}
|
|
|
|
|
|
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 {
|
2013-08-14 14:58:28 -04:00
|
|
|
err := enc.p.marshalValue(reflect.ValueOf(v), nil, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return enc.p.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EncodeElement writes the XML encoding of v to the stream,
|
|
|
|
|
// using start as the outermost tag in the encoding.
|
|
|
|
|
//
|
|
|
|
|
// See the documentation for Marshal for details about the conversion
|
|
|
|
|
// of Go values to XML.
|
|
|
|
|
func (enc *Encoder) EncodeElement(v interface{}, start StartElement) error {
|
|
|
|
|
err := enc.p.marshalValue(reflect.ValueOf(v), nil, &start)
|
2012-06-25 16:00:35 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
return enc.p.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
endComment = []byte("-->")
|
|
|
|
|
endProcInst = []byte("?>")
|
|
|
|
|
endDirective = []byte(">")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EncodeToken writes the given XML token to the stream.
|
|
|
|
|
// It returns an error if StartElement and EndElement tokens are not properly matched.
|
|
|
|
|
func (enc *Encoder) EncodeToken(t Token) error {
|
|
|
|
|
p := &enc.p
|
|
|
|
|
switch t := t.(type) {
|
|
|
|
|
case StartElement:
|
|
|
|
|
if err := p.writeStart(&t); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
case EndElement:
|
|
|
|
|
if err := p.writeEnd(t.Name); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
case CharData:
|
|
|
|
|
EscapeText(p, t)
|
|
|
|
|
case Comment:
|
|
|
|
|
if bytes.Contains(t, endComment) {
|
|
|
|
|
return fmt.Errorf("xml: EncodeToken of Comment containing --> marker")
|
|
|
|
|
}
|
|
|
|
|
p.WriteString("<!--")
|
|
|
|
|
p.Write(t)
|
|
|
|
|
p.WriteString("-->")
|
2013-09-06 07:54:43 +10:00
|
|
|
return p.cachedWriteError()
|
2013-08-14 14:58:28 -04:00
|
|
|
case ProcInst:
|
|
|
|
|
if t.Target == "xml" || !isNameString(t.Target) {
|
|
|
|
|
return fmt.Errorf("xml: EncodeToken of ProcInst with invalid Target")
|
|
|
|
|
}
|
|
|
|
|
if bytes.Contains(t.Inst, endProcInst) {
|
|
|
|
|
return fmt.Errorf("xml: EncodeToken of ProcInst containing ?> marker")
|
|
|
|
|
}
|
|
|
|
|
p.WriteString("<?")
|
|
|
|
|
p.WriteString(t.Target)
|
|
|
|
|
if len(t.Inst) > 0 {
|
|
|
|
|
p.WriteByte(' ')
|
|
|
|
|
p.Write(t.Inst)
|
|
|
|
|
}
|
|
|
|
|
p.WriteString("?>")
|
|
|
|
|
case Directive:
|
|
|
|
|
if bytes.Contains(t, endDirective) {
|
|
|
|
|
return fmt.Errorf("xml: EncodeToken of Directive containing > marker")
|
|
|
|
|
}
|
|
|
|
|
p.WriteString("<!")
|
|
|
|
|
p.Write(t)
|
|
|
|
|
p.WriteString(">")
|
|
|
|
|
}
|
2013-09-06 07:54:43 +10:00
|
|
|
return p.cachedWriteError()
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
|
|
|
|
|
2012-01-24 01:10:32 -02:00
|
|
|
type printer struct {
|
|
|
|
|
*bufio.Writer
|
2013-08-14 14:58:28 -04:00
|
|
|
encoder *Encoder
|
2013-03-12 11:46:12 -04:00
|
|
|
seq int
|
2012-02-16 02:01:46 -02:00
|
|
|
indent string
|
|
|
|
|
prefix string
|
|
|
|
|
depth int
|
|
|
|
|
indentedIn bool
|
2013-02-03 11:21:07 -05:00
|
|
|
putNewline bool
|
2013-03-13 14:36:42 -04:00
|
|
|
attrNS map[string]string // map prefix -> name space
|
|
|
|
|
attrPrefix map[string]string // map name space -> prefix
|
2013-08-14 14:58:28 -04:00
|
|
|
prefixes []string
|
|
|
|
|
tags []Name
|
2013-03-13 14:36:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// createAttrPrefix finds the name space prefix attribute to use for the given name space,
|
2013-08-14 14:58:28 -04:00
|
|
|
// defining a new prefix if necessary. It returns the prefix.
|
|
|
|
|
func (p *printer) createAttrPrefix(url string) string {
|
|
|
|
|
if prefix := p.attrPrefix[url]; prefix != "" {
|
|
|
|
|
return prefix
|
2013-03-13 14:36:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The "http://www.w3.org/XML/1998/namespace" name space is predefined as "xml"
|
|
|
|
|
// and must be referred to that way.
|
|
|
|
|
// (The "http://www.w3.org/2000/xmlns/" name space is also predefined as "xmlns",
|
|
|
|
|
// but users should not be trying to use that one directly - that's our job.)
|
|
|
|
|
if url == xmlURL {
|
2013-08-14 14:58:28 -04:00
|
|
|
return "xml"
|
2013-03-13 14:36:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need to define a new name space.
|
|
|
|
|
if p.attrPrefix == nil {
|
|
|
|
|
p.attrPrefix = make(map[string]string)
|
|
|
|
|
p.attrNS = make(map[string]string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pick a name. We try to use the final element of the path
|
|
|
|
|
// but fall back to _.
|
2013-08-14 14:58:28 -04:00
|
|
|
prefix := strings.TrimRight(url, "/")
|
2013-03-13 14:36:42 -04:00
|
|
|
if i := strings.LastIndex(prefix, "/"); i >= 0 {
|
|
|
|
|
prefix = prefix[i+1:]
|
|
|
|
|
}
|
|
|
|
|
if prefix == "" || !isName([]byte(prefix)) || strings.Contains(prefix, ":") {
|
|
|
|
|
prefix = "_"
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(prefix, "xml") {
|
|
|
|
|
// xmlanything is reserved.
|
|
|
|
|
prefix = "_" + prefix
|
|
|
|
|
}
|
|
|
|
|
if p.attrNS[prefix] != "" {
|
|
|
|
|
// Name is taken. Find a better one.
|
|
|
|
|
for p.seq++; ; p.seq++ {
|
|
|
|
|
if id := prefix + "_" + strconv.Itoa(p.seq); p.attrNS[id] == "" {
|
|
|
|
|
prefix = id
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.attrPrefix[url] = prefix
|
|
|
|
|
p.attrNS[prefix] = url
|
|
|
|
|
|
|
|
|
|
p.WriteString(`xmlns:`)
|
|
|
|
|
p.WriteString(prefix)
|
|
|
|
|
p.WriteString(`="`)
|
|
|
|
|
EscapeText(p, []byte(url))
|
|
|
|
|
p.WriteString(`" `)
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
p.prefixes = append(p.prefixes, prefix)
|
|
|
|
|
|
|
|
|
|
return prefix
|
2013-03-13 14:36:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deleteAttrPrefix removes an attribute name space prefix.
|
|
|
|
|
func (p *printer) deleteAttrPrefix(prefix string) {
|
|
|
|
|
delete(p.attrPrefix, p.attrNS[prefix])
|
|
|
|
|
delete(p.attrNS, prefix)
|
2012-01-24 01:10:32 -02:00
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
func (p *printer) markPrefix() {
|
|
|
|
|
p.prefixes = append(p.prefixes, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *printer) popPrefix() {
|
|
|
|
|
for len(p.prefixes) > 0 {
|
|
|
|
|
prefix := p.prefixes[len(p.prefixes)-1]
|
|
|
|
|
p.prefixes = p.prefixes[:len(p.prefixes)-1]
|
|
|
|
|
if prefix == "" {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
p.deleteAttrPrefix(prefix)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
|
|
|
|
|
marshalerAttrType = reflect.TypeOf((*MarshalerAttr)(nil)).Elem()
|
2013-08-14 18:52:09 -04:00
|
|
|
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
|
2013-08-14 14:58:28 -04: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.
|
2013-08-14 14:58:28 -04:00
|
|
|
func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplate *StartElement) error {
|
|
|
|
|
if startTemplate != nil && startTemplate.Name.Local == "" {
|
|
|
|
|
return fmt.Errorf("xml: EncodeElement of StartElement with missing name")
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
val = val.Elem()
|
|
|
|
|
typ = val.Type()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for marshaler.
|
2013-08-14 18:52:09 -04:00
|
|
|
if val.CanInterface() && typ.Implements(marshalerType) {
|
|
|
|
|
return p.marshalInterface(val.Interface().(Marshaler), defaultStart(typ, finfo, startTemplate))
|
|
|
|
|
}
|
|
|
|
|
if val.CanAddr() {
|
2013-08-14 14:58:28 -04:00
|
|
|
pv := val.Addr()
|
|
|
|
|
if pv.CanInterface() && pv.Type().Implements(marshalerType) {
|
2013-08-14 18:52:09 -04:00
|
|
|
return p.marshalInterface(pv.Interface().(Marshaler), defaultStart(pv.Type(), finfo, startTemplate))
|
2013-08-14 14:58:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
2013-08-14 18:52:09 -04:00
|
|
|
|
|
|
|
|
// Check for text marshaler.
|
|
|
|
|
if val.CanInterface() && typ.Implements(textMarshalerType) {
|
|
|
|
|
return p.marshalTextInterface(val.Interface().(encoding.TextMarshaler), defaultStart(typ, finfo, startTemplate))
|
|
|
|
|
}
|
|
|
|
|
if val.CanAddr() {
|
|
|
|
|
pv := val.Addr()
|
|
|
|
|
if pv.CanInterface() && pv.Type().Implements(textMarshalerType) {
|
|
|
|
|
return p.marshalTextInterface(pv.Interface().(encoding.TextMarshaler), defaultStart(pv.Type(), finfo, startTemplate))
|
|
|
|
|
}
|
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++ {
|
2013-08-14 14:58:28 -04:00
|
|
|
if err := p.marshalValue(val.Index(i), finfo, startTemplate); 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
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
// Create start element.
|
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
|
|
|
// Precedence for the XML element name is:
|
2013-08-14 14:58:28 -04:00
|
|
|
// 0. startTemplate
|
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
|
|
|
// 1. XMLName field in underlying struct;
|
|
|
|
|
// 2. field name/tag in the struct field; and
|
|
|
|
|
// 3. type name
|
2013-08-14 14:58:28 -04:00
|
|
|
var start StartElement
|
|
|
|
|
|
|
|
|
|
if startTemplate != nil {
|
|
|
|
|
start.Name = startTemplate.Name
|
|
|
|
|
start.Attr = append(start.Attr, startTemplate.Attr...)
|
|
|
|
|
} else if tinfo.xmlname != 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
|
|
|
xmlname := tinfo.xmlname
|
|
|
|
|
if xmlname.name != "" {
|
2013-08-14 14:58:28 -04:00
|
|
|
start.Name.Space, start.Name.Local = xmlname.xmlns, xmlname.name
|
2012-05-16 23:21:31 -03:00
|
|
|
} else if v, ok := xmlname.value(val).Interface().(Name); ok && v.Local != "" {
|
2013-08-14 14:58:28 -04:00
|
|
|
start.Name = v
|
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
|
|
|
}
|
|
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
if start.Name.Local == "" && finfo != nil {
|
|
|
|
|
start.Name.Space, start.Name.Local = finfo.xmlns, finfo.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
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
if start.Name.Local == "" {
|
|
|
|
|
name := typ.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 name == "" {
|
|
|
|
|
return &UnsupportedTypeError{typ}
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
start.Name.Local = 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
|
|
|
}
|
|
|
|
|
|
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-05-16 23:21:31 -03:00
|
|
|
fv := finfo.value(val)
|
2013-08-14 14:58:28 -04:00
|
|
|
name := Name{Space: finfo.xmlns, Local: finfo.name}
|
|
|
|
|
|
|
|
|
|
if finfo.flags&fOmitEmpty != 0 && isEmptyValue(fv) {
|
2012-02-08 01:57:44 -02:00
|
|
|
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
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
|
2013-08-14 18:52:09 -04:00
|
|
|
if fv.Kind() == reflect.Interface && fv.IsNil() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if fv.CanInterface() && fv.Type().Implements(marshalerAttrType) {
|
|
|
|
|
attr, err := fv.Interface().(MarshalerAttr).MarshalXMLAttr(name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if attr.Name.Local != "" {
|
|
|
|
|
start.Attr = append(start.Attr, attr)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
if fv.CanAddr() {
|
|
|
|
|
pv := fv.Addr()
|
|
|
|
|
if pv.CanInterface() && pv.Type().Implements(marshalerAttrType) {
|
|
|
|
|
attr, err := pv.Interface().(MarshalerAttr).MarshalXMLAttr(name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if attr.Name.Local != "" {
|
|
|
|
|
start.Attr = append(start.Attr, attr)
|
|
|
|
|
}
|
|
|
|
|
continue
|
2013-08-14 00:17:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
|
2013-08-14 18:52:09 -04:00
|
|
|
if fv.CanInterface() && fv.Type().Implements(textMarshalerType) {
|
|
|
|
|
text, err := fv.Interface().(encoding.TextMarshaler).MarshalText()
|
2013-08-14 14:58:28 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2013-08-14 18:52:09 -04:00
|
|
|
start.Attr = append(start.Attr, Attr{name, string(text)})
|
2013-08-14 14:58:28 -04:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 18:52:09 -04:00
|
|
|
if fv.CanAddr() {
|
|
|
|
|
pv := fv.Addr()
|
|
|
|
|
if pv.CanInterface() && pv.Type().Implements(textMarshalerType) {
|
|
|
|
|
text, err := pv.Interface().(encoding.TextMarshaler).MarshalText()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
start.Attr = append(start.Attr, Attr{name, string(text)})
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
// Dereference or skip nil pointer, interface values.
|
|
|
|
|
switch fv.Kind() {
|
|
|
|
|
case reflect.Ptr, reflect.Interface:
|
|
|
|
|
if fv.IsNil() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2013-08-08 10:40:51 -07:00
|
|
|
fv = fv.Elem()
|
|
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
|
|
|
|
|
s, b, err := p.marshalSimple(fv.Type(), fv)
|
|
|
|
|
if err != nil {
|
2012-01-23 00:50:05 -02:00
|
|
|
return err
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
if b != nil {
|
|
|
|
|
s = string(b)
|
|
|
|
|
}
|
|
|
|
|
start.Attr = append(start.Attr, Attr{name, s})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := p.writeStart(&start); err != nil {
|
|
|
|
|
return err
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
|
|
|
|
|
2012-01-23 00:50:05 -02:00
|
|
|
if val.Kind() == reflect.Struct {
|
|
|
|
|
err = p.marshalStruct(tinfo, val)
|
|
|
|
|
} else {
|
2013-08-14 14:58:28 -04:00
|
|
|
s, b, err1 := p.marshalSimple(typ, val)
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
err = err1
|
|
|
|
|
} else if b != nil {
|
|
|
|
|
EscapeText(p, b)
|
|
|
|
|
} else {
|
|
|
|
|
p.EscapeString(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := p.writeEnd(start.Name); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p.cachedWriteError()
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 18:52:09 -04:00
|
|
|
// defaultStart returns the default start element to use,
|
|
|
|
|
// given the reflect type, field info, and start template.
|
|
|
|
|
func defaultStart(typ reflect.Type, finfo *fieldInfo, startTemplate *StartElement) StartElement {
|
2013-08-14 14:58:28 -04:00
|
|
|
var start StartElement
|
|
|
|
|
// Precedence for the XML element name is as above,
|
|
|
|
|
// except that we do not look inside structs for the first field.
|
|
|
|
|
if startTemplate != nil {
|
|
|
|
|
start.Name = startTemplate.Name
|
|
|
|
|
start.Attr = append(start.Attr, startTemplate.Attr...)
|
|
|
|
|
} else if finfo != nil && finfo.name != "" {
|
|
|
|
|
start.Name.Local = finfo.name
|
|
|
|
|
start.Name.Space = finfo.xmlns
|
|
|
|
|
} else if typ.Name() != "" {
|
|
|
|
|
start.Name.Local = typ.Name()
|
|
|
|
|
} else {
|
|
|
|
|
// Must be a pointer to a named type,
|
|
|
|
|
// since it has the Marshaler methods.
|
|
|
|
|
start.Name.Local = typ.Elem().Name()
|
2012-01-23 00:50:05 -02:00
|
|
|
}
|
2013-08-14 18:52:09 -04:00
|
|
|
return start
|
|
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
|
2013-08-14 18:52:09 -04:00
|
|
|
// marshalInterface marshals a Marshaler interface value.
|
|
|
|
|
func (p *printer) marshalInterface(val Marshaler, start StartElement) error {
|
2013-08-14 14:58:28 -04:00
|
|
|
// Push a marker onto the tag stack so that MarshalXML
|
|
|
|
|
// cannot close the XML tags that it did not open.
|
|
|
|
|
p.tags = append(p.tags, Name{})
|
|
|
|
|
n := len(p.tags)
|
|
|
|
|
|
|
|
|
|
err := val.MarshalXML(p.encoder, start)
|
2012-01-23 00:50:05 -02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
// Make sure MarshalXML closed all its tags. p.tags[n-1] is the mark.
|
|
|
|
|
if len(p.tags) > n {
|
|
|
|
|
return fmt.Errorf("xml: %s.MarshalXML wrote invalid XML: <%s> not closed", receiverType(val), p.tags[len(p.tags)-1].Local)
|
|
|
|
|
}
|
|
|
|
|
p.tags = p.tags[:n-1]
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 18:52:09 -04:00
|
|
|
// marshalTextInterface marshals a TextMarshaler interface value.
|
|
|
|
|
func (p *printer) marshalTextInterface(val encoding.TextMarshaler, start StartElement) error {
|
|
|
|
|
if err := p.writeStart(&start); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
text, err := val.MarshalText()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
EscapeText(p, text)
|
|
|
|
|
return p.writeEnd(start.Name)
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
// writeStart writes the given start element.
|
|
|
|
|
func (p *printer) writeStart(start *StartElement) error {
|
|
|
|
|
if start.Name.Local == "" {
|
|
|
|
|
return fmt.Errorf("xml: start tag with no name")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.tags = append(p.tags, start.Name)
|
|
|
|
|
p.markPrefix()
|
|
|
|
|
|
|
|
|
|
p.writeIndent(1)
|
|
|
|
|
p.WriteByte('<')
|
|
|
|
|
p.WriteString(start.Name.Local)
|
|
|
|
|
|
|
|
|
|
if start.Name.Space != "" {
|
|
|
|
|
p.WriteString(` xmlns="`)
|
|
|
|
|
p.EscapeString(start.Name.Space)
|
|
|
|
|
p.WriteByte('"')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attributes
|
|
|
|
|
for _, attr := range start.Attr {
|
|
|
|
|
name := attr.Name
|
|
|
|
|
if name.Local == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
p.WriteByte(' ')
|
|
|
|
|
if name.Space != "" {
|
|
|
|
|
p.WriteString(p.createAttrPrefix(name.Space))
|
|
|
|
|
p.WriteByte(':')
|
|
|
|
|
}
|
|
|
|
|
p.WriteString(name.Local)
|
|
|
|
|
p.WriteString(`="`)
|
|
|
|
|
p.EscapeString(attr.Value)
|
|
|
|
|
p.WriteByte('"')
|
|
|
|
|
}
|
|
|
|
|
p.WriteByte('>')
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *printer) writeEnd(name Name) error {
|
|
|
|
|
if name.Local == "" {
|
|
|
|
|
return fmt.Errorf("xml: end tag with no name")
|
|
|
|
|
}
|
|
|
|
|
if len(p.tags) == 0 || p.tags[len(p.tags)-1].Local == "" {
|
|
|
|
|
return fmt.Errorf("xml: end tag </%s> without start tag", name.Local)
|
|
|
|
|
}
|
|
|
|
|
if top := p.tags[len(p.tags)-1]; top != name {
|
|
|
|
|
if top.Local != name.Local {
|
|
|
|
|
return fmt.Errorf("xml: end tag </%s> does not match start tag <%s>", name.Local, top.Local)
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("xml: end tag </%s> in namespace %s does not match start tag <%s> in namespace %s", name.Local, name.Space, top.Local, top.Space)
|
|
|
|
|
}
|
|
|
|
|
p.tags = p.tags[:len(p.tags)-1]
|
|
|
|
|
|
2012-02-16 02:01:46 -02:00
|
|
|
p.writeIndent(-1)
|
2012-01-23 00:50:05 -02:00
|
|
|
p.WriteByte('<')
|
|
|
|
|
p.WriteByte('/')
|
2013-08-14 14:58:28 -04:00
|
|
|
p.WriteString(name.Local)
|
2012-01-23 00:50:05 -02:00
|
|
|
p.WriteByte('>')
|
2013-08-14 14:58:28 -04:00
|
|
|
p.popPrefix()
|
|
|
|
|
return nil
|
2012-01-23 00:50:05 -02:00
|
|
|
}
|
|
|
|
|
|
2013-08-14 14:58:28 -04:00
|
|
|
func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []byte, error) {
|
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:
|
2013-08-14 14:58:28 -04:00
|
|
|
return strconv.FormatInt(val.Int(), 10), nil, nil
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2013-08-14 14:58:28 -04:00
|
|
|
return strconv.FormatUint(val.Uint(), 10), nil, nil
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2013-08-14 14:58:28 -04:00
|
|
|
return strconv.FormatFloat(val.Float(), 'g', -1, val.Type().Bits()), nil, nil
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.String:
|
2013-08-14 14:58:28 -04:00
|
|
|
return val.String(), nil, nil
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Bool:
|
2013-08-14 14:58:28 -04:00
|
|
|
return strconv.FormatBool(val.Bool()), nil, nil
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Array:
|
|
|
|
|
// will be [...]byte
|
2013-02-20 14:41:23 -08:00
|
|
|
var bytes []byte
|
|
|
|
|
if val.CanAddr() {
|
|
|
|
|
bytes = val.Slice(0, val.Len()).Bytes()
|
|
|
|
|
} else {
|
|
|
|
|
bytes = make([]byte, val.Len())
|
|
|
|
|
reflect.Copy(reflect.ValueOf(bytes), val)
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
return "", bytes, nil
|
2011-06-27 19:07:28 -04:00
|
|
|
case reflect.Slice:
|
|
|
|
|
// will be []byte
|
2013-08-14 14:58:28 -04:00
|
|
|
return "", val.Bytes(), nil
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
return "", nil, &UnsupportedTypeError{typ}
|
2011-06-27 19:07:28 -04:00
|
|
|
}
|
|
|
|
|
|
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 {
|
2013-08-14 14:58:28 -04:00
|
|
|
s := parentStack{p: p}
|
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]
|
2013-03-12 16:42:25 -04:00
|
|
|
if finfo.flags&fAttr != 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
|
|
|
continue
|
|
|
|
|
}
|
2012-05-16 23:21:31 -03:00
|
|
|
vf := finfo.value(val)
|
2013-08-14 14:58:28 -04:00
|
|
|
|
|
|
|
|
// Dereference or skip nil pointer, interface values.
|
|
|
|
|
switch vf.Kind() {
|
|
|
|
|
case reflect.Ptr, reflect.Interface:
|
|
|
|
|
if !vf.IsNil() {
|
|
|
|
|
vf = vf.Elem()
|
|
|
|
|
}
|
2013-08-08 10:40:51 -07:00
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
|
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
|
|
|
switch finfo.flags & fMode {
|
|
|
|
|
case fCharData:
|
2013-08-14 18:52:09 -04:00
|
|
|
if vf.CanInterface() && vf.Type().Implements(textMarshalerType) {
|
|
|
|
|
data, err := vf.Interface().(encoding.TextMarshaler).MarshalText()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
Escape(p, data)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if vf.CanAddr() {
|
|
|
|
|
pv := vf.Addr()
|
|
|
|
|
if pv.CanInterface() && pv.Type().Implements(textMarshalerType) {
|
|
|
|
|
data, err := pv.Interface().(encoding.TextMarshaler).MarshalText()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
Escape(p, data)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-22 22:13:40 -05:00
|
|
|
var scratch [64]byte
|
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
|
|
|
switch vf.Kind() {
|
2013-01-22 22:13:40 -05:00
|
|
|
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()))
|
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
|
|
|
case reflect.String:
|
2013-02-20 14:41:23 -08:00
|
|
|
if err := EscapeText(p, []byte(vf.String())); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
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
|
|
|
case reflect.Slice:
|
|
|
|
|
if elem, ok := vf.Interface().([]byte); ok {
|
2013-02-20 14:41:23 -08:00
|
|
|
if err := EscapeText(p, elem); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-22 10:00:36 -05:00
|
|
|
case fElement, fElement | fAny:
|
2013-08-14 14:58:28 -04:00
|
|
|
if err := s.trim(finfo.parents); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
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 len(finfo.parents) > len(s.stack) {
|
|
|
|
|
if vf.Kind() != reflect.Ptr && vf.Kind() != reflect.Interface || !vf.IsNil() {
|
2013-08-14 14:58:28 -04:00
|
|
|
if err := s.push(finfo.parents[len(s.stack):]); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-14 14:58:28 -04:00
|
|
|
if err := p.marshalValue(vf, finfo, nil); err != 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 err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s.trim(nil)
|
2012-06-25 16:00:35 -04:00
|
|
|
return p.cachedWriteError()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return the bufio Writer's cached write error
|
|
|
|
|
func (p *printer) cachedWriteError() error {
|
|
|
|
|
_, err := p.Write(nil)
|
|
|
|
|
return err
|
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-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
|
|
|
|
|
}
|
2013-02-03 11:21:07 -05:00
|
|
|
if p.putNewline {
|
|
|
|
|
p.WriteByte('\n')
|
|
|
|
|
} else {
|
|
|
|
|
p.putNewline = true
|
|
|
|
|
}
|
2012-02-16 02:01:46 -02:00
|
|
|
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 {
|
2013-08-14 14:58:28 -04:00
|
|
|
p *printer
|
2011-08-26 12:29:52 -03:00
|
|
|
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.
|
2013-08-14 14:58:28 -04:00
|
|
|
func (s *parentStack) trim(parents []string) error {
|
2011-08-26 12:29:52 -03:00
|
|
|
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-- {
|
2013-08-14 14:58:28 -04:00
|
|
|
if err := s.p.writeEnd(Name{Local: s.stack[i]}); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2011-08-26 12:29:52 -03:00
|
|
|
}
|
|
|
|
|
s.stack = parents[:split]
|
2013-08-14 14:58:28 -04:00
|
|
|
return nil
|
2011-08-26 12:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// push adds parent elements to the stack and writes open tags.
|
2013-08-14 14:58:28 -04:00
|
|
|
func (s *parentStack) push(parents []string) error {
|
2011-08-26 12:29:52 -03:00
|
|
|
for i := 0; i < len(parents); i++ {
|
2013-08-14 14:58:28 -04:00
|
|
|
if err := s.p.writeStart(&StartElement{Name: Name{Local: parents[i]}}); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2011-08-26 12:29:52 -03:00
|
|
|
}
|
|
|
|
|
s.stack = append(s.stack, parents...)
|
2013-08-14 14:58:28 -04:00
|
|
|
return nil
|
2011-08-26 12:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|