-
Notifications
You must be signed in to change notification settings - Fork 1
/
entities.go
52 lines (47 loc) · 1.28 KB
/
entities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gcloudcx
import (
"context"
"encoding/json"
)
type Entities struct {
Entities [][]byte `json:"-"`
PageSize int64 `json:"pageSize"`
PageNumber int64 `json:"pageNumber"`
PageCount uint64 `json:"pageCount"`
PageTotal uint64 `json:"total"`
FirstURI string `json:"firstUri"`
SelfURI string `json:"selfUri"`
LastURI string `json:"lastUri"`
}
func (client *Client) FetchEntities(context context.Context, uri URI) ([][]byte, error) {
entities := Entities{}
values := [][]byte{}
page := uint64(1)
for {
if err := client.Get(context, uri.WithQuery(Query{"pageNumber": page}), &entities); err != nil {
return nil, err
}
values = append(values, entities.Entities...)
if page++; page > entities.PageCount {
break
}
}
return values, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (entities *Entities) UnmarshalJSON(data []byte) error {
type surrogate Entities
var inner struct {
surrogate
Entities []json.RawMessage `json:"entities"`
}
if err := json.Unmarshal(data, &inner); err != nil {
return err
}
*entities = Entities(inner.surrogate)
entities.Entities = make([][]byte, 0, len(inner.Entities))
for _, entity := range inner.Entities {
entities.Entities = append(entities.Entities, entity)
}
return nil
}