-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
paging.go
120 lines (103 loc) · 3.03 KB
/
paging.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package asc
import (
"encoding/json"
"net/url"
)
// Reference is a wrapper type for a URL that contains a cursor parameter.
type Reference struct {
url.URL
}
// Cursor returns the cursor parameter on the Reference's internal URL.
func (r Reference) Cursor() string {
return r.Query().Get("cursor")
}
// MarshalJSON marshals the Reference into a JSON fragment.
func (r Reference) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
}
// UnmarshalJSON unmarshals the JSON fragment into a Reference.
func (r *Reference) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
u, err := url.Parse(s)
if err != nil {
return err
}
if u != nil {
r.URL = *u
}
return nil
}
// PagedDocumentLinks defines model for PagedDocumentLinks.
type PagedDocumentLinks struct {
First *Reference `json:"first,omitempty"`
Next *Reference `json:"next,omitempty"`
Self Reference `json:"self"`
}
// PagingInformation defines model for PagingInformation.
type PagingInformation struct {
Paging struct {
Limit int `json:"limit"`
Total int `json:"total"`
} `json:"paging"`
}
// ResourceLinks defines model for ResourceLinks.
type ResourceLinks struct {
Self Reference `json:"self"`
}
// DocumentLinks defines model for DocumentLinks.
type DocumentLinks struct {
Self Reference `json:"self"`
}
// Relationship contains data about a related resources as well as API references that can be followed.
type Relationship struct {
Data *RelationshipData `json:"data,omitempty"`
Links *RelationshipLinks `json:"links,omitempty"`
}
// PagedRelationship is a relationship to multiple resources that have paging information.
type PagedRelationship struct {
Data []RelationshipData `json:"data,omitempty"`
Links *RelationshipLinks `json:"links,omitempty"`
Meta *PagingInformation `json:"meta,omitempty"`
}
// RelationshipData contains data on the given relationship.
type RelationshipData struct {
ID string `json:"id"`
Type string `json:"type"`
}
// RelationshipLinks contains links on the given relationship.
type RelationshipLinks struct {
Related *Reference `json:"related,omitempty"`
Self *Reference `json:"self,omitempty"`
}
// relationshipDeclaration represents a declared relationship to a single resource.
type relationshipDeclaration struct {
Data RelationshipData `json:"data"`
}
// pagedRelationshipDeclaration represents a declared relationship to multiple resources.
type pagedRelationshipDeclaration struct {
Data []RelationshipData `json:"data"`
}
func newRelationshipDeclaration(id *string, relationshipType string) *relationshipDeclaration {
if id == nil {
return nil
}
return &relationshipDeclaration{
Data: RelationshipData{
ID: *id,
Type: relationshipType,
},
}
}
func newPagedRelationshipDeclaration(ids []string, relationshipType string) pagedRelationshipDeclaration {
datas := []RelationshipData{}
for _, id := range ids {
datas = append(datas, RelationshipData{
ID: id,
Type: relationshipType,
})
}
return pagedRelationshipDeclaration{Data: datas}
}