Skip to content

Commit 07def0b

Browse files
committed
refactor: add setField return for GetURLFields
1 parent 3ccf52f commit 07def0b

File tree

9 files changed

+32
-13
lines changed

9 files changed

+32
-13
lines changed

pkg/controllers/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (co Controller) GetAccounts(c *gin.Context) {
165165
}
166166

167167
// Get the set parameters in the query string
168-
queryFields := httputil.GetURLFields(c.Request.URL, filter)
168+
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
169169

170170
// Convert the QueryFilter to a Create struct
171171
create, ok := filter.ToCreate(c)

pkg/controllers/allocation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (co Controller) GetAllocations(c *gin.Context) {
160160
}
161161

162162
// Get the parameters set in the query string
163-
queryFields := httputil.GetURLFields(c.Request.URL, filter)
163+
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
164164

165165
// Convert the QueryFilter to a Create struct
166166
create, ok := filter.ToCreate(c)

pkg/controllers/budget.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (co Controller) GetBudgets(c *gin.Context) {
237237
_ = c.Bind(&filter)
238238

239239
// Get the fields that we're filtering for
240-
queryFields := httputil.GetURLFields(c.Request.URL, filter)
240+
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
241241

242242
var budgets []models.Budget
243243

pkg/controllers/category.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (co Controller) GetCategories(c *gin.Context) {
159159
_ = c.Bind(&filter)
160160

161161
// Get the fields that we are filtering for
162-
queryFields := httputil.GetURLFields(c.Request.URL, filter)
162+
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
163163

164164
// Convert the QueryFilter to a Create struct
165165
create, ok := filter.ToCreate(c)

pkg/controllers/envelope.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (co Controller) GetEnvelopes(c *gin.Context) {
161161
// The filters contain only strings, so this will always succeed
162162
_ = c.Bind(&filter)
163163

164-
queryFields := httputil.GetURLFields(c.Request.URL, filter)
164+
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
165165

166166
// Convert the QueryFilter to a Create struct
167167
create, ok := filter.ToCreate(c)

pkg/controllers/month_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (co Controller) GetMonthConfigs(c *gin.Context) {
169169
}
170170

171171
// Get the set parameters in the query string
172-
queryFields := httputil.GetURLFields(c.Request.URL, filter)
172+
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
173173

174174
// Convert the QueryFilter to a Filter struct
175175
parsed, ok := filter.Parse(c)

pkg/controllers/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (co Controller) GetTransactions(c *gin.Context) {
224224
}
225225

226226
// Get the fields set in the filter
227-
queryFields := httputil.GetURLFields(c.Request.URL, filter)
227+
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
228228

229229
// Convert the QueryFilter to a Create struct
230230
create, ok := filter.ToCreate(c)

pkg/httputil/query.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

pkg/httputil/query_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515
)
1616

1717
func TestGetURLFields(t *testing.T) {
18-
url, _ := url.Parse("http://example.com/api/v1/accounts?budget=87645467-ad8a-4e16-ae7f-9d879b45f569&onBudget=false")
18+
url, _ := url.Parse("http://example.com/api/v1/accounts?budget=87645467-ad8a-4e16-ae7f-9d879b45f569&onBudget=false&name=")
1919

20-
queryFields := httputil.GetURLFields(url, controllers.AccountQueryFilter{})
20+
queryFields, setFields := httputil.GetURLFields(url, controllers.AccountQueryFilter{})
2121

2222
assert.Equal(t, []interface{}{"BudgetID", "OnBudget"}, queryFields)
23+
assert.Equal(t, []string{"Name", "BudgetID", "OnBudget"}, setFields)
2324
}
2425

2526
func TestGetBodyFields(t *testing.T) {

0 commit comments

Comments
 (0)