filter

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildOrderBy

func BuildOrderBy(sortBy string, sortOrder SortOrder, table string, alias string) string

BuildOrderBy constructs an ORDER BY clause using only safe, constant column names.

func GetDefaultSortField

func GetDefaultSortField(table string) string

GetDefaultSortField returns the default sort field for a table.

func GetSortableFieldsForTable

func GetSortableFieldsForTable(table string) map[string]bool

GetSortableFieldsForTable returns fields that can be sorted. All filterable fields are sortable. For optimal performance on large datasets, consider adding indexes for frequently sorted fields. See docs/performance-tuning.mdx.

func GetValidFieldsForTable

func GetValidFieldsForTable(table string) map[string]bool

func ParseDateTime

func ParseDateTime(value string) (time.Time, error)

func ResolveSortField

func ResolveSortField(table, sortBy string) string

ResolveSortField maps a requested sort field to a safe column name. It returns only string literals from a switch; user input selects which constant to return. This breaks the taint chain for static analyzers.

func Validate

func Validate(filters *QueryFilterSet, table string) error

func ValidateSortByForTable

func ValidateSortByForTable(opts *QueryOptions, table string) error

ValidateSortByForTable validates opts.SortBy against the table's allowed sort fields. Normalizes SortBy (lowercase, trim) and returns an error if invalid. Call this at the database boundary before BuildWithOptions for defense-in-depth.

func ValidateSortField

func ValidateSortField(sortBy, table string) error

ValidateSortField validates that the sort field is allowed for the table.

Types

type BuildResult

type BuildResult struct {
	CTEs       []string
	Conditions []string
	Args       []interface{}
	NextArgPos int
	OrderBy    string // The ORDER BY clause (without "ORDER BY" prefix)
}

func Build

func Build(filters *QueryFilterSet, table string, alias string, startArgPos int) (*BuildResult, error)

func BuildWithOptions

func BuildWithOptions(filters *QueryFilterSet, table string, alias string, startArgPos int, opts *QueryOptions) (*BuildResult, error)

BuildWithOptions builds filter conditions and includes sorting options. It validates both filters and sort options, returning an error if either is invalid.

type Operator

type Operator string

Operator represents supported filter operators.

const (
	OpEqual              Operator = "eq"
	OpNotEqual           Operator = "ne"
	OpGreaterThan        Operator = "gt"
	OpGreaterThanOrEqual Operator = "gte"
	OpLessThan           Operator = "lt"
	OpLessThanOrEqual    Operator = "lte"
	OpIn                 Operator = "in"
	OpBetween            Operator = "between"
	OpLike               Operator = "like"
	OpILike              Operator = "ilike"
	OpIsNull             Operator = "isnull"
	OpIsNotNull          Operator = "isnotnull"
)

func ResolveOperator

func ResolveOperator(s string) Operator

type ParseError

type ParseError struct {
	Param   string
	Message string
}

type ParseOptions

type ParseOptions struct {
	MaxFilters  int // default 20
	MaxInValues int // default 100
	MaxCharLen  int // default 1000
}

type ParseResult

type ParseResult struct {
	Filters *QueryFilterSet
	Errors  []ParseError
}

func ParseFromQuery

func ParseFromQuery(queryParams url.Values, opts *ParseOptions) *ParseResult

ParseFromQuery parses URL query parameters into a ParseResult. Returns errors for invalid params rather than silently dropping them.

type QueryFilter

type QueryFilter struct {
	Field    string        `json:"field"`
	Operator Operator      `json:"operator"`
	Value    interface{}   `json:"value,omitempty"`
	Values   []interface{} `json:"values,omitempty"`
}

type QueryFilterSet

type QueryFilterSet struct {
	Filters []QueryFilter `json:"filters"`
}

type QueryOptions

type QueryOptions struct {
	SortBy       string    `json:"sort_by,omitempty"`
	SortOrder    SortOrder `json:"sort_order,omitempty"`
	IncludeCount bool      `json:"include_count,omitempty"`
}

QueryOptions contains sorting and pagination options for queries.

func (*QueryOptions) DefaultSortOrder

func (o *QueryOptions) DefaultSortOrder() SortOrder

DefaultSortOrder returns desc if empty, otherwise validates and returns the order.

type SortOrder

type SortOrder string

SortOrder represents the sort direction.

const (
	SortAsc  SortOrder = "asc"
	SortDesc SortOrder = "desc"
)

type TimestampValue

type TimestampValue struct {
	Time      time.Time
	Original  string
	Precision string
}

TimestampValue represents a parsed timestamp with its original string format.

Jump to

Keyboard shortcuts

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