github.com/cloudwan/edgelq-sdk@v1.15.4/devices/resources/v1/customized_image/customized_image.pb.pagination.go (about)

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