Commit 54bdfc00 authored by Russ Cox's avatar Russ Cox

encoding/xml: add, support Marshaler interface

See golang.org/s/go12xml for design.

Repeat of CL 12603044, which was submitted accidentally
and then rolled back.

Fixes #2771.
Fixes #4169.
Fixes #5975.
Fixes #6125.

R=golang-dev
CC=golang-dev
https://golang.org/cl/12919043
parent 84b0842a
...@@ -92,7 +92,6 @@ pkg encoding/json, method (Number) String() string ...@@ -92,7 +92,6 @@ pkg encoding/json, method (Number) String() string
pkg encoding/json, type Number string pkg encoding/json, type Number string
pkg encoding/xml, func EscapeText(io.Writer, []uint8) error pkg encoding/xml, func EscapeText(io.Writer, []uint8) error
pkg encoding/xml, method (*Encoder) Indent(string, string) pkg encoding/xml, method (*Encoder) Indent(string, string)
pkg encoding/xml, method (Encoder) ReadFrom(io.Reader) (int64, error)
pkg encoding/xml, type Decoder struct, DefaultSpace string pkg encoding/xml, type Decoder struct, DefaultSpace string
pkg go/ast, func NewCommentMap(*token.FileSet, Node, []*CommentGroup) CommentMap pkg go/ast, func NewCommentMap(*token.FileSet, Node, []*CommentGroup) CommentMap
pkg go/ast, method (CommentMap) Comments() []*CommentGroup pkg go/ast, method (CommentMap) Comments() []*CommentGroup
......
...@@ -2425,13 +2425,6 @@ pkg encoding/xml, method (*UnsupportedTypeError) Error() string ...@@ -2425,13 +2425,6 @@ pkg encoding/xml, method (*UnsupportedTypeError) Error() string
pkg encoding/xml, method (CharData) Copy() CharData pkg encoding/xml, method (CharData) Copy() CharData
pkg encoding/xml, method (Comment) Copy() Comment pkg encoding/xml, method (Comment) Copy() Comment
pkg encoding/xml, method (Directive) Copy() Directive pkg encoding/xml, method (Directive) Copy() Directive
pkg encoding/xml, method (Encoder) Available() int
pkg encoding/xml, method (Encoder) Buffered() int
pkg encoding/xml, method (Encoder) Flush() error
pkg encoding/xml, method (Encoder) Write([]uint8) (int, error)
pkg encoding/xml, method (Encoder) WriteByte(uint8) error
pkg encoding/xml, method (Encoder) WriteRune(int32) (int, error)
pkg encoding/xml, method (Encoder) WriteString(string) (int, error)
pkg encoding/xml, method (ProcInst) Copy() ProcInst pkg encoding/xml, method (ProcInst) Copy() ProcInst
pkg encoding/xml, method (StartElement) Copy() StartElement pkg encoding/xml, method (StartElement) Copy() StartElement
pkg encoding/xml, method (UnmarshalError) Error() string pkg encoding/xml, method (UnmarshalError) Error() string
This diff is collapsed.
...@@ -289,6 +289,31 @@ type ChardataEmptyTest struct { ...@@ -289,6 +289,31 @@ type ChardataEmptyTest struct {
Contents *string `xml:",chardata"` Contents *string `xml:",chardata"`
} }
type MyMarshalerTest struct {
}
var _ Marshaler = (*MyMarshalerTest)(nil)
func (m *MyMarshalerTest) MarshalXML(e *Encoder, start StartElement) error {
e.EncodeToken(start)
e.EncodeToken(CharData([]byte("hello world")))
e.EncodeToken(EndElement{start.Name})
return nil
}
type MyMarshalerAttrTest struct {
}
var _ MarshalerAttr = (*MyMarshalerAttrTest)(nil)
func (m *MyMarshalerAttrTest) MarshalXMLAttr(name Name) (Attr, error) {
return Attr{name, "hello world"}, nil
}
type MarshalerStruct struct {
Foo MyMarshalerAttrTest `xml:",attr"`
}
var ( var (
nameAttr = "Sarah" nameAttr = "Sarah"
ageAttr = uint(12) ageAttr = uint(12)
...@@ -844,6 +869,15 @@ var marshalTests = []struct { ...@@ -844,6 +869,15 @@ var marshalTests = []struct {
ExpectXML: `<Strings><A></A></Strings>`, ExpectXML: `<Strings><A></A></Strings>`,
Value: &Strings{}, Value: &Strings{},
}, },
// Custom marshalers.
{
ExpectXML: `<MyMarshalerTest>hello world</MyMarshalerTest>`,
Value: &MyMarshalerTest{},
},
{
ExpectXML: `<MarshalerStruct Foo="hello world"></MarshalerStruct>`,
Value: &MarshalerStruct{},
},
} }
func TestMarshal(t *testing.T) { func TestMarshal(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment