-
Notifications
You must be signed in to change notification settings - Fork 19
/
paging.go
82 lines (70 loc) · 1.92 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
package model
import (
"fmt"
"reflect"
)
// The paging object used to hold information
type Paging struct {
Size int32
Position int32
TotalCount int32
PageMore bool
OrderBy []*OrderByEntity
}
// Initialize the paging with default values
//
// Paging.Size: -1
// Paging.Position: -1
// Paging.TotalCount: -1
// Paging.PageMore: false
func NewUndefinedPaging() *Paging {
return &Paging{
Size: -1,
Position: -1,
TotalCount: -1,
PageMore: false,
}
}
// Gets the offset to be skipped
func (self *Paging) GetOffset() int32 {
if self.Position <= 1 {
return 0
}
return (self.Position - 1) * self.Size
}
// Shows the information of paging
func (self *Paging) String() string {
return fmt.Sprintf(
"Page Size:[%d]. Page Position:[%d]. Total Count:[%d]. Has More:[%v]. Order By: [%v]",
self.Size, self.Position, self.TotalCount, self.PageMore, self.OrderBy,
)
}
// Sets the total count of data and set-up PageMore flag
func (self *Paging) SetTotalCount(totalCount int32) {
self.TotalCount = totalCount
self.PageMore = self.GetOffset()+self.Size < totalCount
}
// Extracts page from array or slice
//
// This function would call Paging.SetTotalCount function on input paging
func ExtractPage(arrayObject interface{}, paging *Paging) interface{} {
arrayValue := reflect.ValueOf(arrayObject)
if arrayValue.Kind() != reflect.Array &&
arrayValue.Kind() != reflect.Slice {
panic(fmt.Sprintf("Input value should be array or slice, got %T", arrayObject))
}
resultSlice := reflect.MakeSlice(arrayValue.Type(), 0, 0)
/**
* Calculates the boundary of indexes on input array
*/
startIndex, endIndex := int(paging.GetOffset()), int(paging.GetOffset()+paging.Size)
lenOfArray := arrayValue.Len()
if endIndex > lenOfArray {
endIndex = lenOfArray
}
// :~)
for i := startIndex; i < endIndex; i++ {
resultSlice = reflect.Append(resultSlice, arrayValue.Index(i))
}
return resultSlice.Interface()
}