I am dealing with an API that is expecting XML that is generally formatted like so.
<receiver>
<receiver_type>test</receiver_type>
<hostnames>http://posttestserver.com</hostnames>
<credentials>
<credential>
<name>app-id</name>
<value>1234</value>
<safe>true</safe>
</credential>
<credential>
<name>app-secret</name>
<value>5678</value>
</credential>
</credentials>
</receiver>
Therefore, in my Go code, I made these structs to use in my functions and marshal into the XML structure. Note that different receivers can have different sets of credentials.
type Receiver1 struct {
XMLName xml.Name `xml:"receiver"`
ReceiverType string `xml:"receiver_type"`
HostNames string `xml:"hostnames"`
Credentials Receiver1Credentials `xml:"credentials"`
}
type Receiver2 struct {
XMLName xml.Name `xml:"receiver"`
ReceiverType string `xml:"receiver_type"`
HostNames string `xml:"hostnames"`
Credentials Receiver2Credentials `xml:"credentials"`
}
type Receiver1Credentials struct {
AppID Credential `xml:"credential"`
AppSecret Credential `xml:"credential"`
}
type Receiver2Credentials struct {
UserName Credential `xml:"credential"`
Password Credential `xml:"credential"`
AppID Credential `xml:"credential"`
}
type Credential struct {
Name string `xml:"name"`
Value string `xml:"value"`
Safe bool `xml:"safe"`
}
However this produces runtime errors along the lines of Receiver1Credentials field "AppID" with tag "credential" conflicts with field "AppSecret" with tag "credential"
meaning that I can't directly tag the Credentials fields the same thing. I've tried using a field XMLName xml.Name xml:"credential"
(like on the Receiver structs) on the Credential struct, but it gets overwritten by the tag on the Credentials structs. Does anyone know how I could work around this?