github.com/cloudwan/edgelq-sdk@v1.15.4/devices/resources/v1/device/device.pb.pagination.go (about) 1 // Code generated by protoc-gen-goten-resource 2 // Resource: Device 3 // DO NOT EDIT!!! 4 5 package device 6 7 import ( 8 "fmt" 9 "reflect" 10 "sort" 11 "strings" 12 13 "google.golang.org/grpc/codes" 14 "google.golang.org/grpc/status" 15 16 gotenobject "github.com/cloudwan/goten-sdk/runtime/object" 17 gotenresource "github.com/cloudwan/goten-sdk/runtime/resource" 18 ) 19 20 // proto imports 21 import ( 22 api "github.com/cloudwan/edgelq-sdk/common/api" 23 project "github.com/cloudwan/edgelq-sdk/devices/resources/v1/project" 24 iam_attestation_domain "github.com/cloudwan/edgelq-sdk/iam/resources/v1/attestation_domain" 25 iam_iam_common "github.com/cloudwan/edgelq-sdk/iam/resources/v1/common" 26 iam_service_account "github.com/cloudwan/edgelq-sdk/iam/resources/v1/service_account" 27 logging_bucket "github.com/cloudwan/edgelq-sdk/logging/resources/v1/bucket" 28 monitoring_bucket "github.com/cloudwan/edgelq-sdk/monitoring/resources/v4/bucket" 29 meta "github.com/cloudwan/goten-sdk/types/meta" 30 latlng "google.golang.org/genproto/googleapis/type/latlng" 31 durationpb "google.golang.org/protobuf/types/known/durationpb" 32 fieldmaskpb "google.golang.org/protobuf/types/known/fieldmaskpb" 33 timestamppb "google.golang.org/protobuf/types/known/timestamppb" 34 ) 35 36 // make sure we're using proto imports 37 var ( 38 _ = &api.HealthCheckSpec{} 39 _ = &project.Project{} 40 _ = &iam_attestation_domain.AttestationDomain{} 41 _ = &iam_iam_common.PCR{} 42 _ = &iam_service_account.ServiceAccount{} 43 _ = &logging_bucket.Bucket{} 44 _ = &monitoring_bucket.Bucket{} 45 _ = &durationpb.Duration{} 46 _ = &fieldmaskpb.FieldMask{} 47 _ = ×tamppb.Timestamp{} 48 _ = &latlng.LatLng{} 49 _ = &meta.Meta{} 50 ) 51 52 // OrderByField is single item in order_by specification 53 // it's string format is composed of 2 white-space separated values: 54 // - fieldPath and direction, e.g. "state.capacity desc". 55 // if direction is not provided, it defaults to "asc" (ascending) 56 type OrderByField struct { 57 FieldPath Device_FieldPath 58 Direction gotenresource.OrderDirection 59 } 60 61 func (orderByFld *OrderByField) GetDirection() gotenresource.OrderDirection { 62 return orderByFld.Direction 63 } 64 65 func (orderByFld *OrderByField) GetFieldPath() gotenobject.FieldPath { 66 return orderByFld.FieldPath 67 } 68 69 func (orderByFld *OrderByField) CompareWithDirection(left, right *Device) int { 70 leftValue, leftPresent := orderByFld.FieldPath.GetSingle(left) 71 _, rightPresent := orderByFld.FieldPath.GetSingle(right) 72 if leftPresent != rightPresent { 73 if orderByFld.Direction == gotenresource.DirectionASC { 74 if !leftPresent { 75 return -1 76 } else { 77 return 1 78 } 79 } else { 80 if leftPresent { 81 return -1 82 } else { 83 return 1 84 } 85 } 86 } 87 if !leftPresent { 88 return 0 89 } 90 leftFpv := orderByFld.FieldPath.WithIValue(leftValue) 91 result, comparable := leftFpv.CompareWith(right) 92 if comparable && result != 0 { 93 if orderByFld.Direction == gotenresource.DirectionASC { 94 return result 95 } else if result > 0 { 96 return -1 97 } else { 98 return 1 99 } 100 } 101 return 0 102 } 103 104 // OrderBy Is string encoded Custom Protobuf type, which handles "order_by" field 105 // order_by consists of coma delimited OrderBy specs, which denote ordering priority, 106 // e.g. "state.value asc, state.capacity desc" 107 type OrderBy struct { 108 OrderByFields []OrderByField 109 } 110 111 func (orderBy *OrderBy) String() string { 112 if orderBy == nil { 113 return "<nil>" 114 } 115 if valueStr, err := orderBy.ProtoString(); err != nil { 116 panic(err) 117 } else { 118 return valueStr 119 } 120 } 121 122 // implement methods required by protobuf-go library for string-struct conversion 123 124 func (orderBy *OrderBy) ProtoString() (string, error) { 125 if orderBy == nil { 126 return "", nil 127 } 128 var results []string 129 for _, orderByField := range orderBy.OrderByFields { 130 results = append(results, fmt.Sprintf("%s %s", orderByField.FieldPath, orderByField.Direction)) 131 } 132 return strings.Join(results, ","), nil 133 } 134 135 func (orderBy *OrderBy) ParseProtoString(data string) error { 136 groups := strings.Split(data, ",") 137 orderBys := make([]OrderByField, 0, len(groups)) 138 for _, orderByStr := range groups { 139 orderByField, err := orderBy.parseOrderByField(orderByStr) 140 if err != nil { 141 return err 142 } 143 orderBys = append(orderBys, orderByField) 144 } 145 orderBy.OrderByFields = orderBys 146 return nil 147 } 148 149 func (orderBy *OrderBy) Sort(results DeviceList) { 150 if orderBy == nil { 151 return 152 } 153 sort.Slice(results, func(i, j int) bool { 154 left, right := results[i], results[j] 155 for _, fld := range orderBy.OrderByFields { 156 compareResult := fld.CompareWithDirection(left, right) 157 if compareResult != 0 { 158 return compareResult < 0 159 } 160 } 161 return false 162 }) 163 } 164 165 func (orderBy *OrderBy) SortRaw(results gotenresource.ResourceList) { 166 orderBy.Sort(results.(DeviceList)) 167 } 168 169 func (orderBy *OrderBy) InsertSorted(sorted DeviceList, elem *Device) (DeviceList, int) { 170 if orderBy == nil { 171 return append(sorted, elem), len(sorted) 172 } 173 i := sort.Search(len(sorted), func(i int) bool { 174 comparedTo := sorted[i] 175 for _, fld := range orderBy.OrderByFields { 176 compareResult := fld.CompareWithDirection(elem, comparedTo) 177 if compareResult != 0 { 178 return compareResult < 0 179 } 180 } 181 return false 182 }) 183 sorted = append(sorted, elem) 184 copy(sorted[i+1:], sorted[i:]) 185 sorted[i] = elem 186 return sorted, i 187 } 188 189 func (orderBy *OrderBy) InsertSortedRaw(sorted gotenresource.ResourceList, elem gotenresource.Resource) (gotenresource.ResourceList, int) { 190 return orderBy.InsertSorted(sorted.(DeviceList), elem.(*Device)) 191 } 192 193 func (orderBy *OrderBy) Compare(left, right *Device) int { 194 if orderBy == nil { 195 return 0 196 } 197 for _, fld := range orderBy.OrderByFields { 198 compareResult := fld.CompareWithDirection(left, right) 199 if compareResult != 0 { 200 return compareResult 201 } 202 } 203 return 0 204 } 205 206 func (orderBy *OrderBy) CompareRaw(left, right gotenresource.Resource) int { 207 return orderBy.Compare(left.(*Device), right.(*Device)) 208 } 209 210 func (orderBy *OrderBy) parseOrderByField(orderByStr string) (OrderByField, error) { 211 split := strings.Fields(orderByStr) 212 if len(split) > 2 || len(split) == 0 { 213 return OrderByField{}, status.Errorf(codes.InvalidArgument, "string '%s' doesn't parse as order by query specifier", orderByStr) 214 } 215 // parse direction 216 direction := gotenresource.DirectionASC 217 if len(split) > 1 { 218 var err error 219 if direction, err = gotenresource.OrderDirectionFromString(split[1]); err != nil { 220 return OrderByField{}, err 221 } 222 } 223 // parse field path 224 fp, err := ParseDevice_FieldPath(split[0]) 225 if err != nil { 226 return OrderByField{}, err 227 } 228 229 return OrderByField{ 230 FieldPath: fp, 231 Direction: direction, 232 }, nil 233 } 234 235 func (orderBy *OrderBy) SetFromCliFlag(raw string) error { 236 field, err := orderBy.parseOrderByField(raw) 237 if err != nil { 238 return err 239 } 240 orderBy.OrderByFields = append(orderBy.OrderByFields, field) 241 return nil 242 } 243 244 func (orderBy *OrderBy) GetOrderByFields() []gotenresource.OrderByField { 245 if orderBy == nil { 246 return nil 247 } 248 249 fields := make([]gotenresource.OrderByField, len(orderBy.OrderByFields)) 250 for idx := range orderBy.OrderByFields { 251 fields[idx] = &orderBy.OrderByFields[idx] 252 } 253 return fields 254 } 255 256 func (orderBy *OrderBy) GetFieldMask() *Device_FieldMask { 257 fieldMask := &Device_FieldMask{} 258 if orderBy == nil { 259 return fieldMask 260 } 261 for _, orderByField := range orderBy.OrderByFields { 262 fieldMask.Paths = append(fieldMask.Paths, orderByField.FieldPath) 263 } 264 return fieldMask 265 } 266 267 func (orderBy *OrderBy) GetRawFieldMask() gotenobject.FieldMask { 268 return orderBy.GetFieldMask() 269 } 270 271 // PagerCursor is protobuf Custom Type, which (de)serializes "string page_token" for API List processing 272 // Database adapter implementation must use this cursor when Paginating list views 273 // Token is composed of 3 values (dot separated in serialized form) 274 // - CursorValue: Backend-specific value of the cursor. 275 // - PageDirection: either l (left) or r (right), which hints DB Adapter whether Snapshot marks Start or End of result 276 // - Inclusion: either i (inclusive) or e (exclusive) - Whether cursor marks exact point or right before/after (depending on direction) 277 type PagerCursor struct { 278 CursorValue gotenresource.CursorValue 279 Inclusion gotenresource.CursorInclusion 280 PageDirection gotenresource.PageDirection 281 } 282 283 func (cursor *PagerCursor) String() string { 284 if cursor == nil { 285 return "<nil>" 286 } 287 if valueStr, err := cursor.ProtoString(); err != nil { 288 panic(err) 289 } else { 290 return valueStr 291 } 292 } 293 294 func (cursor *PagerCursor) IsEmpty() bool { 295 return cursor == nil || cursor.CursorValue == nil || reflect.ValueOf(cursor.CursorValue).IsNil() 296 } 297 298 // implement methods required by protobuf-go library for string-struct conversion 299 300 func (cursor *PagerCursor) ProtoString() (string, error) { 301 if cursor.IsEmpty() { 302 return "", nil 303 } 304 return fmt.Sprintf("%s.%s.%s.%s", 305 cursor.PageDirection, cursor.Inclusion, cursor.CursorValue.GetValueType(), cursor.CursorValue.String()), nil 306 } 307 308 func (cursor *PagerCursor) ParseProtoString(data string) (err error) { 309 split := strings.Split(data, ".") 310 if len(split) != 4 { 311 return status.Errorf(codes.InvalidArgument, "invalid cursor format: '%s'", data) 312 } 313 if cursor.PageDirection, err = gotenresource.PageDirectionFromString(split[0]); err != nil { 314 return err 315 } 316 if cursor.Inclusion, err = gotenresource.CursorInclusionFromString(split[1]); err != nil { 317 return err 318 } 319 switch gotenresource.CursorValueType(split[2]) { 320 case gotenresource.CustomCursorValueType: 321 cursor.CursorValue, err = gotenresource.ParseCustomCursorValue(split[3]) 322 if err != nil { 323 return err 324 } 325 case gotenresource.OffsetCursorValueType: 326 cursor.CursorValue, err = gotenresource.ParseOffsetCursorValue(split[3]) 327 if err != nil { 328 return err 329 } 330 case gotenresource.SnapshotCursorValueType: 331 cursor.CursorValue, err = gotenresource.ParseSnapshotCursorValue(GetDescriptor(), split[3]) 332 if err != nil { 333 return err 334 } 335 default: 336 return status.Errorf(codes.InvalidArgument, "invalid cursor value type: '%s'", split[2]) 337 } 338 return nil 339 } 340 341 func (cursor *PagerCursor) SetFromCliFlag(raw string) error { 342 return cursor.ParseProtoString(raw) 343 } 344 345 func (cursor *PagerCursor) GetPageDirection() gotenresource.PageDirection { 346 if cursor == nil { 347 return "" 348 } 349 return cursor.PageDirection 350 } 351 352 func (cursor *PagerCursor) GetInclusion() gotenresource.CursorInclusion { 353 if cursor == nil { 354 return "" 355 } 356 return cursor.Inclusion 357 } 358 359 func (cursor *PagerCursor) GetValue() gotenresource.CursorValue { 360 if cursor == nil { 361 return nil 362 } 363 return cursor.CursorValue 364 } 365 366 func (cursor *PagerCursor) SetPageDirection(direction gotenresource.PageDirection) { 367 cursor.PageDirection = direction 368 } 369 370 func (cursor *PagerCursor) SetInclusion(inclusion gotenresource.CursorInclusion) { 371 cursor.Inclusion = inclusion 372 } 373 374 func (cursor *PagerCursor) SetCursorValue(value gotenresource.CursorValue) { 375 cursor.CursorValue = value 376 } 377 378 // PagerQuery is main struct used for assisting server and database to perform Pagination 379 type PagerQuery struct { 380 OrderBy *OrderBy 381 Cursor *PagerCursor 382 Limit int 383 PeekForward bool 384 } 385 386 // MakePagerQuery builds pager from API data and applies defaults 387 func MakePagerQuery(orderBy *OrderBy, cursor *PagerCursor, pageSize int32, peekForward bool) *PagerQuery { 388 if pageSize == 0 { 389 pageSize = 100 390 } 391 if orderBy == nil { 392 orderBy = &OrderBy{} 393 } 394 hasName := false 395 for _, orderByField := range orderBy.OrderByFields { 396 if orderByField.FieldPath.Selector() == Device_FieldPathSelectorName { 397 hasName = true 398 } 399 } 400 if !hasName { 401 nameDirection := gotenresource.DirectionASC 402 if len(orderBy.OrderByFields) > 0 { 403 lastOrderBy := orderBy.OrderByFields[len(orderBy.OrderByFields)-1] 404 nameDirection = lastOrderBy.Direction 405 } 406 orderBy.OrderByFields = append(orderBy.OrderByFields, OrderByField{ 407 FieldPath: &Device_FieldTerminalPath{selector: Device_FieldPathSelectorName}, 408 Direction: nameDirection, 409 }) 410 } 411 412 return &PagerQuery{ 413 OrderBy: orderBy, 414 Cursor: cursor, 415 Limit: int(pageSize), 416 PeekForward: peekForward, 417 } 418 } 419 420 func (p *PagerQuery) GetOrderBy() gotenresource.OrderBy { 421 if p == nil { 422 return (*OrderBy)(nil) 423 } 424 return p.OrderBy 425 } 426 427 func (p *PagerQuery) GetCursor() gotenresource.Cursor { 428 if p == nil { 429 return (*PagerCursor)(nil) 430 } 431 return p.Cursor 432 } 433 434 func (p *PagerQuery) GetLimit() int { 435 if p == nil { 436 return 0 437 } 438 return p.Limit 439 } 440 441 func (p *PagerQuery) GetPeekForward() bool { 442 if p == nil { 443 return false 444 } 445 return p.PeekForward 446 } 447 448 func (p *PagerQuery) PageDirection() gotenresource.PageDirection { 449 if p == nil || p.Cursor == nil { 450 return gotenresource.PageRight 451 } else { 452 return p.Cursor.PageDirection 453 } 454 } 455 456 func (p *PagerQuery) SetCursor(cursor gotenresource.Cursor) { 457 if cursor == nil { 458 p.Cursor = nil 459 } else { 460 p.Cursor = cursor.(*PagerCursor) 461 } 462 } 463 464 func (p *PagerQuery) SetOrderBy(orderBy gotenresource.OrderBy) { 465 if orderBy == nil { 466 p.OrderBy = nil 467 } else { 468 p.OrderBy = orderBy.(*OrderBy) 469 } 470 } 471 472 func (p *PagerQuery) SetLimit(limit int) { 473 p.Limit = limit 474 } 475 476 func (p *PagerQuery) SetPeekForward(peekForward bool) { 477 p.PeekForward = peekForward 478 } 479 480 func (p *PagerQuery) SetPageDirection(direction gotenresource.PageDirection) { 481 p.Cursor.PageDirection = direction 482 } 483 484 func (p *PagerQuery) GetResourceDescriptor() gotenresource.Descriptor { 485 return descriptor 486 }