Skip to content

Commit 51fad25

Browse files
fnoopvSystem-Glitch
andauthored
feat: add global variables for all query params name (#12)
Co-authored-by: SystemGlitch <jeremy.la@outlook.fr>
1 parent 3602db4 commit 51fad25

File tree

5 files changed

+87
-57
lines changed

5 files changed

+87
-57
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ results := []*model.User{}
106106
paginator, err := settings.Scope(session.DB(ctx, r.DB), request, &results)
107107
```
108108

109+
You can customize the names of the query parameters using the following global variables:
110+
- `QueryParamSearch`
111+
- `QueryParamFilter`
112+
- `QueryParamOr`
113+
- `QueryParamSort`
114+
- `QueryParamJoin`
115+
- `QueryParamFields`
116+
- `QueryParamPage`
117+
- `QueryParamPerPage`
118+
109119
### Filter
110120

111121
> ?filter=**field**||**$operator**||**value**

settings.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,59 @@ type Request struct {
2727
PerPage typeutil.Undefined[int]
2828
}
2929

30+
var (
31+
// QueryParamSearch the name of the query parameter for the search feature
32+
QueryParamSearch = "search"
33+
// QueryParamFilter the name of the query parameter for the filter feature
34+
QueryParamFilter = "filter"
35+
// QueryParamOr the name of the query parameter for the "or" filter feature
36+
QueryParamOr = "or"
37+
// QueryParamSort the name of the query parameter for the sort feature
38+
QueryParamSort = "sort"
39+
// QueryParamJoin the name of the query parameter for the join feature
40+
QueryParamJoin = "join"
41+
// QueryParamFields the name of the query parameter for the fields feature
42+
QueryParamFields = "fields"
43+
// QueryParamPage the name of the query parameter indicating the current page
44+
QueryParamPage = "page"
45+
// QueryParamPerPage the name of the query parameter indicating the page size
46+
QueryParamPerPage = "per_page"
47+
// DefaultPageSize the default pagination page size if the "per_page" query param
48+
// isn't provided.
49+
DefaultPageSize = 10
50+
51+
modelCache = &sync.Map{}
52+
)
53+
3054
// NewRequest creates a filter request from an HTTP request's query.
31-
// Uses the following entries in the query, expected to be validated:
32-
// - search
33-
// - filter
34-
// - or
35-
// - sort
36-
// - join
37-
// - fields
38-
// - page
39-
// - per_page
55+
// Uses the entries defined by the "QueryParam*" global variables from the given query. All those entries are expected to be validated.
4056
//
4157
// If a field in the query doesn't match the expected type (non-validated) for the
4258
// filtering option, it will be ignored without an error.
4359
func NewRequest(query map[string]any) *Request {
4460
r := &Request{}
45-
if search, ok := query["search"].(string); ok {
61+
if search, ok := query[QueryParamSearch].(string); ok {
4662
r.Search = typeutil.NewUndefined(search)
4763
}
48-
if filter, ok := query["filter"].([]*Filter); ok {
64+
if filter, ok := query[QueryParamFilter].([]*Filter); ok {
4965
r.Filter = typeutil.NewUndefined(filter)
5066
}
51-
if or, ok := query["or"].([]*Filter); ok {
67+
if or, ok := query[QueryParamOr].([]*Filter); ok {
5268
r.Or = typeutil.NewUndefined(or)
5369
}
54-
if sort, ok := query["sort"].([]*Sort); ok {
70+
if sort, ok := query[QueryParamSort].([]*Sort); ok {
5571
r.Sort = typeutil.NewUndefined(sort)
5672
}
57-
if join, ok := query["join"].([]*Join); ok {
73+
if join, ok := query[QueryParamJoin].([]*Join); ok {
5874
r.Join = typeutil.NewUndefined(join)
5975
}
60-
if fields, ok := query["fields"].([]string); ok {
76+
if fields, ok := query[QueryParamFields].([]string); ok {
6177
r.Fields = typeutil.NewUndefined(fields)
6278
}
63-
if page, ok := query["page"].(int); ok {
79+
if page, ok := query[QueryParamPage].(int); ok {
6480
r.Page = typeutil.NewUndefined(page)
6581
}
66-
if perPage, ok := query["per_page"].(int); ok {
82+
if perPage, ok := query[QueryParamPerPage].(int); ok {
6783
r.PerPage = typeutil.NewUndefined(perPage)
6884
}
6985
return r
@@ -116,14 +132,6 @@ type Blacklist struct {
116132
IsFinal bool
117133
}
118134

119-
var (
120-
// DefaultPageSize the default pagination page size if the "per_page" query param
121-
// isn't provided.
122-
DefaultPageSize = 10
123-
124-
modelCache = &sync.Map{}
125-
)
126-
127135
func parseModel(db *gorm.DB, model any) (*schema.Schema, error) {
128136
return schema.Parse(model, modelCache, db.NamingStrategy)
129137
}

settings_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,19 +1997,19 @@ func TestNewRequest(t *testing.T) {
19971997
{
19981998
desc: "all_fields",
19991999
query: map[string]any{
2000-
"filter": []*Filter{
2000+
QueryParamFilter: []*Filter{
20012001
{Field: "name", Args: []string{"val1"}, Operator: Operators["$cont"]},
20022002
{Field: "name", Args: []string{"val2"}, Operator: Operators["$cont"]},
20032003
},
2004-
"or": []*Filter{
2004+
QueryParamOr: []*Filter{
20052005
{Field: "name", Args: []string{"val3"}, Or: true, Operator: Operators["$eq"]},
20062006
},
2007-
"sort": []*Sort{{Field: "name", Order: SortDescending}},
2008-
"join": []*Join{{Relation: "Relation", Fields: []string{"a", "b"}}},
2009-
"page": 2,
2010-
"per_page": 15,
2011-
"fields": []string{"id", "name", "email", "computed"},
2012-
"search": "val",
2007+
QueryParamSort: []*Sort{{Field: "name", Order: SortDescending}},
2008+
QueryParamJoin: []*Join{{Relation: "Relation", Fields: []string{"a", "b"}}},
2009+
QueryParamPage: 2,
2010+
QueryParamPerPage: 15,
2011+
QueryParamFields: []string{"id", "name", "email", "computed"},
2012+
QueryParamSearch: "val",
20132013
},
20142014
want: &Request{
20152015
Filter: typeutil.NewUndefined([]*Filter{
@@ -2030,12 +2030,12 @@ func TestNewRequest(t *testing.T) {
20302030
{
20312031
desc: "partial",
20322032
query: map[string]any{
2033-
"filter": []*Filter{
2033+
QueryParamFilter: []*Filter{
20342034
{Field: "name", Args: []string{"val1"}, Operator: Operators["$cont"]},
20352035
{Field: "name", Args: []string{"val2"}, Operator: Operators["$cont"]},
20362036
},
2037-
"sort": []*Sort{{Field: "name", Order: SortDescending}},
2038-
"per_page": 15,
2037+
QueryParamSort: []*Sort{{Field: "name", Order: SortDescending}},
2038+
QueryParamPerPage: 15,
20392039
},
20402040
want: &Request{
20412041
Filter: typeutil.NewUndefined([]*Filter{
@@ -2049,14 +2049,14 @@ func TestNewRequest(t *testing.T) {
20492049
{
20502050
desc: "incorrect_type",
20512051
query: map[string]any{
2052-
"filter": "a",
2053-
"or": "b",
2054-
"sort": "c",
2055-
"join": "d",
2056-
"page": "e",
2057-
"per_page": "f",
2058-
"fields": "g",
2059-
"search": 1,
2052+
QueryParamFilter: "a",
2053+
QueryParamOr: "b",
2054+
QueryParamSort: "c",
2055+
QueryParamJoin: "d",
2056+
QueryParamPage: "e",
2057+
QueryParamPerPage: "f",
2058+
QueryParamFields: "g",
2059+
QueryParamSearch: 1,
20602060
},
20612061
want: &Request{},
20622062
},

validation.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,18 @@ func (v *JoinValidator) IsType() bool { return true }
135135
// Validation returns a new RuleSet for query validation.
136136
func Validation(_ *goyave.Request) v.RuleSet {
137137
return v.RuleSet{
138-
{Path: "filter", Rules: v.List{v.Array()}},
139-
{Path: "filter[]", Rules: v.List{&FilterValidator{}}},
140-
{Path: "or", Rules: v.List{v.Array()}},
141-
{Path: "or[]", Rules: v.List{&FilterValidator{Or: true}}},
142-
{Path: "sort", Rules: v.List{v.Array()}},
143-
{Path: "sort[]", Rules: v.List{&SortValidator{}}},
144-
{Path: "join", Rules: v.List{v.Array()}},
145-
{Path: "join[]", Rules: v.List{&JoinValidator{}}},
146-
{Path: "page", Rules: v.List{v.Int(), v.Min(1)}},
147-
{Path: "per_page", Rules: v.List{v.Int(), v.Between(1, 500)}},
148-
{Path: "search", Rules: v.List{v.String(), v.Max(255)}},
149-
{Path: "fields", Rules: v.List{v.String(), &FieldsValidator{}}},
138+
{Path: QueryParamFilter, Rules: v.List{v.Array()}},
139+
{Path: fmt.Sprintf("%s[]", QueryParamFilter), Rules: v.List{&FilterValidator{}}},
140+
{Path: QueryParamOr, Rules: v.List{v.Array()}},
141+
{Path: fmt.Sprintf("%s[]", QueryParamOr), Rules: v.List{&FilterValidator{Or: true}}},
142+
{Path: QueryParamSort, Rules: v.List{v.Array()}},
143+
{Path: fmt.Sprintf("%s[]", QueryParamSort), Rules: v.List{&SortValidator{}}},
144+
{Path: QueryParamJoin, Rules: v.List{v.Array()}},
145+
{Path: fmt.Sprintf("%s[]", QueryParamJoin), Rules: v.List{&JoinValidator{}}},
146+
{Path: QueryParamPage, Rules: v.List{v.Int(), v.Min(1)}},
147+
{Path: QueryParamPerPage, Rules: v.List{v.Int(), v.Between(1, 500)}},
148+
{Path: QueryParamSearch, Rules: v.List{v.String(), v.Max(255)}},
149+
{Path: QueryParamFields, Rules: v.List{v.String(), &FieldsValidator{}}},
150150
}
151151
}
152152

validation_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ import (
1212
func TestApplyValidation(t *testing.T) {
1313
set := Validation(nil)
1414

15-
expectedFields := []string{"filter", "filter[]", "or", "or[]", "sort", "sort[]", "join", "join[]", "fields", "page", "per_page", "search"}
15+
expectedFields := []string{
16+
QueryParamFilter,
17+
fmt.Sprintf("%s[]", QueryParamFilter),
18+
QueryParamOr,
19+
fmt.Sprintf("%s[]", QueryParamOr),
20+
QueryParamSort,
21+
fmt.Sprintf("%s[]", QueryParamSort),
22+
QueryParamJoin,
23+
fmt.Sprintf("%s[]", QueryParamJoin),
24+
QueryParamFields,
25+
QueryParamPage,
26+
QueryParamPerPage,
27+
QueryParamSearch}
1628
assert.True(t, lo.EveryBy(set, func(f *validation.FieldRules) bool {
1729
return lo.Contains(expectedFields, f.Path)
1830
}))

0 commit comments

Comments
 (0)