internal: add EncodeRawXMLElement

This commit is contained in:
Simon Ser 2020-01-15 11:14:34 +01:00
parent 56c162197b
commit 44f7f84ef5
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -11,6 +11,10 @@ import (
type RawXMLValue struct { type RawXMLValue struct {
tok xml.Token // guaranteed not to be xml.EndElement tok xml.Token // guaranteed not to be xml.EndElement
children []RawXMLValue 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. // NewRawXMLElement creates a new RawXMLValue for an element.
@ -18,10 +22,17 @@ func NewRawXMLElement(name xml.Name, attr []xml.Attr, children []RawXMLValue) *R
return &RawXMLValue{tok: xml.StartElement{name, attr}, children: children} 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. // UnmarshalXML implements xml.Unmarshaler.
func (val *RawXMLValue) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { func (val *RawXMLValue) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
val.tok = start val.tok = start
val.children = nil val.children = nil
val.out = nil
for { for {
tok, err := d.Token() tok, err := d.Token()
@ -45,6 +56,10 @@ func (val *RawXMLValue) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
// MarshalXML implements xml.Marshaler. // MarshalXML implements xml.Marshaler.
func (val *RawXMLValue) MarshalXML(e *xml.Encoder, start xml.StartElement) error { 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) { switch tok := val.tok.(type) {
case xml.StartElement: case xml.StartElement:
if err := e.EncodeToken(tok); err != nil { if err := e.EncodeToken(tok); err != nil {