-
Notifications
You must be signed in to change notification settings - Fork 2
/
paging.go
53 lines (44 loc) · 1.13 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
package navitia
import (
"context"
"encoding/json"
"github.com/pkg/errors"
)
// Paging holds potential Previous / Next functions
type Paging struct {
// Next results
Next func(ctx context.Context, s *Session, res results) error
// Previous results
Previous func(ctx context.Context, s *Session, res results) error
}
type link struct {
Href string
Rel string
Templated bool
Type string
}
// createPagingFunc creates a paging func (either Previous or Next)
func createPagingFunc(url string) func(ctx context.Context, s *Session, res results) error {
f := func(ctx context.Context, s *Session, res results) error {
return s.requestURL(ctx, url, res)
}
return f
}
// UnmarshalJSON unmarshals a Paging type from a Links data structure
func (p *Paging) UnmarshalJSON(b []byte) error {
var links []link
err := json.Unmarshal(b, &links)
if err != nil {
return errors.Wrap(err, "error while unmarshalling links")
}
// Iterate through the links
for _, l := range links {
switch l.Type {
case "next":
p.Next = createPagingFunc(l.Href)
case "previous":
p.Previous = createPagingFunc(l.Href)
}
}
return nil
}