-
Notifications
You must be signed in to change notification settings - Fork 1
/
page.go
167 lines (152 loc) · 4.36 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package response
import (
"net/http"
"strings"
"time"
"golang.org/x/exp/slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kubegems.io/library/rest/request"
)
const DefaultPageSize = 10
type Page[T any] struct {
Total int64 `json:"total"`
List []T `json:"list"`
Page int64 `json:"page"`
Size int64 `json:"size"`
}
func PageObjectFromRequest[T any](req *http.Request, list []T) Page[T] {
return PageObjectFromListOptions(list, request.GetListOptions(req))
}
// PageObjectFromListOptions used for client.Object pagination T in list
// use any of T to suit for both eg. Pod(not implement metav1.Object) and *Pod(metav1.Object)
func PageObjectFromListOptions[T any](list []T, opts request.ListOptions) Page[T] {
getname := func(t T) string {
if item, ok := any(t).(interface{ GetName() string }); ok {
return item.GetName()
}
if item, ok := any(&t).(interface{ GetName() string }); ok {
return item.GetName()
}
return ""
}
gettime := func(t T) time.Time {
if item, ok := any(t).(interface{ GetCreationTimestamp() metav1.Time }); ok {
return item.GetCreationTimestamp().Time
}
if item, ok := any(&t).(interface{ GetCreationTimestamp() metav1.Time }); ok {
return item.GetCreationTimestamp().Time
}
return time.Time{}
}
return PageFromListOptions(list, opts, getname, gettime)
}
// PageFromRequest auto pagination from user request on item name or time in list
func PageFromRequest[T any](req *http.Request, list []T, namefunc func(item T) string, timefunc func(item T) time.Time) Page[T] {
return PageFromListOptions(list, request.GetListOptions(req), namefunc, timefunc)
}
func PageFromListOptions[T any](list []T, opts request.ListOptions, namefunc func(item T) string, timefunc func(item T) time.Time) Page[T] {
return PageFrom(list, opts.Page, opts.Size, SearchNameFunc(opts.Search, namefunc), SortByFunc(opts.Sort, namefunc, timefunc))
}
func PageFrom[T any](list []T, page, size int, pickfun func(item T) bool, sortfun func(a, b T) int) Page[T] {
if page < 1 {
page = 1
}
if size < 1 {
size = DefaultPageSize
}
// filter
if pickfun != nil {
datas := []T{}
for _, item := range list {
if pickfun(item) {
datas = append(datas, item)
}
}
list = datas
}
// sort
if sortfun != nil {
slices.SortFunc(list, sortfun)
}
// page
total := len(list)
startIdx := (page - 1) * size
endIdx := startIdx + size
if startIdx > total {
startIdx = 0
endIdx = 0
}
if endIdx > total {
endIdx = total
}
list = list[startIdx:endIdx]
return Page[T]{
Total: int64(total),
List: list,
Page: int64(page),
Size: int64(size),
}
}
func SearchNameFunc[T any](search string, getname func(T) string) func(T) bool {
if getname == nil || search == "" {
return nil
}
return func(item T) bool {
return strings.Contains(getname(item), search)
}
}
func SortByFunc[T any](by string, getname func(T) string, gettime func(T) time.Time) func(a, b T) int {
switch by {
case "createTime", "createTimeAsc", "time":
if gettime == nil {
return nil
}
return func(a, b T) int {
if timcmp := gettime(a).Compare(gettime(b)); timcmp == 0 && getname != nil {
return strings.Compare(getname(a), getname(b))
} else {
return timcmp
}
}
case "createTimeDesc", "time-", "": // default sort by time desc
if gettime == nil {
return nil
}
return func(a, b T) int {
if timcmp := gettime(b).Compare(gettime(a)); timcmp == 0 && getname != nil {
return strings.Compare(getname(a), getname(b))
} else {
return timcmp
}
}
case "name":
if getname == nil {
return nil
}
return func(a, b T) int {
return strings.Compare(getname(a), getname(b))
}
case "nameDesc", "name-":
if getname == nil {
return nil
}
return func(a, b T) int {
return strings.Compare(getname(b), getname(a))
}
default:
return nil
}
}