@@ -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.
4359func 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-
127135func parseModel (db * gorm.DB , model any ) (* schema.Schema , error ) {
128136 return schema .Parse (model , modelCache , db .NamingStrategy )
129137}
0 commit comments