Documentation
¶
Index ¶
- Constants
- func Base64URLDecode(s string) ([]byte, error)
- func Base64URLEncode(v []byte) string
- func StringToType(s string, valueType ValueType) (any, error)
- func StringsToType(ss []string, valueType ValueType) (any, error)
- func WithIn(values ...string) optionValidateFunc
- func WithMax(max string) optionValidateFunc
- func WithMin(min string) optionValidateFunc
- func WithNotAllowed() optionValidateFunc
- func WithNotEmpty() optionValidateFunc
- func WithNotIn(values ...string) optionValidateFunc
- func WithNotOperator(operators ...operatorCmpType) optionValidateFunc
- func WithOperator(operators ...operatorCmpType) optionValidateFunc
- func WithRequired() optionValidateFunc
- type Expression
- type ExpressionCmp
- type ExpressionLogic
- type ExpressionSort
- type KeyOption
- type OperatorCmpType
- type OptionQuery
- func WithCommaSplit(keys ...string) OptionQuery
- func WithDefaultLimit(limit uint64) OptionQuery
- func WithDefaultOffset(offset uint64) OptionQuery
- func WithExpressionCmp(key string, value *ExpressionCmp) OptionQuery
- func WithKey(key string, opts ...KeyOption) OptionQuery
- func WithKeyOperator(key string, operator operatorCmpType) OptionQuery
- func WithKeyType(key string, valueType ValueType) OptionQuery
- func WithKeyValueTransform(key string, fn func(string) string) OptionQuery
- func WithSkipExpressionCmp(key ...string) OptionQuery
- func WithSkipUnderscore(v bool) OptionQuery
- func WithUnderscorePrefix(v bool) OptionQuery
- type OptionValidate
- type OptionValidateSet
- func WithField(opts ...optionValidateFunc) OptionValidateSet
- func WithLimit(opts ...optionValidateFunc) OptionValidateSet
- func WithOffset(opts ...optionValidateFunc) OptionValidateSet
- func WithSort(opts ...optionValidateFunc) OptionValidateSet
- func WithValue(key string, opts ...optionValidateFunc) OptionValidateSet
- func WithValues(opts ...optionValidateFunc) OptionValidateSet
- type Query
- func (q *Query) AddField(fields ...string) *Query
- func (q *Query) AddSort(sorts ...ExpressionSort) *Query
- func (q *Query) AddWhere(exprs ...Expression) *Query
- func (q *Query) CloneLimit() *uint64
- func (q *Query) CloneOffset() *uint64
- func (q *Query) GetLimit() uint64
- func (q *Query) GetOffset() uint64
- func (q *Query) GetValue(v string) string
- func (q *Query) GetValues(v string) []string
- func (q *Query) Has(v string) bool
- func (q *Query) HasAny(vList ...string) bool
- func (q *Query) MarshalText() ([]byte, error)
- func (q *Query) SetLimit(limit uint64) *Query
- func (q *Query) SetOffset(offset uint64) *Query
- func (q *Query) Validate(v *Validator) error
- func (q *Query) Walk(fn func(Token) error) error
- type Token
- type Validator
- type ValueType
- type WalkType
Constants ¶
const ( // OperatorEmpty is the empty operator which is equal to OperatorIn. OperatorEmpty operatorCmpType = "" // OperatorEq is the equality operator. OperatorEq operatorCmpType = "eq" // OperatorNe is the not equal operator. OperatorNe operatorCmpType = "ne" // OperatorGt is the greater than operator. OperatorGt operatorCmpType = "gt" // OperatorLt is the less than operator. OperatorLt operatorCmpType = "lt" // OperatorGte is the greater than or equal operator. OperatorGte operatorCmpType = "gte" // OperatorLte is the less than or equal operator. OperatorLte operatorCmpType = "lte" // OperatorLike is the like operator. OperatorLike operatorCmpType = "like" // OperatorILike is the case insensitive like operator. OperatorILike operatorCmpType = "ilike" // OperatorNLike is the not like operator. OperatorNLike operatorCmpType = "nlike" // OperatorNILike is the case insensitive not like operator. OperatorNILike operatorCmpType = "nilike" // OperatorIn is the in operator. OperatorIn operatorCmpType = "in" // OperatorNIn is the not in operator. OperatorNIn operatorCmpType = "nin" // OperatorIs is the is null operator. OperatorIs operatorCmpType = "is" // OperatorIsNot is the is not null operator. OperatorIsNot operatorCmpType = "not" // OperatorKV is the contains operator JSON types. OperatorKV operatorCmpType = "kv" // OperatorJIn is the JSONB array "has any" operator (?|). OperatorJIn operatorCmpType = "jin" // OperatorNJIn is the negated JSONB array "has any" operator (NOT ?|). OperatorNJIn operatorCmpType = "njin" )
const ( // OperatorAnd is the AND operator. OperatorAnd operatorLogicType = "and" // OperatorOr is the OR operator. OperatorOr operatorLogicType = "or" )
Variables ¶
This section is empty.
Functions ¶
func Base64URLDecode ¶ added in v0.4.0
Base64URLDecode decodes a base64 URL encoded string without padding.
func Base64URLEncode ¶ added in v0.4.0
Base64URLEncode encodes v using base64 URL encoding without padding.
- This useful for non-URL-safe strings that need to be included in URLs.
- OperatorKV understands this encoding.
func StringsToType ¶ added in v0.3.2
func WithIn ¶
func WithIn(values ...string) optionValidateFunc
WithIn checks if the value is in the list of values.
- Usable for 'WithValue', 'WithSort', 'WithValues', 'WithFields'
func WithMax ¶
func WithMax(max string) optionValidateFunc
WithMax to validate the maximum of a value.
- Usable for 'WithValue', 'WithLimit', 'WithOffset'
func WithMin ¶
func WithMin(min string) optionValidateFunc
WithMin to validate the minimum of a value.
- Usable for 'WithValue', WithSort', 'WithLimit'
func WithNotAllowed ¶
func WithNotAllowed() optionValidateFunc
WithNotAllowed to validate the value is not allowed.
- Usable for 'WithValue', 'WithOffset', 'WithLimit', 'WithSort', 'WithValues', 'WithFields'
func WithNotEmpty ¶
func WithNotEmpty() optionValidateFunc
WithNotEmpty to validate the value is not empty.
- Usable for 'WithValue'
func WithNotIn ¶
func WithNotIn(values ...string) optionValidateFunc
WithNotIn checks if the value is not in the list of values.
- Usable for 'WithValue', 'WithSort', 'WithValues', 'WithFields'
func WithNotOperator ¶
func WithNotOperator(operators ...operatorCmpType) optionValidateFunc
WithNotOperator to validate the operator is not allowed.
- Usable for 'WithValue'
func WithOperator ¶
func WithOperator(operators ...operatorCmpType) optionValidateFunc
WithOperator to validate the operator is allowed.
- Usable for 'WithValue'
func WithRequired ¶
func WithRequired() optionValidateFunc
WithRequired to validate the value is required.
- Usable for 'WithValue'
Types ¶
type Expression ¶
type Expression interface {
Expression() Expression
String() string
}
type ExpressionCmp ¶
func NewExpressionCmp ¶ added in v0.2.1
func NewExpressionCmp(operator operatorCmpType, field string, value any) *ExpressionCmp
NewExpressionCmp creates a new ExpressionCmp.
func ParseExpression ¶ added in v0.2.1
func ParseExpression(key, value string, valueType ValueType) (*ExpressionCmp, error)
ParseExpression parses a single expression from key-value pairs.
- key -> key[eq]
- eq, ne, gt, lt, gte, lte, like, ilike, nlike, nilike, in, nin, is, not, kv
func ParseExpressionWithOperator ¶ added in v0.2.1
func (*ExpressionCmp) Expression ¶
func (e *ExpressionCmp) Expression() Expression
func (ExpressionCmp) String ¶ added in v0.2.2
func (e ExpressionCmp) String() string
type ExpressionLogic ¶
type ExpressionLogic struct {
Operator operatorLogicType
List []Expression
}
func NewExpressionLogic ¶ added in v0.4.0
func NewExpressionLogic(operator operatorLogicType, list []Expression) *ExpressionLogic
NewExpressionLogic creates a new ExpressionLogic.
func (*ExpressionLogic) Expression ¶
func (e *ExpressionLogic) Expression() Expression
func (ExpressionLogic) String ¶ added in v0.2.2
func (e ExpressionLogic) String() string
type ExpressionSort ¶
type KeyOption ¶ added in v0.4.6
type KeyOption func(key string, o *optionQuery)
KeyOption is a functional option scoped to a single key, used with WithKey.
func KeyCommaSplit ¶ added in v0.4.6
func KeyCommaSplit() KeyOption
KeyCommaSplit enables comma-separated value splitting for the key. When enabled, values containing commas are split and combined with OR (for positive operators) or AND (for negated operators like ne, nlike, nilike).
func KeyOperator ¶ added in v0.4.6
func KeyOperator(op operatorCmpType) KeyOption
KeyOperator sets the default operator for the key when no bracket operator is specified.
- For example, KeyOperator(OperatorILike) will parse "name=foo" as "name[ilike]=foo".
func KeyValueTransform ¶ added in v0.4.6
KeyValueTransform sets a value transform function for the key. The function is applied to the raw value string before parsing.
- For example, KeyValueTransform(func(v string) string { return "%" + v + "%" }) will parse "name=foo" as name=%foo%.
type OperatorCmpType ¶
type OperatorCmpType = operatorCmpType
OperatorCmpType is the comparison operator type used in expressions.
type OptionQuery ¶
type OptionQuery func(*optionQuery)
func WithCommaSplit ¶ added in v0.4.4
func WithCommaSplit(keys ...string) OptionQuery
WithCommaSplit enables comma-separated value splitting for the given keys. When enabled, values containing commas are split and combined with OR (for positive operators) or AND (for negated operators like ne, nlike, nilike).
This applies to all operators except in, nin, jin, njin (which already handle commas natively), and is, not, kv (which are single-value or special-format operators).
- For example, WithCommaSplit("name") will parse "name[ilike]=%foo%,%bar%" as (name ILIKE '%foo%' OR name ILIKE '%bar%').
- For negated operators: "name[nlike]=%foo%,%bar%" becomes (name NOT LIKE '%foo%' AND name NOT LIKE '%bar%').
func WithDefaultLimit ¶
func WithDefaultLimit(limit uint64) OptionQuery
WithDefaultLimit sets the default limit value.
func WithDefaultOffset ¶
func WithDefaultOffset(offset uint64) OptionQuery
WithDefaultOffset sets the default offset value.
func WithExpressionCmp ¶
func WithExpressionCmp(key string, value *ExpressionCmp) OptionQuery
WithExpressionCmp sets the expression comparison for a given key.
func WithKey ¶ added in v0.4.6
func WithKey(key string, opts ...KeyOption) OptionQuery
WithKey groups multiple key-scoped options for a single key. This is a convenience wrapper that applies each KeyOption to the given key.
query.WithKey("title",
query.KeyOperator(query.OperatorILike),
query.KeyValueTransform(func(v string) string { return "%" + v + "%" }),
query.KeyCommaSplit(),
)
func WithKeyOperator ¶ added in v0.4.2
func WithKeyOperator(key string, operator operatorCmpType) OptionQuery
WithKeyOperator sets the default operator for a given key when no bracket operator is specified.
- For example, WithKeyOperator("name", OperatorLike) will parse "name=foo" as "name[like]=foo".
func WithKeyType ¶ added in v0.3.2
func WithKeyType(key string, valueType ValueType) OptionQuery
func WithKeyValueTransform ¶ added in v0.4.3
func WithKeyValueTransform(key string, fn func(string) string) OptionQuery
WithKeyValueTransform sets a value transform function for a given key. The function is applied to the raw value string before parsing, regardless of whether a bracket operator is present.
- For example, WithKeyValueTransform("name", func(v string) string { return "%" + v + "%" }) will parse "name=foo" as name=%foo%.
func WithSkipExpressionCmp ¶
func WithSkipExpressionCmp(key ...string) OptionQuery
WithSkipExpressionCmp sets the keys to be skipped in the query.
func WithSkipUnderscore ¶ added in v0.3.0
func WithSkipUnderscore(v bool) OptionQuery
WithSkipUnderscore sets whether to skip keys starting with underscore.
- Default is true.
func WithUnderscorePrefix ¶ added in v0.4.1
func WithUnderscorePrefix(v bool) OptionQuery
WithUnderscorePrefix sets whether the special query keys use an underscore prefix.
- Default is true: _limit, _offset, _sort, _fields.
- When set to false: limit, offset, sort, fields.
type OptionValidate ¶
type OptionValidate func(string, ...optionValidateFunc)
type OptionValidateSet ¶
func WithField ¶
func WithField(opts ...optionValidateFunc) OptionValidateSet
func WithLimit ¶
func WithLimit(opts ...optionValidateFunc) OptionValidateSet
func WithOffset ¶
func WithOffset(opts ...optionValidateFunc) OptionValidateSet
func WithSort ¶
func WithSort(opts ...optionValidateFunc) OptionValidateSet
func WithValue ¶
func WithValue(key string, opts ...optionValidateFunc) OptionValidateSet
func WithValues ¶
func WithValues(opts ...optionValidateFunc) OptionValidateSet
type Query ¶
type Query struct {
Values map[string][]*ExpressionCmp
Select []string
Where []Expression
Sort []ExpressionSort
Offset *uint64
Limit *uint64
}
func Parse ¶
func Parse(query string, opts ...OptionQuery) (*Query, error)
Parse parses a query string into a Query struct.
func ParseWithValidator ¶
func ParseWithValidator(query string, validator *Validator, opts ...OptionQuery) (*Query, error)
func (*Query) AddSort ¶ added in v0.2.1
func (q *Query) AddSort(sorts ...ExpressionSort) *Query
func (*Query) AddWhere ¶ added in v0.2.1
func (q *Query) AddWhere(exprs ...Expression) *Query
func (*Query) CloneLimit ¶
func (*Query) CloneOffset ¶
func (*Query) MarshalText ¶ added in v0.2.2
func (*Query) SetLimit ¶ added in v0.2.1
SetLimit sets the limit for the query.
- if limit is <= 0, it means no limit.
type Token ¶
type Token struct {
Expression Expression
Type WalkType
}
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func NewValidator ¶
func NewValidator(opts ...OptionValidateSet) (*Validator, error)