Skip to content

Commit

Permalink
Add custom XML serializer for instance metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansb committed Dec 6, 2015
1 parent 2321b58 commit c5511c2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package fargo

import (
"encoding/json"
"encoding/xml"
"strconv"
)

Expand Down Expand Up @@ -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()
}

0 comments on commit c5511c2

Please sign in to comment.