golang xml package which add marshal self-closing tag support
the code applied from https://go-review.googlesource.com/c/go/+/469495
I found this https://twitter.com/ZeCoffee/status/766349635359211520
Custom MarshalXML ref https://pkg.go.dev/encoding/xml#Marshal
self-closing tag example:
import "github.com/ttys3/go-xml"
// no `xml` struct tag is needed or can be used here
// since we handle all this in `MarshalXML`
type Foo struct {
Bar string
Comment string
}
// Custom XML marshaler for Foo
func (i Foo) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
attrs := []xml.Attr{
{
Name: xml.Name{Local: "bar"},
Value: i.Bar,
},
{
Name: xml.Name{Local: "comment"},
Value: i.Comment,
},
}
// Create a self-closing tag for Item
empty := xml.EmptyElement{
Name: xml.Name{
Space: "",
Local: "foo",
},
Attr: attrs,
}
// can not use Encode or EncodeElement here, because they will not emit self-closing tag
err := e.EncodeToken(empty)
if err != nil {
return err
}
// Flush must be called since we are not using Encode or EncodeElement
if err := e.Flush(); err != nil {
return err
}
return nil
}
func TestSelfClodingTagFoo(t *testing.T) {
expectedXML := `<foo bar="hello" comment="world"/>`
foo := Foo{
Bar: "hello",
Comment: "world",
}
marshaledXML, err := xml.MarshalIndent(foo, "", " ")
if err != nil {
t.Fatalf("Failed to marshal XML: %v", err)
}
if string(marshaledXML) != expectedXML {
t.Errorf("Expected marshaled XML:\n%s\n\nGot:\n%s", expectedXML, marshaledXML)
}
}
see golang/go#21399
https://go-review.googlesource.com/c/go/+/469495