Files
micro--go-micro/model/query.go
T
wehub-resource-sync e071084ebe
govulncheck / govulncheck (push) Waiting to run
Harness (E2E) / Harnesses (mock LLM) (push) Waiting to run
Harness (E2E) / Provider harnesses (live LLM conformance) (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Run Tests / Unit Tests (push) Waiting to run
Run Tests / Etcd Integration Tests (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 12:40:33 +08:00

74 lines
1.7 KiB
Go

package model
// QueryOptions configures a List or Count operation.
type QueryOptions struct {
Filters []Filter
OrderBy string
Desc bool
Limit uint
Offset uint
}
// Filter represents a field-level query condition.
type Filter struct {
Field string // Column name
Op string // Operator: =, !=, <, >, <=, >=, LIKE
Value any // Comparison value
}
// QueryOption sets values in QueryOptions.
type QueryOption func(*QueryOptions)
// ApplyQueryOptions applies a set of QueryOptions and returns the result.
func ApplyQueryOptions(opts ...QueryOption) QueryOptions {
q := QueryOptions{}
for _, o := range opts {
o(&q)
}
return q
}
// Where adds an equality filter: field = value.
func Where(field string, value any) QueryOption {
return func(q *QueryOptions) {
q.Filters = append(q.Filters, Filter{Field: field, Op: "=", Value: value})
}
}
// WhereOp adds a filter with a custom operator (=, !=, <, >, <=, >=, LIKE).
func WhereOp(field, op string, value any) QueryOption {
return func(q *QueryOptions) {
q.Filters = append(q.Filters, Filter{Field: field, Op: op, Value: value})
}
}
// OrderAsc orders results by field ascending.
func OrderAsc(field string) QueryOption {
return func(q *QueryOptions) {
q.OrderBy = field
q.Desc = false
}
}
// OrderDesc orders results by field descending.
func OrderDesc(field string) QueryOption {
return func(q *QueryOptions) {
q.OrderBy = field
q.Desc = true
}
}
// Limit limits the number of returned records.
func Limit(n uint) QueryOption {
return func(q *QueryOptions) {
q.Limit = n
}
}
// Offset skips the first n records (for pagination).
func Offset(n uint) QueryOption {
return func(q *QueryOptions) {
q.Offset = n
}
}