Skip to content

Instantly share code, notes, and snippets.

@jackspirou
Last active September 6, 2024 03:24
Show Gist options
  • Save jackspirou/4477e37d1f1c043806e0 to your computer and use it in GitHub Desktop.
Save jackspirou/4477e37d1f1c043806e0 to your computer and use it in GitHub Desktop.

Revisions

  1. jackspirou revised this gist Aug 7, 2015. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions StringMapStringMarshalXML.go
    Original file line number Diff line number Diff line change
    @@ -8,16 +8,14 @@ func (s StringMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    tokens := []xml.Token{start}

    for key, value := range s {
    tok := xml.StartElement{Name: xml.Name{"", key}}
    tokens = append(tokens, tok)
    tokens = append(tokens, xml.CharData(value))
    tokens = append(tokens, xml.EndElement{tok.Name})
    t := xml.StartElement{Name: xml.Name{"", key}}
    tokens = append(tokens, t, xml.CharData(value), xml.EndElement{t.Name})
    }

    tokens = append(tokens, xml.EndElement{start.Name})

    for _, tok := range tokens {
    err := e.EncodeToken(tok)
    for _, t := range tokens {
    err := e.EncodeToken(t)
    if err != nil {
    return err
    }
  2. jackspirou revised this gist Aug 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion StringMapStringMarshalXML.go
    Original file line number Diff line number Diff line change
    @@ -30,4 +30,4 @@ func (s StringMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    }

    return nil
    }
    }
  3. jackspirou revised this gist Aug 6, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions StringMapStringMarshalXML.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    // StringMap is a map[string]string.
    type StringMap map[string]string

  4. jackspirou renamed this gist Aug 6, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. jackspirou revised this gist Aug 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion StringMapMarshalXML.go
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ func (s StringMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    }
    }

    // flush to ensure tokens are written
    // flush to ensure tokens are written
    err := e.Flush()
    if err != nil {
    return err
  6. jackspirou created this gist Aug 6, 2015.
    32 changes: 32 additions & 0 deletions StringMapMarshalXML.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    // StringMap is a map[string]string.
    type StringMap map[string]string

    // StringMap marshals into XML.
    func (s StringMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {

    tokens := []xml.Token{start}

    for key, value := range s {
    tok := xml.StartElement{Name: xml.Name{"", key}}
    tokens = append(tokens, tok)
    tokens = append(tokens, xml.CharData(value))
    tokens = append(tokens, xml.EndElement{tok.Name})
    }

    tokens = append(tokens, xml.EndElement{start.Name})

    for _, tok := range tokens {
    err := e.EncodeToken(tok)
    if err != nil {
    return err
    }
    }

    // flush to ensure tokens are written
    err := e.Flush()
    if err != nil {
    return err
    }

    return nil
    }