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