~/Projects/webdav-go
git clone https://code.lsong.org/webdav-go
Commit
- Commit
- 44f7f84ef5785174c2003e1aa1415d175ef82205
- Author
- Simon Ser <[email protected]>
- Date
- 2020-01-15 11:14:34 +0100 +0100
- Diffstat
internal/xml.go | 15 +++++++++++++++
internal: add EncodeRawXMLElement
diff --git a/internal/xml.go b/internal/xml.go index c5dbc5b2f6207e9d14676a3bbc3f10b5821864ea..7cf6e0553c68b24a6cd2f745a06ae04702922c41 100644 --- a/internal/xml.go +++ b/internal/xml.go @@ -11,6 +11,10 @@ // encoding. type RawXMLValue struct { tok xml.Token // guaranteed not to be xml.EndElement children []RawXMLValue + + // Unfortunately encoding/xml doesn't offer TokenWriter, so we need to + // cache outgoing data. + out interface{} } // NewRawXMLElement creates a new RawXMLValue for an element. @@ -18,10 +22,17 @@ func NewRawXMLElement(name xml.Name, attr []xml.Attr, children []RawXMLValue) *RawXMLValue { return &RawXMLValue{tok: xml.StartElement{name, attr}, children: children} } +// EncodeRawXMLElement encodes a value into a new RawXMLValue. The XML value +// can only be used for marshalling. +func EncodeRawXMLElement(v interface{}) (*RawXMLValue, error) { + return &RawXMLValue{out: v}, nil +} + // UnmarshalXML implements xml.Unmarshaler. func (val *RawXMLValue) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { val.tok = start val.children = nil + val.out = nil for { tok, err := d.Token() @@ -45,6 +56,10 @@ } // MarshalXML implements xml.Marshaler. func (val *RawXMLValue) MarshalXML(e *xml.Encoder, start xml.StartElement) error { + if val.out != nil { + return e.Encode(val.out) + } + switch tok := val.tok.(type) { case xml.StartElement: if err := e.EncodeToken(tok); err != nil {