Documentation
¶
Index ¶
- Constants
- Variables
- func FilterValues(values url.Values, excludes ...string) url.Values
- func JoinCsv(val []string) string
- func JoinCsvDeprecated(ss []string) (string, error)
- func LowerCommonInitialism(field string) string
- func Map[T, K any](in []T, fn func(T) (K, error)) ([]K, error)
- func NewParsers() map[string]ParserFn
- func ParseBool(in string) (any, error)
- func ParseByte(in string) (any, error)
- func ParseFloat32(in string) (any, error)
- func ParseFloat64(in string) (any, error)
- func ParseInt(in string) (any, error)
- func ParseInt16(in string) (any, error)
- func ParseInt32(in string) (any, error)
- func ParseInt64(in string) (any, error)
- func ParseJSON(in string) (any, error)
- func ParseNop(in string) (any, error)
- func ParseNumericPointer[T int | int16 | int32 | int64 | float32 | float64](in string) (any, error)
- func ParseString(in string) (any, error)
- func ParseStringPointer[T string | bool | time.Time](in string) (any, error)
- func ParseStruct(unk any, filterTag, sortTag string) (map[string]*Tag, error)
- func ParseTime(in string) (any, error)
- func SortQuery(queries []Query)
- func Split2(str, by string) (string, string)
- func Split3(str, by string) (string, string, string)
- func SplitCsv(val string) []string
- func SplitCsvDeprecated(s string) ([]string, error)
- func SplitOutsideBrackets(val string) []string
- func Unique[T comparable](values []T) []T
- func Unquote(str string, l, r rune) (string, bool)
- type Decoder
- func (d *Decoder[T]) Decode(u url.Values) (*Filter, error)
- func (d *Decoder[T]) SetFilterTag(filterTag string) *Decoder[T]
- func (d *Decoder[T]) SetLimitRange(min, max int) *Decoder[T]
- func (d *Decoder[T]) SetOps(field string, ops Op) *Decoder[T]
- func (d *Decoder[T]) SetParser(name string, parserFn ParserFn) *Decoder[T]
- func (d *Decoder[T]) SetParsers(parsers map[string]ParserFn) *Decoder[T]
- func (d *Decoder[T]) SetQueryLimitName(name string) *Decoder[T]
- func (d *Decoder[T]) SetQueryOffsetName(name string) *Decoder[T]
- func (d *Decoder[T]) SetQuerySortName(name string) *Decoder[T]
- func (d *Decoder[T]) SetSortTag(sortTag string) *Decoder[T]
- func (d *Decoder[T]) Validate() error
- type FieldSet
- type Filter
- type Op
- type Order
- type ParserFn
- type Query
- type SortDirection
- type SortOption
- type Tag
- type Type
Constants ¶
const ( // Tag name for struct parsing, customizable. TagFilter = "q" TagSort = "sort" // Reserved query string fields, customizable, since 'sort_by' or 'limit' // could be a valid field name. QuerySort = "sort_by" QueryLimit = "limit" QueryOffset = "offset" QueryAnd = "and" QueryOr = "or" // Pagination limit. LimitMin = 1 LimitMax = 20 )
const ( // OpsComparable represents comparable operators. // Most (or all) datatypes in SQL are comparable. OpsComparable = OpEq | OpNeq | OpLt | OpLte | OpGt | OpGte | OpIn | OpNotIn // OpsNull represents nullable operators. OpsNull = OpIs | OpIsNot // OpsIn represents the variation of `in`. OpsIn = OpIn | OpNotIn // OpsLike represents the variation of `like`/`not like`. OpsLike = OpLike | OpNotLike | OpIlike | OpNotIlike // OpsFullTextSearch represents full-text-search operators. OpsFullTextSearch = OpFts | OpPlFts | OpPhFts | OpWFts // OpsRange operators supports range operations. OpsRange = OpCs | OpCd | OpOv | OpSl | OpSr | OpNxr | OpNxl | OpAdj // OpsMany operators supports multiple values. OpsMany = OpsIn | OpsLike )
Variables ¶
var ( ErrUnknownOperator = errors.New("goql: unknown op") ErrInvalidOp = errors.New("goql: invalid op") ErrUnknownField = errors.New("goql: unknown field") ErrUnknownParser = errors.New("goql: unknown parser") ErrInvalidConjunction = errors.New("goql: invalid conjunction") ErrBadValue = errors.New("goql: bad value") ErrTooManyValues = errors.New("goql: too many values") )
var ( ErrInvalidSortDirection = errors.New("goql: invalid sort direction") ErrInvalidSortOption = errors.New("goql: invalid sort option") )
var ( SortDirectionAscending SortDirection = "asc" SortDirectionDescending SortDirection = "desc" SortOptionNullsFirst SortOption = "nullsfirst" // Default SortOptionNullsLast SortOption = "nullslast" // Default )
var QueryDelimiter = ','
Functions ¶
func FilterValues ¶ added in v0.0.2
FilterValues filters the keys from the url.Values.
func JoinCsvDeprecated ¶
func LowerCommonInitialism ¶
func NewParsers ¶
NewParsers returns a list of default parsers. This can be extended, and set back to the Decoder.
func ParseFloat32 ¶
ParseFloat32 parses string to float32.
func ParseFloat64 ¶
ParseFloat64 parses string to float64.
func ParseNumericPointer ¶
ParseNumericPointer parses json string to pointer type.
func ParseString ¶
ParseString returns the string as it is.
func ParseStringPointer ¶
ParseStringPointer parses json string to pointer type.
func SplitCsvDeprecated ¶
func SplitOutsideBrackets ¶
func Unique ¶ added in v0.0.2
func Unique[T comparable](values []T) []T
Unique returns a unique items in the same order.
Types ¶
type Decoder ¶
type Decoder[T any] struct { // contains filtered or unexported fields }
func NewDecoder ¶
func (*Decoder[T]) SetFilterTag ¶
func (*Decoder[T]) SetLimitRange ¶
func (*Decoder[T]) SetParsers ¶
func (*Decoder[T]) SetQueryLimitName ¶
func (*Decoder[T]) SetQueryOffsetName ¶
func (*Decoder[T]) SetQuerySortName ¶
func (*Decoder[T]) SetSortTag ¶
type FieldSet ¶
type Op ¶
type Op int
Op represents a SQL operator.
const ( OpEq Op = 1 << iota // =, equals, e.g. name.eq=john appleseed OpNeq // <> or !=, not equals, e.g. name.neq=john appleseed OpLt // <, less than OpLte // <=, less than equals OpGt // >, greater than OpGte // >=, greater than equals OpLike // like, multi-values, e.g. name.like=john* OpIlike // ilike, multi-values, same as like, but case insensitive, e.g. name.ilike=john% OpNotLike // not like, multi-values, e.g. name.notlike=john* OpNotIlike // not ilike, multi-values, e.g. name.notilike=john* OpIn // in, multi-values, name.in=alice&name.in=bob OpNotIn // not in, multi-values, name.notin=alice&name.notin=bob OpIs // is, checking for exact equality (null,true,false,unknown), e.g. age.is=null OpIsNot // is not, e.g. age.isnot=null OpFts // Full-Text search using to_tsquery OpPlFts // Full-Text search using plain to tsquery OpPhFts // Full-Text search using phrase to tsquery OpWFts // Full-Text search using word. OpCs // @>, contains, e.g. ?tags.cs=apple&tags.cs=orange OpCd // <@, contained in e.g. ?values.cd=1&values.cd=2 OpOv // &&, overlap OpSl // <<, strictly left of OpSr // >>, strictly right of OpNxr // &< OpNxl // &> OpAdj // -|- OpNot // not OpOr // or, e.g. or=(age.gt:10,age.lt:100) OpAnd // and, e.g. and=(or.(married_at.isnot:null, married_at.gt:now)) )
Useful reference: functions: https://www.postgresql.org/docs/14/functions.html range operators: https://www.postgresql.org/docs/14/functions-range.html array operators: https://www.postgresql.org/docs/current/functions-array.html
type Order ¶
type Order struct { Field string Direction SortDirection Option SortOption }
func ParseOrder ¶
type ParserFn ¶
ParserFn handles conversion of the querystring `string` input to the designated types.
type Query ¶
Query represents the parsed query string with operators.
func ParseQuery ¶
ParseQuery parses the query with operators.
type SortDirection ¶
type SortDirection string
func (SortDirection) DefaultOption ¶
func (o SortDirection) DefaultOption() SortOption
func (SortDirection) Valid ¶
func (o SortDirection) Valid() bool