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

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