query

package
v4.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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"
)
View Source
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

View Source
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

func DefaultObjectMetaCompare(left, right metav1.Object, sortBy string) bool

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

func DefaultObjectMetaFilter(item metav1.Object, filter Filter) bool

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

func DefaultString(value, defaultValue string) string

DefaultString returns the default value if the input string is empty

func GetFieldByJSONTag

func GetFieldByJSONTag(obj reflect.Value, filed string) reflect.Value

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

type CompareFunc[T any] func(T, T, string) bool

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

type FilterFunc[T any] func(T, Filter) bool

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 New

func New() *Query

New creates a new Query instance with default values

func ParseQueryParameter

func ParseQueryParameter(request *restful.Request) *Query

ParseQueryParameter parses query parameters from a RESTful request into a Query struct

func (*Query) AppendLabelSelector

func (q *Query) AppendLabelSelector(ls map[string]string) error

AppendLabelSelector adds additional label selectors to the existing query

func (*Query) Selector

func (q *Query) Selector() labels.Selector

Selector returns a labels.Selector for the query's label selector

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL