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

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