I have a soap service I'm writing against. One portion of the soap API is for returning a query result, and I'm hoping to provide the base structs for decoding the envelope, while allow a developer to fill in the interface in which encoding/xml will decode to.
type QueryEnvelope struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
Body *QueryBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}
type QueryBody struct {
QueryResult *QueryResult `xml:"queryResponse>result"`
}
type QueryResult struct {
Done bool `xml:"done"`
Size int `xml:"size"`
Records Record `xml:"records"`
}
type Record interface {
UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
}
Is it possible to inject an interface like this for unmarhsalling or do I have to accept the interface at the QueryEnvelope{} level?
Ideally client side would act as such:
type Record struct {
Id int `xml:"id"`,
Name stirng `xml:"name"`
}
func (r *Record) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// unmasrshal here, or best case I dont even need to implment UnmarsahlXML()!
}
res, err := Query("select id from table", Record{})
This would mean that they don't have to reproduce the QueryEnvelope struct (as a developer using the package I'm creating)