-
Notifications
You must be signed in to change notification settings - Fork 1
/
plan_assignment_request.pb.pagination.go
476 lines (421 loc) · 12.7 KB
/
plan_assignment_request.pb.pagination.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Code generated by protoc-gen-goten-resource
// Resource: PlanAssignmentRequest
// DO NOT EDIT!!!
package plan_assignment_request
import (
"fmt"
"reflect"
"sort"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
gotenobject "github.com/cloudwan/goten-sdk/runtime/object"
gotenresource "github.com/cloudwan/goten-sdk/runtime/resource"
)
// proto imports
import (
iam_organization "github.com/cloudwan/edgelq-sdk/iam/resources/v1alpha2/organization"
iam_project "github.com/cloudwan/edgelq-sdk/iam/resources/v1alpha2/project"
common "github.com/cloudwan/edgelq-sdk/limits/resources/v1alpha2/common"
plan "github.com/cloudwan/edgelq-sdk/limits/resources/v1alpha2/plan"
plan_assignment "github.com/cloudwan/edgelq-sdk/limits/resources/v1alpha2/plan_assignment"
meta_service "github.com/cloudwan/edgelq-sdk/meta/resources/v1alpha2/service"
meta "github.com/cloudwan/goten-sdk/types/meta"
)
// make sure we're using proto imports
var (
_ = &iam_organization.Organization{}
_ = &iam_project.Project{}
_ = &common.Allowance{}
_ = &plan.Plan{}
_ = &plan_assignment.PlanAssignment{}
_ = &meta_service.Service{}
_ = &meta.Meta{}
)
// OrderByField is single item in order_by specification
// it's string format is composed of 2 white-space separated values:
// - fieldPath and direction, e.g. "state.capacity desc".
// if direction is not provided, it defaults to "asc" (ascending)
type OrderByField struct {
FieldPath PlanAssignmentRequest_FieldPath
Direction gotenresource.OrderDirection
}
func (orderByFld *OrderByField) GetDirection() gotenresource.OrderDirection {
return orderByFld.Direction
}
func (orderByFld *OrderByField) GetFieldPath() gotenobject.FieldPath {
return orderByFld.FieldPath
}
func (orderByFld *OrderByField) CompareWithDirection(left, right *PlanAssignmentRequest) int {
leftValue, leftPresent := orderByFld.FieldPath.GetSingle(left)
_, rightPresent := orderByFld.FieldPath.GetSingle(right)
if leftPresent != rightPresent {
if orderByFld.Direction == gotenresource.DirectionASC {
if !leftPresent {
return -1
} else {
return 1
}
} else {
if leftPresent {
return -1
} else {
return 1
}
}
}
if !leftPresent {
return 0
}
leftFpv := orderByFld.FieldPath.WithIValue(leftValue)
result, comparable := leftFpv.CompareWith(right)
if comparable && result != 0 {
if orderByFld.Direction == gotenresource.DirectionASC {
return result
} else if result > 0 {
return -1
} else {
return 1
}
}
return 0
}
// OrderBy Is string encoded Custom Protobuf type, which handles "order_by" field
// order_by consists of coma delimited OrderBy specs, which denote ordering priority,
// e.g. "state.value asc, state.capacity desc"
type OrderBy struct {
OrderByFields []OrderByField
}
func (orderBy *OrderBy) String() string {
if orderBy == nil {
return "<nil>"
}
if valueStr, err := orderBy.ProtoString(); err != nil {
panic(err)
} else {
return valueStr
}
}
// implement methods required by protobuf-go library for string-struct conversion
func (orderBy *OrderBy) ProtoString() (string, error) {
if orderBy == nil {
return "", nil
}
var results []string
for _, orderByField := range orderBy.OrderByFields {
results = append(results, fmt.Sprintf("%s %s", orderByField.FieldPath, orderByField.Direction))
}
return strings.Join(results, ","), nil
}
func (orderBy *OrderBy) ParseProtoString(data string) error {
groups := strings.Split(data, ",")
orderBys := make([]OrderByField, 0, len(groups))
for _, orderByStr := range groups {
orderByField, err := orderBy.parseOrderByField(orderByStr)
if err != nil {
return err
}
orderBys = append(orderBys, orderByField)
}
orderBy.OrderByFields = orderBys
return nil
}
func (orderBy *OrderBy) Sort(results PlanAssignmentRequestList) {
if orderBy == nil {
return
}
sort.Slice(results, func(i, j int) bool {
left, right := results[i], results[j]
for _, fld := range orderBy.OrderByFields {
compareResult := fld.CompareWithDirection(left, right)
if compareResult != 0 {
return compareResult < 0
}
}
return false
})
}
func (orderBy *OrderBy) SortRaw(results gotenresource.ResourceList) {
orderBy.Sort(results.(PlanAssignmentRequestList))
}
func (orderBy *OrderBy) InsertSorted(sorted PlanAssignmentRequestList, elem *PlanAssignmentRequest) (PlanAssignmentRequestList, int) {
if orderBy == nil {
return append(sorted, elem), len(sorted)
}
i := sort.Search(len(sorted), func(i int) bool {
comparedTo := sorted[i]
for _, fld := range orderBy.OrderByFields {
compareResult := fld.CompareWithDirection(elem, comparedTo)
if compareResult != 0 {
return compareResult < 0
}
}
return false
})
sorted = append(sorted, elem)
copy(sorted[i+1:], sorted[i:])
sorted[i] = elem
return sorted, i
}
func (orderBy *OrderBy) InsertSortedRaw(sorted gotenresource.ResourceList, elem gotenresource.Resource) (gotenresource.ResourceList, int) {
return orderBy.InsertSorted(sorted.(PlanAssignmentRequestList), elem.(*PlanAssignmentRequest))
}
func (orderBy *OrderBy) Compare(left, right *PlanAssignmentRequest) int {
if orderBy == nil {
return 0
}
for _, fld := range orderBy.OrderByFields {
compareResult := fld.CompareWithDirection(left, right)
if compareResult != 0 {
return compareResult
}
}
return 0
}
func (orderBy *OrderBy) CompareRaw(left, right gotenresource.Resource) int {
return orderBy.Compare(left.(*PlanAssignmentRequest), right.(*PlanAssignmentRequest))
}
func (orderBy *OrderBy) parseOrderByField(orderByStr string) (OrderByField, error) {
split := strings.Fields(orderByStr)
if len(split) > 2 || len(split) == 0 {
return OrderByField{}, status.Errorf(codes.InvalidArgument, "string '%s' doesn't parse as order by query specifier", orderByStr)
}
// parse direction
direction := gotenresource.DirectionASC
if len(split) > 1 {
var err error
if direction, err = gotenresource.OrderDirectionFromString(split[1]); err != nil {
return OrderByField{}, err
}
}
// parse field path
fp, err := ParsePlanAssignmentRequest_FieldPath(split[0])
if err != nil {
return OrderByField{}, err
}
return OrderByField{
FieldPath: fp,
Direction: direction,
}, nil
}
func (orderBy *OrderBy) SetFromCliFlag(raw string) error {
field, err := orderBy.parseOrderByField(raw)
if err != nil {
return err
}
orderBy.OrderByFields = append(orderBy.OrderByFields, field)
return nil
}
func (orderBy *OrderBy) GetOrderByFields() []gotenresource.OrderByField {
if orderBy == nil {
return nil
}
fields := make([]gotenresource.OrderByField, len(orderBy.OrderByFields))
for idx := range orderBy.OrderByFields {
fields[idx] = &orderBy.OrderByFields[idx]
}
return fields
}
func (orderBy *OrderBy) GetFieldMask() *PlanAssignmentRequest_FieldMask {
fieldMask := &PlanAssignmentRequest_FieldMask{}
if orderBy == nil {
return fieldMask
}
for _, orderByField := range orderBy.OrderByFields {
fieldMask.Paths = append(fieldMask.Paths, orderByField.FieldPath)
}
return fieldMask
}
func (orderBy *OrderBy) GetRawFieldMask() gotenobject.FieldMask {
return orderBy.GetFieldMask()
}
// PagerCursor is protobuf Custom Type, which (de)serializes "string page_token" for API List processing
// Database adapter implementation must use this cursor when Paginating list views
// Token is composed of 3 values (dot separated in serialized form)
// - CursorValue: Backend-specific value of the cursor.
// - PageDirection: either l (left) or r (right), which hints DB Adapter whether Snapshot marks Start or End of result
// - Inclusion: either i (inclusive) or e (exclusive) - Whether cursor marks exact point or right before/after (depending on direction)
type PagerCursor struct {
CursorValue gotenresource.CursorValue
Inclusion gotenresource.CursorInclusion
PageDirection gotenresource.PageDirection
}
func (cursor *PagerCursor) String() string {
if cursor == nil {
return "<nil>"
}
if valueStr, err := cursor.ProtoString(); err != nil {
panic(err)
} else {
return valueStr
}
}
func (cursor *PagerCursor) IsEmpty() bool {
return cursor == nil || cursor.CursorValue == nil || reflect.ValueOf(cursor.CursorValue).IsNil()
}
// implement methods required by protobuf-go library for string-struct conversion
func (cursor *PagerCursor) ProtoString() (string, error) {
if cursor.IsEmpty() {
return "", nil
}
return fmt.Sprintf("%s.%s.%s.%s",
cursor.PageDirection, cursor.Inclusion, cursor.CursorValue.GetValueType(), cursor.CursorValue.String()), nil
}
func (cursor *PagerCursor) ParseProtoString(data string) (err error) {
split := strings.Split(data, ".")
if len(split) != 4 {
return status.Errorf(codes.InvalidArgument, "invalid cursor format: '%s'", data)
}
if cursor.PageDirection, err = gotenresource.PageDirectionFromString(split[0]); err != nil {
return err
}
if cursor.Inclusion, err = gotenresource.CursorInclusionFromString(split[1]); err != nil {
return err
}
switch gotenresource.CursorValueType(split[2]) {
case gotenresource.CustomCursorValueType:
cursor.CursorValue, err = gotenresource.ParseCustomCursorValue(split[3])
if err != nil {
return err
}
case gotenresource.OffsetCursorValueType:
cursor.CursorValue, err = gotenresource.ParseOffsetCursorValue(split[3])
if err != nil {
return err
}
case gotenresource.SnapshotCursorValueType:
cursor.CursorValue, err = gotenresource.ParseSnapshotCursorValue(GetDescriptor(), split[3])
if err != nil {
return err
}
default:
return status.Errorf(codes.InvalidArgument, "invalid cursor value type: '%s'", split[2])
}
return nil
}
func (cursor *PagerCursor) SetFromCliFlag(raw string) error {
return cursor.ParseProtoString(raw)
}
func (cursor *PagerCursor) GetPageDirection() gotenresource.PageDirection {
if cursor == nil {
return ""
}
return cursor.PageDirection
}
func (cursor *PagerCursor) GetInclusion() gotenresource.CursorInclusion {
if cursor == nil {
return ""
}
return cursor.Inclusion
}
func (cursor *PagerCursor) GetValue() gotenresource.CursorValue {
if cursor == nil {
return nil
}
return cursor.CursorValue
}
func (cursor *PagerCursor) SetPageDirection(direction gotenresource.PageDirection) {
cursor.PageDirection = direction
}
func (cursor *PagerCursor) SetInclusion(inclusion gotenresource.CursorInclusion) {
cursor.Inclusion = inclusion
}
func (cursor *PagerCursor) SetCursorValue(value gotenresource.CursorValue) {
cursor.CursorValue = value
}
// PagerQuery is main struct used for assisting server and database to perform Pagination
type PagerQuery struct {
OrderBy *OrderBy
Cursor *PagerCursor
Limit int
PeekForward bool
}
// MakePagerQuery builds pager from API data and applies defaults
func MakePagerQuery(orderBy *OrderBy, cursor *PagerCursor, pageSize int32, peekForward bool) *PagerQuery {
if pageSize == 0 {
pageSize = 100
}
if orderBy == nil {
orderBy = &OrderBy{}
}
hasName := false
for _, orderByField := range orderBy.OrderByFields {
if orderByField.FieldPath.Selector() == PlanAssignmentRequest_FieldPathSelectorName {
hasName = true
}
}
if !hasName {
nameDirection := gotenresource.DirectionASC
if len(orderBy.OrderByFields) > 0 {
lastOrderBy := orderBy.OrderByFields[len(orderBy.OrderByFields)-1]
nameDirection = lastOrderBy.Direction
}
orderBy.OrderByFields = append(orderBy.OrderByFields, OrderByField{
FieldPath: &PlanAssignmentRequest_FieldTerminalPath{selector: PlanAssignmentRequest_FieldPathSelectorName},
Direction: nameDirection,
})
}
return &PagerQuery{
OrderBy: orderBy,
Cursor: cursor,
Limit: int(pageSize),
PeekForward: peekForward,
}
}
func (p *PagerQuery) GetOrderBy() gotenresource.OrderBy {
if p == nil {
return (*OrderBy)(nil)
}
return p.OrderBy
}
func (p *PagerQuery) GetCursor() gotenresource.Cursor {
if p == nil {
return (*PagerCursor)(nil)
}
return p.Cursor
}
func (p *PagerQuery) GetLimit() int {
if p == nil {
return 0
}
return p.Limit
}
func (p *PagerQuery) GetPeekForward() bool {
if p == nil {
return false
}
return p.PeekForward
}
func (p *PagerQuery) PageDirection() gotenresource.PageDirection {
if p == nil || p.Cursor == nil {
return gotenresource.PageRight
} else {
return p.Cursor.PageDirection
}
}
func (p *PagerQuery) SetCursor(cursor gotenresource.Cursor) {
if cursor == nil {
p.Cursor = nil
} else {
p.Cursor = cursor.(*PagerCursor)
}
}
func (p *PagerQuery) SetOrderBy(orderBy gotenresource.OrderBy) {
if orderBy == nil {
p.OrderBy = nil
} else {
p.OrderBy = orderBy.(*OrderBy)
}
}
func (p *PagerQuery) SetLimit(limit int) {
p.Limit = limit
}
func (p *PagerQuery) SetPeekForward(peekForward bool) {
p.PeekForward = peekForward
}
func (p *PagerQuery) SetPageDirection(direction gotenresource.PageDirection) {
p.Cursor.PageDirection = direction
}
func (p *PagerQuery) GetResourceDescriptor() gotenresource.Descriptor {
return descriptor
}