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