query

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparison

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

Comparison allows comparing a particular value with one or more operators. The zero-value is treated equivalent to Equal(null).

func Equal

func Equal(v any) Comparison

Equal returns a comparison that match values equal to v. Panics if v is not JSON marshalable into a simple JSON type (string, number, bool or null).

func Greater

func Greater(gt any) Comparison

Greater returns a comparison that matches values > gte. Panics if gt is not JSON marshalable into an a sortable JSON type (string or number).

func GreaterOrEqual

func GreaterOrEqual(gte any) Comparison

GreaterOrEqual returns a comparison that matches values >= gte. Panics if gte is not JSON marshalable into an a sortable JSON type (string or number).

func In

func In[E any](elements ...E) Comparison

In returns a comparison that match values in elements. Panics if any element is not JSON marshalable into an a simple JSON type (string, number, bool or null).

func Less

func Less(lt any) Comparison

Less returns a comparison that matches values < lt. Panics if lt is not JSON marshalable into an a sortable JSON type (string or number).

func LessOrEqual

func LessOrEqual(lte any) Comparison

LessOrEqual returns a comparison that matches values <= lte. Panics if lte is not JSON marshalable into an a sortable JSON type (string or number).

func MultiOperator

func MultiOperator(cmps ...Comparison) Comparison

MultiOperator merges multiple comparisons with different operators together to a single comparison entry.

When conflicting operators ar encountered, the right most value is selected for the result. Operators are resolved based on operator keys, where some initializers have an overlap:

  • Equal and In both users $in.
  • NotEqual and NotIn both users $nin.
  • Range and GreaterThanOrEqual both uses $gte.
  • Range and LessThan both uses $lt.

Example valid usage:

MultiOperator(Equal(nil), LessThan(49))        // {"$in":[nil],"$lt":49}
MultiOperator(GreaterOrEqual(0), LessThan(49)) // {"$gte":0,"$lt":49}

Example of conflicting operators:

MultiOperator(Equal(0), In(1, 2))       // {"$in":[1,2]}
MultiOperator(In(1, 2), Equal(nil))     // null
MultiOperator(NotIn(1, 2), NotEqual(0)) // {"$nin":[0]}
MultiOperator(NotEqual(0), NotIn(1, 2)) // {"$nin":[1,2]}

func NotEqual

func NotEqual(v any) Comparison

NotEqual returns a comparison that match values not equal to v. Panics if v is not JSON marshalable into a simple JSON type (string, number, bool or null).

func NotIn

func NotIn[E any](elements ...E) Comparison

NotIn returns a comparison that match values not in elements. Panics if any element is not JSON marshalable into an a simple JSON type (string, number, bool or null).

func Range

func Range(gte, lt any) Comparison

Range is a short-hand for:

MergeComparisons(GreaterThanOrEqual(gte), LessThan(lt))

func Regex

func Regex(pattern string) Comparison

Regex returns a comparison that match values that matches the provided regexp pattern.

func (Comparison) MarshalJSON

func (c Comparison) MarshalJSON() ([]byte, error)

func (Comparison) String

func (cmp Comparison) String() string

func (*Comparison) UnmarshalJSON

func (c *Comparison) UnmarshalJSON(data []byte) error

type Comparisons

type Comparisons map[string]Comparison

Comparisons maps field paths joined by dot to a comparison.

func (Comparisons) Filter

func (paths Comparisons) Filter() Filter

type Data

type Data struct {
	Filter DataFilter `json:"filter"`
	Rollup string     `json:"rollup"`
	Last   int        `json:"last,omitempty"`
}

Data describes a data frame query structure.

type DataFilter

type DataFilter struct {
	Times DataTimesComparison `json:"times"`
}

DataFilter allows filtering which data to include in a data frame. The filter follows a similar structure to a resource Filter, but is more limited, and does not allow combining filters with "$and" or "$or" conjunctions.

type DataTimesComparison

type DataTimesComparison struct {
	GreaterOrEqual time.Time `json:"$gte,omitempty"`
	Less           time.Time `json:"$lt,omitempty"`
}

DataTimesComparison allows filtering times. The zero-value indicate API defaults.

func DataTimesRange

func DataTimesRange(gte, lt time.Time) DataTimesComparison

DataTimesRange matches times within the specified range.

type Filter

type Filter struct {
	And   []Filter
	Or    []Filter
	Paths Comparisons
}

Filter describes a search filter for matching resources.

func And

func And(filters ...FilterType) Filter

And returns an new filter that merges the passed in filters with logical AND.

func Field

func Field(path string, cmp Comparison) Filter

Field returns a new filter comparing a single field path.

func Or

func Or(filters ...FilterType) Filter

Or returns an new filter that merges the passed in filters with logical OR.

func (Filter) Filter

func (f Filter) Filter() Filter

func (Filter) MarshalJSON

func (f Filter) MarshalJSON() ([]byte, error)

func (Filter) String

func (f Filter) String() string

type FilterType

type FilterType interface {
	Filter() Filter
}

FilterType describe any type that can generate a filter.

type Query

type Query struct {
	Filter Filter   `json:"filter,omitempty"`
	Sort   []string `json:"sort,omitempty"`
	Limit  int      `json:"limit"`
	Skip   int      `json:"skip"`
}

Query describes the resource query structure.

func New

func New() Query

New returns a new resource query, using the API default limit.

Jump to

Keyboard shortcuts

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