-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
110 lines (99 loc) · 3.91 KB
/
request.go
File metadata and controls
110 lines (99 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package queryutil
import (
"fmt"
"go.lumeweb.com/queryutil/filter"
"go.lumeweb.com/queryutil/filter/parser"
"net/http"
"net/url"
)
// ParseQuery parses all query parameters from a map.
// This is the main entry point for processing query parameters in an HTTP-agnostic way.
// It combines parsing of filters, sorts, and pagination parameters.
//
// Returns parsed Filter, Sort, and Pagination structs or an error
// if any validation fails.
//
// Example:
//
// // With a map of query parameters
// query := map[string][]string{
// "name": {"john"},
// "age_gte": {"18"},
// "_sort": {"name"},
// "_order": {"desc"},
// "_start": {"0"},
// "_end": {"10"},
// }
// filters, sorts, pagination, err := ParseQuery(query)
func ParseQuery(query map[string][]string) ([]CrudFilter, []Sort, Pagination, error) {
p := parser.NewQueryParamParser(query)
return ParseFromSource(p)
}
// ParseQueryWithSearch parses query parameters with global search support.
// Similar to ParseQuery but includes configuration for global search
// functionality across multiple columns.
//
// The searchConfig parameter specifies which columns should be included
// in global search operations when the 'q' parameter is present.
func ParseQueryWithSearch(query map[string][]string, searchConfig *filter.GlobalSearchConfig) ([]CrudFilter, []Sort, Pagination, error) {
p := parser.NewQueryParamParser(query, parser.WithSearchConfig(searchConfig))
return ParseFromSource(p)
}
// HTTPRequestParser implements the RequestParser interface for HTTP requests.
// It provides a way to parse query parameters from an HTTP request's URL query.
type HTTPRequestParser struct {
Query url.Values
SearchConfig *filter.GlobalSearchConfig
sortConfig *filter.SortConfig
}
// NewHTTPRequestParser creates a new HTTPRequestParser from an http.Request
func NewHTTPRequestParser(r *http.Request, searchConfig *filter.GlobalSearchConfig, sortConfig *filter.SortConfig) *HTTPRequestParser {
return &HTTPRequestParser{
Query: r.URL.Query(),
SearchConfig: searchConfig,
sortConfig: sortConfig,
}
}
// ParseFilters implements RequestParser.ParseFilters
func (p *HTTPRequestParser) ParseFilters() ([]filter.CrudFilter, error) {
return parser.NewQueryParamParser(p.Query).ParseFilters()
}
// ParseSorts implements RequestParser.ParseSorts
func (p *HTTPRequestParser) ParseSorts(config *filter.SortConfig) ([]filter.Sort, error) {
// Use provided config if available, fallback to our config
if config == nil {
config = p.sortConfig
}
if config == nil {
config = &filter.SortConfig{} // Default empty config
}
return ParseQuerySort(p.Query, config)
}
// GetSortConfig returns the parser's sort configuration
func (p *HTTPRequestParser) GetSortConfig() *filter.SortConfig {
return p.sortConfig
}
// ParsePagination implements RequestParser.ParsePagination
func (p *HTTPRequestParser) ParsePagination() (Pagination, error) {
return ParseQueryPagination(p.Query)
}
// ParseRequest parses all query parameters from an HTTP request.
// This is maintained for backward compatibility with older code.
func ParseRequest(r any) ([]CrudFilter, []Sort, Pagination, error) {
if req, ok := r.(*http.Request); ok {
filters, sorts, pagination, err := ParseFromSource(NewHTTPRequestParser(req, nil, nil))
return filters, sorts, pagination, err
}
return nil, nil, Pagination{}, fmt.Errorf("unsupported request type")
}
// ParseRequestWithSearch parses query parameters with global search support.
// This is maintained for backward compatibility.
func ParseRequestWithSearch(r any, searchConfig *GlobalSearchConfig) ([]CrudFilter, []Sort, Pagination, error) {
if req, ok := r.(*http.Request); ok {
filters, sorts, pagination, err := ParseFromSource(NewHTTPRequestParser(req, searchConfig, &filter.SortConfig{
SortableFields: searchConfig.SearchableColumns,
}))
return filters, sorts, pagination, err
}
return nil, nil, Pagination{}, fmt.Errorf("unsupported request type")
}