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

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