criteria

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter represents a collection of filter criteria parsed from query parameters. Example: filter[status]=active,inactive -> {"status": ["active", "inactive"]}

func NewFilter

func NewFilter(keyword string) Filter

NewFilter creates a new Filter with an optional keyword prefix. If keyword is empty, "filter" is used as the default.

func (*Filter) Add

func (f *Filter) Add(name string, vals ...string)

Add manually inserts one or more values for a given field name.

func (*Filter) Params

func (f *Filter) Params() map[string][]string

Params returns the parsed filter values by field name.

func (*Filter) Parse

func (f *Filter) Parse(raw RawParams) error

Parse extracts filter parameters from raw key/value pairs. Keys must have the format: "<keyword>[<field>]" Example:

raw := RawParams{"filter[status]": "active,inactive"}
f := NewFilter("")
if err := f.Parse(raw); err != nil { ... }
fmt.Println(f.Params()) // => map[status:[active inactive]]

type Pagination

type Pagination struct {
	// contains filtered or unexported fields
}

Pagination represents pagination criteria typically parsed from query parameters. It holds limit, offset, and total values, and can be configured with custom keywords for parsing parameter names.

func NewPagination

func NewPagination(opts ...PaginationOpt) Pagination

NewPagination creates a new Pagination instance with optional configuration overrides. By default, it uses "pagination[limit]" and "pagination[offset]" as parameter keys.

Example:

p := NewPagination() raw := RawParams{"pagination[limit]": "20", "pagination[offset]": "40"} if err := p.Parse(raw); err != nil { ... } fmt.Println(p.Limit(), p.Offset()) // => 20, 40

func (*Pagination) Limit

func (p *Pagination) Limit() uint64

Limit returns the configured limit value for pagination (number of items per page).

func (*Pagination) Offset

func (p *Pagination) Offset() uint64

Offset returns the configured offset value for pagination (number of items to skip).

func (*Pagination) Parse

func (p *Pagination) Parse(raw RawParams) error

Parse extracts pagination values (limit and offset) from the given raw parameters map. Keys must match the configured keyword format, e.g.:

pagination[limit]=20&pagination[offset]=40

If no limit is provided, a default value is used. Returns an error if parsing fails or if an unsupported key is encountered.

func (*Pagination) SetTotal

func (p *Pagination) SetTotal(total uint64)

SetTotal sets the total number of items across all pages. This is typically populated after executing a database query that counts rows.

func (*Pagination) ToQuery deprecated

func (p *Pagination) ToQuery() query.Limit

Deprecated: ToQuery conversion to query data structures should be done in the persistence rather than pkg, but current limitation and use of postgresdb.ExecOption parameters in calls to persistence is a limitation and planned to be refactored in the future which makes this function immediatelly deprecated.

func (*Pagination) Total

func (p *Pagination) Total() uint64

Total returns the total number of items across all pages. Note: This must be set explicitly by calling SetTotal after a query.

type PaginationOpt

type PaginationOpt func(*Pagination)

PaginationOpt is a functional option for configuring a Pagination instance.

func WithPaginationKeyword

func WithPaginationKeyword(k string) PaginationOpt

WithPaginationKeyword sets the base keyword used when parsing pagination parameters (default: "pagination").

func WithPaginationLimitKeyword

func WithPaginationLimitKeyword(k string) PaginationOpt

WithPaginationLimitKeyword sets the keyword used to identify the limit parameter (default: "limit").

func WithPaginationOffsetKeyword

func WithPaginationOffsetKeyword(k string) PaginationOpt

WithPaginationOffsetKeyword sets the keyword used to identify the offset parameter (default: "offset").

type RawParams

type RawParams = map[string]string

RawParams represents unprocessed key/value parameters, usually extracted from a request URL query.

type Sort

type Sort struct {
	Direction string
	NullsLast *bool
}

Sort describes how a single field should be sorted in a query. Direction should typically be "asc" or "desc". NullsLast indicates whether NULL values should appear last (optional).

type SortBy

type SortBy struct {
	// contains filtered or unexported fields
}

SortBy represents a collection of sorting instructions parsed from query parameters. Each field can have a Sort with direction and optional NullsLast flag. Example usage:

raw := RawParams{"sort[status]": "asc:true", "sort[start]": "desc:false"} s := NewSortBy("") if err := s.Parse(raw); err != nil { ... } fmt.Println(s.Params()) // => map[status:{Direction: "asc", NullsLast: true}, start:{Direction: "desc", NullsLast: false}]

func NewSortBy

func NewSortBy(keyword string) SortBy

NewSortBy creates a new SortBy with an optional keyword prefix. If keyword is empty, "sort" is used as the default. The keyword is used to identify relevant keys when parsing raw parameters.

func (*SortBy) Add

func (s *SortBy) Add(name string, direction string, nullsLast *bool)

Add manually inserts a sorting instruction for a given field name. direction should typically be "asc" or "desc". nullsLast is optional; pass nil if not needed.

func (*SortBy) Params

func (s *SortBy) Params() map[string]Sort

Params returns all parsed sorting instructions keyed by field name. Each value is a Sort containing direction and optional NullsLast flag.

func (*SortBy) Parse

func (s *SortBy) Parse(raw RawParams) error

Parse extracts sorting instructions from raw key/value pairs. Only keys with the format "<keyword>[<field>]" are considered. The value format is "direction[:nullsLast]" where nullsLast is optional and boolean. Examples:

"sort[status]=asc:true" → field "status", ascending, nulls last "sort[start]=desc:false" → field "start", descending, nulls not last

Returns an error if the key format is invalid or if nullsLast cannot be parsed as boolean.

func (*SortBy) ToQuery deprecated

func (s *SortBy) ToQuery() (*query.OrderBy, error)

Deprecated: ToQuery conversion to query data structures should be done in the persistence rather than pkg, but current limitation and use of postgresdb.ExecOption parameters in calls to persistence is a limitation and planned to be refactored in the future which makes this function immediatelly deprecated.

Jump to

Keyboard shortcuts

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