github.com/cloudwan/edgelq-sdk@v1.15.4/limits/resources/v1alpha2/limit_pool/limit_pool.pb.pagination.go (about)

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