@@ -15,8 +15,20 @@ import (
1515 "github.com/rs/zerolog/log"
1616)
1717
18- func GetURLFields (url * url.URL , filter any ) []any {
18+ // GetURLFields checks which query parameters are set and which query
19+ // parameters are set and can be used directly in a gorm query
20+ //
21+ // queryFields contains all field names that can be used directly
22+ // in a gorm Where statament as argument to specify the fields filtered on.
23+ // As gorm uses interface{} as type for the Where statement, we cannot use
24+ // a []string type here.
25+ //
26+ // setFields returns a []string with all field names set in the query parameters.
27+ // This can be useful to filter for zero values without defining them as pointer
28+ // fields in gorm.
29+ func GetURLFields (url * url.URL , filter any ) ([]any , []string ) {
1930 var queryFields []any
31+ var setFields []string
2032
2133 // Add all parameters set in the query string to the queryFields
2234 // This is used to determine which fields are queried in the database
@@ -31,11 +43,17 @@ func GetURLFields(url *url.URL, filter any) []any {
3143 // GetURLFields (e.g. AccountID on a TransactionQueryFilter)
3244 filterField := val .Type ().Field (i ).Tag .Get ("filterField" )
3345
34- if url .Query ().Has (param ) && filterField != "false" {
35- queryFields = append (queryFields , field )
46+ if url .Query ().Has (param ) {
47+ // All fields are added to SetFields
48+ setFields = append (setFields , field )
49+
50+ // If the field is a filterField (true by default), add it to the queryFields
51+ if filterField != "false" {
52+ queryFields = append (queryFields , field )
53+ }
3654 }
3755 }
38- return queryFields
56+ return queryFields , setFields
3957}
4058
4159// GetBodyFields returns a slice of strings with the field names
0 commit comments