Few of the XML tags are not getting decoded while reading the request body
I have defined my nested struct with both json and xml tags, as I want to use the same schema for request and response in both json and xml.
var dataNewTestplans DataTestplan
err := xml.NewDecoder(r.Body).Decode(&dataNewTestplans)
xmlData, _ := xml.Marshal(dataNewTestplans)
fmt.Printf(string(xmlData))
DataTestplan struct:
type DataTestplan struct {
Data []Testplan `json:"data" xml:"data"`
}
Testplan struct:
type Testplan struct {
Group string `json:"group" xml:"group"`
Name string `json:"name" xml:"name"`
Parameters []Parameter `json:"parameters,omitempty" xml:"parameters,omitempty"`
Released bool `json:"released" xml:"released"`
Teststeps []Teststep `json:"teststeps,omitempty" xml:"teststeps,omitempty"`
Version int32 `json:"version" xml:"version"`
}
Parameter struct:
type Parameter struct {
XMLName xml.Name `xml:"parameters"`
Comment string `json:"comment" xml:"comment"`
Description string `json:"description,omitempty" xml:"description,omitempty"`
}
Teststep struct:
type Teststep struct {
XMLName xml.Name `xml:"teststeps"`
AslID string `json:"aslId,omitempty" xml:"aslID"`
Bin int32 `json:"bin,omitempty" xml:"bin"`
}
XML sent :
<root>
<data>
<element>
<group>TEST</group>
<name>TEST</name>
<parameters>
<element>
<comment>test</comment>
<description>test</description>
</element>
<element>
<comment>test1</comment>
</element>
</parameters>
<released>true</released>
<teststeps>
<element>
<bin>32</bin>
</element>
</teststeps>
<version>1</version>
</element>
</data>
</root>
XML decoded:
<DataTestplan>
<data>
<group></group>
<name></name>
<released>false</released>
<version>0</version>
</data>
</DataTestplan>
I understand missing tags are most probably because of miss tagging of xml in the struct definition but I am not sure why the decoded information is missing value for tags ? What's the trick with XML and JSON encoding ?