mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-23 00:34:23 +00:00
internal: add EncodeRawXMLElement
This commit is contained in:
parent
56c162197b
commit
44f7f84ef5
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user