So I've got this specific format for xml (industry standard) and I'm trying to create a simple program to allow us to make samples of this xml to test our services. I'm using the standard Go XML library.
Problem is the XML is annoyingly formatted. Here's the simplified version of it:
<Document>
<SubDocument>
{other fields}
<component>
<section>
<id value="0" valueType="num"/> //This is the part that differentiates the type of the section
<title>Foo-Type Section</title>
{other fields you lot don't need to care about}
</section>
</component>
<component>
<section>
<id value="1" valueType="num"/>
<title>Bar-Type Section</title>
{more fields you don't need to care about, but most are different than above}
</section>
</component>
{more sections}
</SubDocument>
</Document>
What I'm struggling with is that in Go, the tags on each section need to be unique if they are different struct types.
I've the following Go code:
type HasID struct{
ID string `xml:"value,attr,omitempty"`
IDType string `xml:"valueType,attr,omitempty"`
}
type FooSection struct{
ID HasID `xml:"id,omitempty"`
Title string `xml:"title,omitempty"`
//Foo fields
}
type BarSection struct{
ID HasID `xml:"id,omitempty"`
Title string `xml:"title,omitempty"`
//Bar fields
}
type Document struct{
XMLName struct{} `xml:"Document,omitempty"`
//Other fields
Sections []interface{} `xml:"SubDocument>component>section,omitempty"`
}
I've also tried to have the Sections field have no tag and have both FooSection and BarSection have the
XMLName struct{} `xml:"component>section,omitempty"`
tag, to no avail. Furthermore, I've tried having Sections be an array of strings and then marshaled each section type, dumped those in and used the ",innerxml" tag, but then it escapes the "<", etc of the innerxml.
Does anyone know a way to do this in Go? The structs are written by me and are completely open to change if need be.
It might just be that I'm too entrenched in OO and am having trouble being Go-like.
Thanks!