filter

package
v0.0.0-...-1fccfa7 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	COALESCE = "coalesce"
)

Variables

View Source
var (
	ErrIncompatibleSort = fmt.Errorf("sort incompatible with cursor; send empty sort")
)

Functions

func Generic

func Generic(oo ...filterOpt) *filter

func WithConstraint

func WithConstraint(k string, v any) filterOpt

WithConstraint sets single constraint to filter

func WithConstraints

func WithConstraints(c map[string][]any) filterOpt

WithConstraints set multiple constraints to filter

func WithCursor

func WithCursor(p *PagingCursor) filterOpt

WithCursor sets cursor to filter

func WithExpression

func WithExpression(e string) filterOpt

WithExpression sets expression to filter

func WithExpressionParsed

func WithExpressionParsed(e *ql.ASTNode) filterOpt

WithExpressionParsed sets parsed expression to filter

func WithLimit

func WithLimit(l uint) filterOpt

WithLimit sets limit to filter

func WithMetaConstraints

func WithMetaConstraints(mc map[string]any) filterOpt

WithMetaConstraints sets multiple state constraints to filter

func WithOrderBy

func WithOrderBy(o SortExprSet) filterOpt

WithOrderBy sets order by expression

func WithStateConstraint

func WithStateConstraint(k string, s State) filterOpt

WithStateConstraint sets single state constraint to filter

func WithStateConstraints

func WithStateConstraints(sc map[string]State) filterOpt

WithStateConstraints sets multiple state constraints to filter

Types

type Filter

type Filter interface {
	// Constraints returns map of attribute idents and values
	// used for structured filtering ({a1: [v1], a2: [v2, v3]} => "a1 = v1 AND a2 = (v2,v4)")
	Constraints() map[string][]any

	// StateConstraints returns map of attribute idents and states
	// used for structured filtering ({a1: s1, a2: s2} => "a1 = s1 AND a2 = s2")
	StateConstraints() map[string]State

	// MetaConstraints returns meta constraints
	MetaConstraints() map[string]any

	// Expression returns string, parseable by ql package
	Expression() string

	// OrderBy one or more fields
	OrderBy() SortExprSet

	// Limit amount of returned results
	Limit() uint

	// Cursor from the last fetch
	Cursor() *PagingCursor
}

type Page

type Page struct {
	Page   uint          `json:"page"`
	Count  uint          `json:"items"`
	Cursor *PagingCursor `json:"cursor"`
}

type Paging

type Paging struct {
	// How many items per fetch do we want
	Limit uint `json:"limit,omitempty"`

	// Opaque pointer to 1st item on page
	// read-only
	PageCursor *PagingCursor `json:"cursor,omitempty"`

	// Opaque cursor that points to the first item on the next page
	// read-only
	NextPage *PagingCursor `json:"nextPage,omitempty"`

	// Opaque cursor that points to the first item on the previous page
	// value of {cursor} will be copied here
	// read-only
	PrevPage *PagingCursor `json:"prevPage,omitempty"`

	IncPageNavigation bool `json:"incPageNavigation,omitempty"`
	IncTotal          bool `json:"incTotal,omitempty"`

	PageNavigation []*Page `json:"pageNavigation,omitempty"`
	Total          uint    `json:"total,omitempty"`
}

Paging is a helper struct that should be embedded in filter types to help with the paging

func NewPaging

func NewPaging(limit uint, cursor string) (p Paging, err error)

func (*Paging) Clone

func (p *Paging) Clone() *Paging

func (*Paging) GetLimit

func (p *Paging) GetLimit() uint

type PagingCursor

type PagingCursor struct {

	// sort in desc order
	ROrder bool

	// use < op instead of >
	LThen bool
	// contains filtered or unexported fields
}

func PagingCursorFrom

func PagingCursorFrom(ss SortExprSet, v valueGetter, primaries ...string) (_ *PagingCursor, err error)

PagingCursorFrom constructs a new paging cursor for the given valueGetter

func (*PagingCursor) Decode

func (p *PagingCursor) Decode(cursor string) error

func (*PagingCursor) Desc

func (p *PagingCursor) Desc() []bool

func (*PagingCursor) Encode

func (p *PagingCursor) Encode() string

func (*PagingCursor) IsLThen

func (p *PagingCursor) IsLThen() bool

func (*PagingCursor) IsROrder

func (p *PagingCursor) IsROrder() bool

func (*PagingCursor) KK

