Skip to content

Commit 3e702fd

Browse files
authored
refactor: simplify signature of httputil.UUIDFromString to enable comma ok idiom (#400)
1 parent cf74c20 commit 3e702fd

File tree

7 files changed

+56
-56
lines changed

7 files changed

+56
-56
lines changed

pkg/controllers/account.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ type AccountQueryFilter struct {
3737
External bool `form:"external"`
3838
}
3939

40-
func (a AccountQueryFilter) ToCreate(c *gin.Context) (models.AccountCreate, error) {
41-
budgetID, err := httputil.UUIDFromString(c, a.BudgetID)
42-
if err != nil {
43-
return models.AccountCreate{}, err
40+
func (a AccountQueryFilter) ToCreate(c *gin.Context) (models.AccountCreate, bool) {
41+
budgetID, ok := httputil.UUIDFromString(c, a.BudgetID)
42+
if !ok {
43+
return models.AccountCreate{}, false
4444
}
4545

4646
return models.AccountCreate{
@@ -49,7 +49,7 @@ func (a AccountQueryFilter) ToCreate(c *gin.Context) (models.AccountCreate, erro
4949
BudgetID: budgetID,
5050
OnBudget: a.OnBudget,
5151
External: a.External,
52-
}, nil
52+
}, true
5353
}
5454

5555
// RegisterAccountRoutes registers the routes for accounts with
@@ -160,8 +160,8 @@ func (co Controller) GetAccounts(c *gin.Context) {
160160
queryFields := httputil.GetURLFields(c.Request.URL, filter)
161161

162162
// Convert the QueryFilter to a Create struct
163-
create, err := filter.ToCreate(c)
164-
if err != nil {
163+
create, ok := filter.ToCreate(c)
164+
if !ok {
165165
return
166166
}
167167

pkg/controllers/allocation.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ type AllocationQueryFilter struct {
3737
EnvelopeID string `form:"envelope"`
3838
}
3939

40-
func (f AllocationQueryFilter) ToCreate(c *gin.Context) (models.AllocationCreate, error) {
41-
envelopeID, err := httputil.UUIDFromString(c, f.EnvelopeID)
42-
if err != nil {
43-
return models.AllocationCreate{}, err
40+
func (f AllocationQueryFilter) ToCreate(c *gin.Context) (models.AllocationCreate, bool) {
41+
envelopeID, ok := httputil.UUIDFromString(c, f.EnvelopeID)
42+
if !ok {
43+
return models.AllocationCreate{}, false
4444
}
4545

4646
return models.AllocationCreate{
4747
Month: f.Month,
4848
Amount: f.Amount,
4949
EnvelopeID: envelopeID,
50-
}, nil
50+
}, true
5151
}
5252

5353
// RegisterAllocationRoutes registers the routes for allocations with
@@ -155,8 +155,8 @@ func (co Controller) GetAllocations(c *gin.Context) {
155155
queryFields := httputil.GetURLFields(c.Request.URL, filter)
156156

157157
// Convert the QueryFilter to a Create struct
158-
create, err := filter.ToCreate(c)
159-
if err != nil {
158+
create, ok := filter.ToCreate(c)
159+
if !ok {
160160
return
161161
}
162162

pkg/controllers/category.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ type CategoryQueryFilter struct {
3636
Note string `form:"note"`
3737
}
3838

39-
func (f CategoryQueryFilter) ToCreate(c *gin.Context) (models.CategoryCreate, error) {
40-
budgetID, err := httputil.UUIDFromString(c, f.BudgetID)
41-
if err != nil {
42-
return models.CategoryCreate{}, err
39+
func (f CategoryQueryFilter) ToCreate(c *gin.Context) (models.CategoryCreate, bool) {
40+
budgetID, ok := httputil.UUIDFromString(c, f.BudgetID)
41+
if !ok {
42+
return models.CategoryCreate{}, false
4343
}
4444

4545
return models.CategoryCreate{
4646
Name: f.Name,
4747
BudgetID: budgetID,
4848
Note: f.Note,
49-
}, nil
49+
}, true
5050
}
5151

5252
// RegisterCategoryRoutes registers the routes for categories with
@@ -154,8 +154,8 @@ func (co Controller) GetCategories(c *gin.Context) {
154154
queryFields := httputil.GetURLFields(c.Request.URL, filter)
155155

156156
// Convert the QueryFilter to a Create struct
157-
create, err := filter.ToCreate(c)
158-
if err != nil {
157+
create, ok := filter.ToCreate(c)
158+
if !ok {
159159
return
160160
}
161161

pkg/controllers/envelope.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ type EnvelopeQueryFilter struct {
4141
Note string `form:"note"`
4242
}
4343

44-
func (e EnvelopeQueryFilter) ToCreate(c *gin.Context) (models.EnvelopeCreate, error) {
45-
categoryID, err := httputil.UUIDFromString(c, e.CategoryID)
46-
if err != nil {
47-
return models.EnvelopeCreate{}, err
44+
func (e EnvelopeQueryFilter) ToCreate(c *gin.Context) (models.EnvelopeCreate, bool) {
45+
categoryID, ok := httputil.UUIDFromString(c, e.CategoryID)
46+
if !ok {
47+
return models.EnvelopeCreate{}, false
4848
}
4949

5050
return models.EnvelopeCreate{
5151
Name: e.Name,
5252
Note: e.Note,
5353
CategoryID: categoryID,
54-
}, nil
54+
}, true
5555
}
5656

5757
// RegisterEnvelopeRoutes registers the routes for envelopes with
@@ -156,8 +156,8 @@ func (co Controller) GetEnvelopes(c *gin.Context) {
156156
queryFields := httputil.GetURLFields(c.Request.URL, filter)
157157

158158
// Convert the QueryFilter to a Create struct
159-
create, err := filter.ToCreate(c)
160-
if err != nil {
159+
create, ok := filter.ToCreate(c)
160+
if !ok {
161161
return
162162
}
163163

pkg/controllers/transaction.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,25 @@ type TransactionQueryFilter struct {
4343
AccountID string `form:"account" createField:"false"`
4444
}
4545

46-
func (f TransactionQueryFilter) ToCreate(c *gin.Context) (models.TransactionCreate, error) {
47-
budgetID, err := httputil.UUIDFromString(c, f.BudgetID)
48-
if err != nil {
49-
return models.TransactionCreate{}, err
46+
func (f TransactionQueryFilter) ToCreate(c *gin.Context) (models.TransactionCreate, bool) {
47+
budgetID, ok := httputil.UUIDFromString(c, f.BudgetID)
48+
if !ok {
49+
return models.TransactionCreate{}, false
5050
}
5151

52-
sourceAccountID, err := httputil.UUIDFromString(c, f.SourceAccountID)
53-
if err != nil {
54-
return models.TransactionCreate{}, err
52+
sourceAccountID, ok := httputil.UUIDFromString(c, f.SourceAccountID)
53+
if !ok {
54+
return models.TransactionCreate{}, false
5555
}
5656

57-
destinationAccountID, err := httputil.UUIDFromString(c, f.DestinationAccountID)
58-
if err != nil {
59-
return models.TransactionCreate{}, err
57+
destinationAccountID, ok := httputil.UUIDFromString(c, f.DestinationAccountID)
58+
if !ok {
59+
return models.TransactionCreate{}, false
6060
}
6161

62-
envelopeID, err := httputil.UUIDFromString(c, f.EnvelopeID)
63-
if err != nil {
64-
return models.TransactionCreate{}, err
62+
envelopeID, ok := httputil.UUIDFromString(c, f.EnvelopeID)
63+
if !ok {
64+
return models.TransactionCreate{}, false
6565
}
6666

6767
// If the envelopeID is nil, use an actual nil, not uuid.Nil
@@ -79,7 +79,7 @@ func (f TransactionQueryFilter) ToCreate(c *gin.Context) (models.TransactionCrea
7979
DestinationAccountID: destinationAccountID,
8080
EnvelopeID: eID,
8181
Reconciled: f.Reconciled,
82-
}, nil
82+
}, true
8383
}
8484

8585
// RegisterTransactionRoutes registers the routes for transactions with
@@ -215,8 +215,8 @@ func (co Controller) GetTransactions(c *gin.Context) {
215215
queryFields := httputil.GetURLFields(c.Request.URL, filter)
216216

217217
// Convert the QueryFilter to a Create struct
218-
create, err := filter.ToCreate(c)
219-
if err != nil {
218+
create, ok := filter.ToCreate(c)
219+
if !ok {
220220
return
221221
}
222222

@@ -226,8 +226,8 @@ func (co Controller) GetTransactions(c *gin.Context) {
226226
}, queryFields...)
227227

228228
if filter.AccountID != "" {
229-
accountID, err := httputil.UUIDFromString(c, filter.AccountID)
230-
if err != nil {
229+
accountID, ok := httputil.UUIDFromString(c, filter.AccountID)
230+
if !ok {
231231
return
232232
}
233233

pkg/httputil/request.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ func BindData(c *gin.Context, data interface{}) error {
3232

3333
// This is needed because gin does not support form binding to uuid.UUID currently.
3434
// Follow https://github.com/gin-gonic/gin/pull/3045 to see when this gets resolved.
35-
func UUIDFromString(c *gin.Context, s string) (uuid.UUID, error) {
35+
func UUIDFromString(c *gin.Context, s string) (uuid.UUID, bool) {
3636
if s == "" {
37-
return uuid.Nil, nil
37+
return uuid.Nil, true
3838
}
3939

4040
u, err := uuid.Parse(s)
4141
if err != nil {
4242
httperrors.InvalidUUID(c)
43-
return uuid.Nil, err
43+
return uuid.Nil, false
4444
}
4545

46-
return u, nil
46+
return u, true
4747
}

pkg/httputil/request_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func TestUUIDFromString(t *testing.T) {
7878
}
7979

8080
_ = c.Bind(&o)
81-
_, err := httputil.UUIDFromString(c, o.UUID)
82-
if err != nil {
81+
_, ok := httputil.UUIDFromString(c, o.UUID)
82+
if !ok {
8383
c.AbortWithStatus(http.StatusBadRequest)
8484
}
8585
c.Status(http.StatusOK)
@@ -100,8 +100,8 @@ func TestUUIDFromStringInvalid(t *testing.T) {
100100
}
101101

102102
_ = c.Bind(&o)
103-
_, err := httputil.UUIDFromString(c, o.UUID)
104-
if err != nil {
103+
_, ok := httputil.UUIDFromString(c, o.UUID)
104+
if !ok {
105105
c.AbortWithStatus(http.StatusBadRequest)
106106
}
107107
c.Status(http.StatusOK)
@@ -122,8 +122,8 @@ func TestUUIDFromStringEmpty(t *testing.T) {
122122
}
123123

124124
_ = c.Bind(&o)
125-
_, err := httputil.UUIDFromString(c, o.UUID)
126-
if err != nil {
125+
_, ok := httputil.UUIDFromString(c, o.UUID)
126+
if !ok {
127127
c.AbortWithStatus(http.StatusBadRequest)
128128
}
129129
c.Status(http.StatusOK)

0 commit comments

Comments
 (0)