我正在制作一个需要获取分页结果的JSON API包装器客户端,其中下一页的URL由上一页提供。为了减少共享相同响应格式的100多个实体的代码重复,我希望有一个单一的客户端方法,从所有分页页面中取出和解组不同的实体。
我目前在简化(伪)版本中的方法(没有错误等):
type ListResponse struct {
Data struct {
Results []interface{} `json:"results"`
Next string `json:"__next"`
} `json:"d"`
}
func (c *Client) ListRequest(uri string) listResponse ListResponse {
// Do a http request to uri and get the body
body := []byte(`{ "d": { "__next": "URL", "results": []}}`)
json.NewDecoder(body).Decode(&listResponse)
}
func (c *Client) ListRequestAll(uri string, v interface{}) {
a := []interface{}
f := c.ListRequest(uri)
a = append(a, f.Data.Results...)
var next = f.Data.Next
for next != "" {
r := c.ListRequest(next)
a = append(a, r.Data.Results...)
next = r.Data.Next
}
b, _ := json.Marshal(a)
json.Unmarshal(b, v)
}
// Then in a method requesting all results for a single entity
var entities []Entity1
client.ListRequestAll("https://foo.bar/entities1.json", &entities)
// and somewehere else
var entities []Entity2
client.ListRequestAll("https://foo.bar/entities2.json", &entities)
然而问题是这种方法效率低下并且使用了太多内存等,即首先在一般ListResponse
中解组,结果为[]interface{}
(查看下一个URL并将结果连成一个切片),然后编组[]interface{}
以解组它直接在[]Entity1
的目的地切片中向后。
我或许可以使用reflect
包动态地制作这些实体的新切片,直接解组到它们之后然后连接/追加它们,但是如果我理解正确的话我最好不要使用reflect
除非严格必要...
看看RawMessage
包装中的encoding/json
类型。它允许您将json值的解码推迟到以后。例如:
Results []json.RawMessage `json:"results"`
甚至...
Results json.RawMessage `json:"results"`
由于json.RawMessage
只是一个字节的一部分,这将比你解组的中间[]interface{}
更有效。
关于如何在给定多页读取的情况下将这些组合成单个切片的第二部分,您可以通过使调用者使用切片类型切片向调用者提出该问题。
// Then in a method requesting all results for a single entity
var entityPages [][]Entity1
client.ListRequestAll("https://foo.bar/entities1.json", &entityPages)
但是,由于您必须一次加载所有页面/项目,因此仍然存在您的一般设计无限制的内存消耗问题。您可能需要考虑更改为使用文件等开放/读取抽象。你有一些Open
方法返回另一种类型,就像os.File
一样,它提供了一次读取数据子集的方法,同时在内部请求页面并根据需要进行缓冲。
也许是这样的(未经测试):
type PagedReader struct {
c *Client
buffer []json.RawMessage
next string
}
func (r *PagedReader) getPage() {
f := r.c.ListRequest(r.next)
r.next = f.Data.Next
r.buffer = append(r.buffer, f.Data.Results...)
}
func (r *PagedReader) ReadItems(output []interface{}) int {
for len(output) > len(buffer) && r.next != "" {
r.getPage()
}
n := 0
for i:=0;i<len(output)&&i< len(r.buffer);i++ {
json.Unmarshal(r.buffer[i], output[i] )
n++
}
r.buffer = r.buffer[n:]
return n
}