I'm having a bit of trouble getting my head around the proper implementation of this. If I use the structs below which are embedded within each other and push them to a SOAP API, the response is decoded directly into the Response struct.
The problem I have is that everything except the ResponseBody struct would be used elsewhere and it seems wrong to repeat all of these structs in every implementation of it.
So I would like to:
- Keep my code as strongly typed as possible
- Use the generic soap structs elsewhere
- Assign / create the proper object at runtime.
Do I use a struct? Interface? Generics? Something else?
package soapv1
import (
"encoding/xml"
)
//Response
type Response struct {
XMLName xml.Name `xml:"Envelope"`
SoapBody *SOAPBodyResponse
}
//SOAPBodyResponse
type SOAPBodyResponse struct {
XMLName xml.Name `xml:"Body"`
Resp *ResponseBody
FaultDetails *Fault
}
//Fault
type Fault struct {
XMLName xml.Name `xml:"Fault"`
Faultcode string `xml:"faultcode"`
Faultstring string `xml:"faultstring"`
}
//ResponseBody
type ResponseBody struct {
XMLName xml.Name `xml:"loginResponse"`
LoginReturn string `xml:"loginReturn"`
}
// Some other code between here to prepare and post request
// read and parse the response body
result := new(Response)
err = xml.NewDecoder(res.Body).Decode(result)