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