-
Notifications
You must be signed in to change notification settings - Fork 3
/
page.go
47 lines (44 loc) · 1.29 KB
/
page.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
package slices
import (
"fmt"
"reflect"
)
// Page slice and returns the current page results and hasNext mark.
// Param of arr must be a slice and returns nil slice when: (pageNum - 1) * pageLimit > len(arr).
// Panics when:
// 1. arr type is unexpected type,
// 2. param: pageNum <= 0 or pageLimit <= 0.
func Page(arr interface{}, pageNum, pageLimit int) (interface{}, bool) {
// Check param and type of arr
if pageNum <= 0 {
panic(fmt.Sprintf("pageNum invalid: %d", pageNum))
}
if pageLimit <= 0 {
panic(fmt.Sprintf("pageLimit invalid: %d", pageLimit))
}
arrValue := reflect.ValueOf(arr)
arrKind := arrValue.Kind()
if arrKind != reflect.Slice {
panic(fmt.Sprintf("arr not slice: kind=%s", arrKind))
}
arrLen := arrValue.Len()
if arrLen == 0 {
return reflect.Zero(arrValue.Type()).Interface(), false
}
offset := (pageNum - 1) * pageLimit
if offset >= arrLen {
return reflect.Zero(arrValue.Type()).Interface(), false
}
// Calculate page logic
var hasNext bool
offsetEnd := offset + pageLimit
if offsetEnd < arrLen {
hasNext = true
} else if offsetEnd > arrLen {
offsetEnd = arrLen
}
sliceLen := offsetEnd - offset
retArr := reflect.MakeSlice(arrValue.Type(), sliceLen, sliceLen)
reflect.Copy(retArr, arrValue.Slice(offset, offsetEnd))
return retArr.Interface(), hasNext
}