Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultList[T any](objects []T, q *Query, compareFunc CompareFunc[T], filterFunc FilterFunc[T], ...) *api.ListResult[T]
- func DefaultObjectMetaCompare(left, right metav1.Object, sortBy string) bool
- func DefaultObjectMetaFilter(item metav1.Object, filter Filter) bool
- func DefaultString(value, defaultValue string) string
- func GetFieldByJSONTag(obj reflect.Value, filed string) reflect.Value
- type CompareFunc
- type Filter
- type FilterFunc
- type Pagination
- type Query
- type TransformFunc
Constants ¶
const ( // FieldName represents the name field of a resource FieldName = "name" // FieldNames represents multiple names field of resources FieldNames = "names" // FieldUID represents the unique identifier field of a resource FieldUID = "uid" // FieldCreationTimeStamp represents the creation timestamp field of a resource FieldCreationTimeStamp = "creationTimestamp" // FieldCreateTime represents the creation time field of a resource FieldCreateTime = "createTime" // FieldLastUpdateTimestamp represents the last update timestamp field of a resource FieldLastUpdateTimestamp = "lastUpdateTimestamp" // FieldUpdateTime represents the update time field of a resource FieldUpdateTime = "updateTime" // FieldLabel represents the label field of a resource FieldLabel = "label" // FieldAnnotation represents the annotation field of a resource FieldAnnotation = "annotation" // FieldNamespace represents the namespace field of a resource FieldNamespace = "namespace" // FieldStatus represents the status field of a resource FieldStatus = "status" // FieldOwnerReference represents the owner reference field of a resource FieldOwnerReference = "ownerReference" // FieldOwnerKind represents the owner kind field of a resource FieldOwnerKind = "ownerKind" )
const ( ParameterName = "name" // Name parameter for filtering by resource name ParameterLabelSelector = "labelSelector" // Label selector parameter for filtering by labels ParameterFieldSelector = "fieldSelector" // Field selector parameter for filtering by fields ParameterPage = "page" // Page number parameter for pagination ParameterLimit = "limit" // Items per page parameter for pagination ParameterOrderBy = "sortBy" // Field to sort results by ParameterAscending = "ascending" // Sort direction parameter (true for ascending, false for descending) )
Common query parameter names used in API requests
Variables ¶
var NoPagination = newPagination(-1, 0)
NoPagination represents a query without pagination
Functions ¶
func DefaultList ¶
func DefaultList[T any](objects []T, q *Query, compareFunc CompareFunc[T], filterFunc FilterFunc[T], transformFuncs ...TransformFunc[T]) *api.ListResult[T]
DefaultList processes a list of objects with filtering, sorting, and pagination Parameters:
- objects: The list of objects to process
- q: Query parameters including filters, sorting, and pagination
- compareFunc: Function to compare objects for sorting
- filterFunc: Function to filter objects
- transformFuncs: Optional functions to transform objects
Returns a ListResult containing the processed objects
func DefaultObjectMetaCompare ¶
DefaultObjectMetaCompare compares two metav1.Object instances Returns true if left is greater than right based on the specified sort field Supports sorting by name or creation timestamp
func DefaultObjectMetaFilter ¶
DefaultObjectMetaFilter filters metav1.Object instances based on various criteria Supports filtering by:
- Names: Exact match against a comma-separated list of names
- Name: Partial match against object name
- UID: Exact match against object UID
- Namespace: Exact match against namespace
- OwnerReference: Match against owner reference UID
- OwnerKind: Match against owner reference kind
- Annotation: Match against annotations using label selector syntax
- Label: Match against labels using label selector syntax
func DefaultString ¶
DefaultString returns the default value if the input string is empty
func GetFieldByJSONTag ¶
GetFieldByJSONTag returns the value of the struct field whose JSON tag matches the given field name (filed). If not found by JSON tag, it tries to find the field by its struct field name. The function expects obj to be a struct or a pointer to a struct.
Types ¶
type CompareFunc ¶
CompareFunc is a generic function type that compares two objects of type T Returns true if left is greater than right
type Filter ¶
type Filter struct {
Field string `json:"field"` // Field to filter on
Value string `json:"value"` // Value to filter by
}
Filter represents a single field-value filter pair
type FilterFunc ¶
FilterFunc is a generic function type that filters objects of type T Returns true if the object matches the filter criteria
type Pagination ¶
type Pagination struct {
Limit int // Number of items per page
Offset int // Starting offset for the current page
}
Pagination represents pagination settings for query results
func (*Pagination) GetValidPagination ¶
func (p *Pagination) GetValidPagination(total int) (startIndex, endIndex int)
GetValidPagination calculates valid start and end indices for pagination
type Query ¶
type Query struct {
Pagination *Pagination // Pagination settings for the query results
// SortBy specifies which field to sort results by, defaults to FieldCreationTimeStamp
SortBy string
// Ascending determines sort direction, defaults to descending (false)
Ascending bool
// Filters contains field-value pairs for filtering results
Filters map[string]string
// LabelSelector contains the label selector string for filtering by labels
LabelSelector string
}
Query represents API search terms and filtering options
func ParseQueryParameter ¶
ParseQueryParameter parses query parameters from a RESTful request into a Query struct
func (*Query) AppendLabelSelector ¶
AppendLabelSelector adds additional label selectors to the existing query
type TransformFunc ¶
type TransformFunc[T any] func(T) T
TransformFunc is a generic function type that transforms objects of type T Returns the transformed object