-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonapi.go
57 lines (45 loc) · 1.11 KB
/
jsonapi.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
package nrdb
import (
"fmt"
"net/url"
)
type Links struct {
Self *string `json:"self,omitempty"`
Related *string `json:"related,omitempty"`
First *string `json:"first,omitempty"`
Last *string `json:"last,omitempty"`
Previous *string `json:"prev,omitempty"`
Next *string `json:"next,omitempty"`
}
type Relationship struct {
Links *Links `json:"links"`
}
type Response[T any] struct {
Data T `json:"data"`
Links *Links `json:"links"`
}
type Document[A any, R any] struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes *A `json:"attributes"`
Links *Links `json:"links"`
Relationships *R `json:"relationships"`
}
type Params struct {
PageSize *uint64
PageNumber *uint64
}
func (p Params) SetPageInfo(query url.Values) url.Values {
if p.PageSize != nil {
query.Set("page[size]", fmt.Sprintf("%d", *p.PageSize))
}
if p.PageNumber != nil {
query.Set("page[number]", fmt.Sprintf("%d", *p.PageNumber))
}
return query
}
func (p Params) Query() (url.Values, error) {
query := url.Values{}
p.SetPageInfo(query)
return query, nil
}