func (p *PagingCursor) KK() [][]string

func (*PagingCursor) Keys

func (p *PagingCursor) Keys() []string

func (*PagingCursor) MarshalJSON

func (p *PagingCursor) MarshalJSON() ([]byte, error)

MarshalJSON serializes cursor struct as JSON and encodes it as base64 + adds quotes to be treated as JSON string

func (*PagingCursor) Modifiers

func (p *PagingCursor) Modifiers() []string

func (*PagingCursor) Set

func (p *PagingCursor) Set(k string, v interface{}, d bool)

func (*PagingCursor) SetModifier

func (p *PagingCursor) SetModifier(k string, v interface{}, d bool, m string, kk ...string)

func (*PagingCursor) Sort

func (p *PagingCursor) Sort(sort SortExprSet) (SortExprSet, error)

Sort returns:

  • sort if cursor is nil
  • sort from cursor when sort is empty
  • sort from cursor when sort is compatible with cursor
  • error if sort & cursor are incompatible

func (*PagingCursor) String

func (p *PagingCursor) String() string

Stirng to implement Stringer and to get human-readable representation of the cursor

func (*PagingCursor) ToAST

func (cur *PagingCursor) ToAST(identLookup func(i string) (string, error), castFn func(i string, val any) (expr.TypedValue, error)) (out *ql.ASTNode, err error)

ToAST converts the given PagingCursor to a corresponding AST tree

The method should be used as the base for generating filtering expressions when working with databases, DAL reports, ...

@todo discuss this one

func (*PagingCursor) UnmarshalJSON

func (p *PagingCursor) UnmarshalJSON(in []byte) error

func (*PagingCursor) Values

func (p *PagingCursor) Values() []interface{}

func (*PagingCursor) Walk

func (p *PagingCursor) Walk(fn func(string, interface{}, bool))

type SortExpr

type SortExpr struct {
	Column string

	Descending bool
	// contains filtered or unexported fields
}

func (*SortExpr) Columns

func (s *SortExpr) Columns() []string

func (*SortExpr) Modifier

func (s *SortExpr) Modifier() string

func (*SortExpr) SetColumns

func (s *SortExpr) SetColumns(columns ...string)

func (*SortExpr) SetModifier

func (s *SortExpr) SetModifier(modifier string) error

type SortExprSet

type SortExprSet []*SortExpr

func (SortExprSet) Clone

func (set SortExprSet) Clone() (out SortExprSet)

Clone returns cloned sort expression set

func (SortExprSet) Columns

func (set SortExprSet) Columns() []string

Reverse reverses direction on each expression

func (SortExprSet) Get

func (set SortExprSet) Get(col string) *SortExpr

Get returns sort expression from set if exists

func (SortExprSet) LastDescending

func (set SortExprSet) LastDescending() bool

LastDescending returns true if last sort expr/col is descending

func (SortExprSet) MarshalJSON

func (set SortExprSet) MarshalJSON() ([]byte, error)

UnmarshalJSON parses sort expression when passed inside JSON

func (SortExprSet) Reverse

func (set SortExprSet) Reverse()

Reverse reverses direction on each expression

func (SortExprSet) Reversed

func (set SortExprSet) Reversed() bool

Sorting is revered if 1st expr has desc direction

func (*SortExprSet) Set

func (set *SortExprSet) Set(in string) error

UnmarshalJSON parses stringified sort expression when passed inside JSON

func (SortExprSet) String

func (set SortExprSet) String() string

func (*SortExprSet) UnmarshalJSON

func (set *SortExprSet) UnmarshalJSON(in []byte) error

UnmarshalJSON parses sort expression when passed inside JSON

func (SortExprSet) Validate

func (set SortExprSet) Validate(cc ...string) error

Validate returns error if any of the SortExpr columns is missing from the given list

type Sorting

type Sorting struct {
	Sort SortExprSet `json:"sort,omitempty"`
}

Sort is a helper struct that should be embedded in filter types to help with the sorting

func NewSorting

func NewSorting(sort string) (s Sorting, err error)

func (*Sorting) OrderBy

func (s *Sorting) OrderBy() SortExprSet

type State

type State uint

State for filtering by state, for example: include, exclude or return only deleted values

const (
	// StateExcluded do not include entries
	StateExcluded State = 0

	// StateInclusive include entries
	StateInclusive State = 1

	// StateExclusive only entries that have this state
	StateExclusive State = 2
)

State* constants aid with Filter*

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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