diff --git a/marshal.go b/marshal.go index 9af0290..d6a20b0 100644 --- a/marshal.go +++ b/marshal.go @@ -4,6 +4,7 @@ package fargo import ( "encoding/json" + "encoding/xml" "strconv" ) @@ -120,3 +121,26 @@ func (i *InstanceMetadata) MarshalJSON() ([]byte, error) { return i.Raw, nil } + +// MarshalXML is a custom XML marshaler for InstanceMetadata. +func (i InstanceMetadata) MarshalXML(e *xml.Encoder, start xml.StartElement) error { + tokens := []xml.Token{start} + + if i.parsed != nil { + for key, value := range i.parsed { + t := xml.StartElement{Name: xml.Name{"", key}} + tokens = append(tokens, t, xml.CharData(value.(string)), xml.EndElement{t.Name}) + } + } + tokens = append(tokens, xml.EndElement{start.Name}) + + for _, t := range tokens { + err := e.EncodeToken(t) + if err != nil { + return err + } + } + + // flush to ensure tokens are written + return e.Flush() +}