-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
46 lines (34 loc) · 884 Bytes
/
example_test.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
package pagination_test
import (
"encoding/json"
"fmt"
"github.com/zheeeng/pagination"
)
type Book struct {
ID int `json:"id"`
Author string `json:"author"`
Name string `json:"name"`
}
type TrunctableBooks []Book
func (tb TrunctableBooks) Slice(startIndex, endIndex int) pagination.Truncatable {
return tb[startIndex:endIndex]
}
func (tb TrunctableBooks) Len() int {
return len(tb)
}
var total = 20
var requestURI = "api.example.com/books?author=jk&page=2&page_size=5"
var books = []Book{}
func init() {
for i := 0; i < 20; i++ {
book := Book{i, "jk", "book"}
books = append(books, book)
}
}
func Example() {
pg := pagination.DefaultPagination()
pgt := pg.Parse(requestURI)
paginatedData := pgt.WrapWithTruncate(TrunctableBooks(books), total)
responseBody, _ := json.MarshalIndent(paginatedData, "", " ")
fmt.Println(string(responseBody))
}