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)
So you can indeed inject an interface partway through, I was failing it allocate memory for the decoder to work:
Package:
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 interface{} `xml:"records"`
}
func (r *Resource) Query(sql string, r interface{}) error {
t := &QueryEnvelope{Body: &QueryBody{QueryResult: &QueryResult{Records: r}}}
//do query and unmarshal into t
return err
}
Client / main:
type Record struct {
Id string `xml:"id"`
Name string `xml:"name"`
}
r := new([]*Record) // must allocate memory using new
err = ent.Query("select id, name from account limit 3", r)
The common solution is to ask the client to pass a pointer to his struct instance like so:
// The custom struct of your client
type ClientStruct struct {
Id int `xml:"id"`
Name string `xml:"name"`
}
// This would be your API
func Query(foo string, v interface{}) {
fakeXmlResult := "<test><id>012345</id><name>MyName</name></test>"
xml.Unmarshal([]byte(fakeXmlResult), v)
}
func main() {
r := ClientStruct{}
Query("SQL QUERY", &r) // Note the &
fmt.Println(r)
}
Did I understand your question correctly?