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