query

package
v0.17.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: Apache-2.0 Imports: 4 Imported by: 7

Documentation

Index

Constants

View Source
const (
	FilterTypeProperty = "property"
	FilterTypeOperator = "operator"
)
View Source
const (
	MinPage        = 1   // MinPage represents the minimum allowed value for the pagination query's Page parameter.
	MinPerPage     = 1   // MinPerPage represents the minimum allowed value for the pagination query's PerPage parameter.
	DefaultPerPage = 10  // DefaultPerPage represents the default value for the pagination query's PerPage parameter.
	MaxPerPage     = 100 // MaxPerPage represents the maximum allowed value for the pagination query's PerPage parameter.
)
View Source
const (
	OrderAsc  = "asc"
	OrderDesc = "desc"
)

Variables

View Source
var (
	ErrFilterInvalid         = errors.New("filter is invalid")
	ErrFilterPropertyInvalid = errors.New("filter property is not valid")
	ErrFilterOperatorInvalid = errors.New("filter operator is not valid")
)

Functions

This section is empty.

Types

type Filter

type Filter struct {
	Type   string      `json:"type,omitempty"`
	Params interface{} `json:"params,omitempty"`
}

func (*Filter) UnmarshalJSON

func (f *Filter) UnmarshalJSON(data []byte) error

type FilterOperator

type FilterOperator struct {
	// Name is the filter operator (e.g., "and", "or").
	Name string `json:"name"`
}

FilterOperator represents a JSON representation of a filter operator in a query (e.g., "and", "or" in MongoDB queries).

type FilterProperty

type FilterProperty struct {
	// Name is the attribute to be observed in the operation.
	Name string `json:"name"`

	// Operator is the operation (e.g., "eq" for equal).
	Operator string `json:"operator"`

	// Value is the value used in the operation. (e.g., "eq" operations use Value to determine the value to be equal).
	Value interface{} `json:"value"`
}

FilterProperty is a JSON representation of a property expression in a query.

Name is the attribute to be observed in the operation, Operator is the operation, and Value is the value used in the operation. While Name can be any string, the Operator must be supported by the implementation, and the Value is the value used in the operation.

Each operator has its own implementation, and one operator can have multiple implementations. For that reason, the operator must be converted to a useful value using build methods.

Examples: A FilterProperty with Operator "gt", Name "count", and Value 12 will filter documents with the attribute "count" greater than 12. Another FilterProperty with Operator "eq", Name "alias", and Value "foobar" will filter documents with the attribute "alias" equal to "foobar".

type Filters

type Filters struct {
	// Raw holds the raw data of the filter and it's a base64-encoded JSON.
	Raw string `query:"filter"`

	// Data stores the decoded filters; it's automatically populated with the Unmarshal method.
	Data []Filter
}

Filters represents a set of filters that can be applied to queries.

func NewFilters

func NewFilters() *Filters

NewFilters creates a new instance of Filters with an empty Data slice.

func (*Filters) Unmarshal

func (fs *Filters) Unmarshal() error

Unmarshal decodes and unmarshals the raw filters, populating the Data attribute.

type Paginator

type Paginator struct {
	// Page represents the current page number.
	Page int `query:"page"`

	// PerPage represents the number of items per page.
	PerPage int `query:"per_page"`
}

Paginator represents the paginator parameters in a query.

func NewPaginator

func NewPaginator() *Paginator

NewPaginator creates and returns a new Paginator instance with MinPage and DefaultPerPage.

func (*Paginator) Normalize

func (p *Paginator) Normalize()

Normalize ensures valid values for Page and PerPage in the pagination query. If query.PerPage is less than zero, it is set to `DefaultPerPage`. If query.Page is less than one, it is set to `MinPage`. The maximum allowed value for query.PerPage is `MaxPerPage`.

type Sorter

type Sorter struct {
	By    string `query:"sort_by"`
	Order string `query:"order_by" validate:"omitempty,oneof=asc desc"`
}

Sorter represents the sorting order in a query.

func NewSorter

func NewSorter() *Sorter

NewOrder creates and returns a new Sort instance with the default descending order.

func (*Sorter) Normalize

func (s *Sorter) Normalize()

Normalize ensures that the sorting order is valid. If an invalid order is provided, it defaults to descending order.

Jump to

Keyboard shortcuts

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