Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: simple use of EncodeToken fails #11719

Closed
rsc opened this Issue Jul 15, 2015 · 3 comments

Comments

Projects
None yet
3 participants
@rsc
Copy link
Contributor

rsc commented Jul 15, 2015

https://play.golang.org/p/DnUU5PWitz should not say 'empty buffer' unless I am very confused about how this package works.

package main

import (
    "bytes"
    "encoding/xml"
    "log"
)

func main() {
    log.SetFlags(0)
    var buf bytes.Buffer
    enc := xml.NewEncoder(&buf)
    if err := enc.EncodeToken(&xml.StartElement{Name: xml.Name{"", "object"}}); err != nil {
        log.Fatal(err)
    }
    if err := enc.EncodeToken(&xml.EndElement{Name: xml.Name{"", "object"}}); err != nil {
        log.Fatal(err)
    }
    enc.Flush()

    if buf.Len() == 0 {
        log.Fatal("empty buffer") // where did the tokens go!?
    }

    log.Print(buf.String())
}

@rsc rsc self-assigned this Jul 15, 2015

@rsc rsc added this to the Go1.5 milestone Jul 15, 2015

@dspezia

This comment has been minimized.

Copy link
Contributor

dspezia commented Jul 15, 2015

EncodeToken expects a value, not a pointer. There is no default case in the type switch, that's why a token with a "wrong" type is just ignored. We have different solutions to fix this:

  • option 1: issue an error when the type is invalid (this includes pointers to valid tokens)
  • option 2: like 1, but if a pointer is provided, just convert it to a value (one level only), and consider this value.
  • option 3: like 1, but if a pointer or an interface is provided, drill down until a value is find, and then consider this value.

Submitting a CL to implement option 2, to be changed if another behavior is preferred.

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Jul 15, 2015

CL https://golang.org/cl/12252 mentions this issue.

@dspezia

This comment has been minimized.

Copy link
Contributor

dspezia commented Jul 17, 2015

Following code review, option 1 is preferred over option 2 (for go1.5). CL changed accordingly.

@rsc rsc closed this in ca1d6c4 Jul 23, 2015

@golang golang locked and limited conversation to collaborators Aug 5, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.