我目前正在针对返回批处理结果的 xml 服务进行编写。我目前有以下几点:
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 {
QueryLocator QueryLocator `xml:"queryLocator"`
Done bool `xml:"done"`
Size int `xml:"size"`
Records interface{} `xml:"records"`
}
type QueryLocator string
func (r *Resource) Query(sql string, r interface{}) error {
// builds xml request
// sends xml request through Resource which contains the net client
// decodes the result into r (type for records in QueryResult)
}
func (r *Resource) QueryMore(q QueryLocator, r interface{}) error {
// same as above except takes queryLocator and makes call to different endpoint in order to continue retrieving results, when QueryResult.Done == true, done fetching results
}
显然这需要重构,因为客户端需要查看是否 Done == true 以便他们可以继续获取。我正在考虑添加以下内容,并将 Query 和 QueryMore 移动为 Querier 的一种方法:
type Querier struct { r *Resource Done bool QueryLocator QueryLocator }
func New(r *Resource) *查询器{}
客户端然后会这样表现:
err := q.Query("sql statement", r)
if err != nil {
// handle
}
// do stuff with r
for q.Done == false {
q.QueryMore(r)
// do stuff with r
}
我想知道这里的惯用方法是什么,以便最好地“流式传输”结果。
呼唤远方
相关分类