Skip to content

Commit a83aa3a

Browse files
committed
refactor(api): pointer receivers for validation
1 parent dfeea8a commit a83aa3a

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

backend/pkg/api/handlers/handler_service.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ func (h *HandlerService) getDataAccessor(ctx context.Context) dataaccess.DataAcc
6363
var allNetworks []types.NetworkInfo
6464

6565
type InputValidator[T any] interface {
66-
Validate(params map[string]string, payload io.ReadCloser) (T, error)
66+
Validate(params map[string]string, payload io.ReadCloser) error
67+
*T
6768
}
6869

6970
type BusinessLogicFunc[Input any, Response any] func(ctx context.Context, input Input) (Response, error)
7071

71-
func Handle[Input InputValidator[Input], Response any](defaultCode int, logicFunc BusinessLogicFunc[Input, Response], isMockingAllowed bool) func(w http.ResponseWriter, r *http.Request) {
72+
func Handle[Input InputValidator[Value], Value, Response any](defaultCode int, logicFunc BusinessLogicFunc[Input, Response], isMockingAllowed bool) func(w http.ResponseWriter, r *http.Request) {
7273
return func(w http.ResponseWriter, r *http.Request) {
7374
// prepare input
7475
vars := mux.Vars(r)
@@ -83,8 +84,8 @@ func Handle[Input InputValidator[Input], Response any](defaultCode int, logicFun
8384
vars[k] = v[0]
8485
}
8586
// input validation
86-
var i Input
87-
input, err := i.Validate(vars, r.Body)
87+
var input Input = new(Value)
88+
err := input.Validate(vars, r.Body)
8889
if err != nil {
8990
handleErr(w, r, err)
9091
return

backend/pkg/api/handlers/handler_service_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type inputMock struct {
3333
successMessage string
3434
}
3535

36-
func (i inputMock) Validate(params map[string]string, payload io.ReadCloser) (inputMock, error) {
36+
func (i *inputMock) Validate(params map[string]string, payload io.ReadCloser) error {
3737
var v validationError
3838

3939
i.shouldFail = v.checkBool(params["should_fail"], "should_fail")
@@ -44,14 +44,14 @@ func (i inputMock) Validate(params map[string]string, payload io.ReadCloser) (in
4444
}
4545
var req request
4646
if err := v.checkBody(&req, payload); err != nil {
47-
return i, err
47+
return err
4848
}
4949
i.successMessage = req.SuccessMessage
5050
}
51-
return i, v.AsError()
51+
return v.AsError()
5252
}
5353

54-
func logicMock(ctx context.Context, input inputMock) (string, error) {
54+
func logicMock(ctx context.Context, input *inputMock) (string, error) {
5555
if !input.shouldFail {
5656
return input.successMessage, nil
5757
}

backend/pkg/api/handlers/validator_dashboard.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ import (
2121
// @Failure 400 {object} types.ApiErrorResponse
2222
// @Failure 409 {object} types.ApiErrorResponse "Conflict. The request could not be performed by the server because the authenticated user has already reached their group limit."
2323
// @Router /validator-dashboards/{dashboard_id}/groups [post]
24-
func (i inputPostValidatorDashboardGroups) Validate(params map[string]string, body io.ReadCloser) (inputPostValidatorDashboardGroups, error) {
24+
func (i *inputPostValidatorDashboardGroups) Validate(params map[string]string, body io.ReadCloser) error {
2525
var v validationError
2626
var req types.PostValidatorDashboardGroupsRequest
2727
if err := v.checkBody(&req, body); err != nil {
28-
return i, err
28+
return err
2929
}
3030
i.dashboardId = v.checkPrimaryDashboardId(params["dashboard_id"])
3131
i.name = v.checkNameNotEmpty(req.Name)
32-
return i, v.AsError()
32+
return v.AsError()
3333
}
3434

3535
type inputPostValidatorDashboardGroups struct {
3636
dashboardId types.VDBIdPrimary
3737
name string
3838
}
3939

40-
func (h *HandlerService) PostValidatorDashboardGroups(ctx context.Context, input inputPostValidatorDashboardGroups) (types.ApiDataResponse[types.VDBPostCreateGroupData], error) {
40+
func (h *HandlerService) PostValidatorDashboardGroups(ctx context.Context, input *inputPostValidatorDashboardGroups) (types.ApiDataResponse[types.VDBPostCreateGroupData], error) {
4141
var r types.ApiDataResponse[types.VDBPostCreateGroupData]
4242
dataAccessor := h.getDataAccessor(ctx)
4343
userId, err := GetUserIdByContext(ctx)
@@ -76,13 +76,13 @@ func (h *HandlerService) PostValidatorDashboardGroups(ctx context.Context, input
7676
// @Success 200 {object} types.GetValidatorDashboardGroupSummaryResponse
7777
// @Failure 400 {object} types.ApiErrorResponse
7878
// @Router /validator-dashboards/{dashboard_id}/groups/{group_id}/summary [get]
79-
func (i inputGetValidatorDashboardGroupSummary) Validate(params map[string]string, _ io.ReadCloser) (inputGetValidatorDashboardGroupSummary, error) {
79+
func (i *inputGetValidatorDashboardGroupSummary) Validate(params map[string]string, _ io.ReadCloser) error {
8080
var v validationError
8181
i.dashboardIdParam = v.checkDashboardId(params["dashboard_id"])
8282
i.groupId = v.checkGroupId(params["group_id"], forbidEmpty)
8383
i.period = checkEnum[enums.TimePeriod](&v, params["period"], "period")
8484
i.protocolModes = v.checkProtocolModes(params["modes"])
85-
return i, v.AsError()
85+
return v.AsError()
8686
}
8787

8888
type inputGetValidatorDashboardGroupSummary struct {
@@ -92,7 +92,7 @@ type inputGetValidatorDashboardGroupSummary struct {
9292
period enums.TimePeriod
9393
}
9494

95-
func (h *HandlerService) GetValidatorDashboardGroupSummary(ctx context.Context, input inputGetValidatorDashboardGroupSummary) (types.GetValidatorDashboardGroupSummaryResponse, error) {
95+
func (h *HandlerService) GetValidatorDashboardGroupSummary(ctx context.Context, input *inputGetValidatorDashboardGroupSummary) (types.GetValidatorDashboardGroupSummaryResponse, error) {
9696
var r types.GetValidatorDashboardGroupSummaryResponse
9797
dashboardId, err := h.getDashboardId(ctx, input.dashboardIdParam)
9898
if err != nil {

backend/pkg/api/handlers/validator_dashboard_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ func TestInputPostValidatorDashboardGroupsValidate(t *testing.T) {
4444
t.Run("success", func(t *testing.T) {
4545
params["dashboard_id"] = "1"
4646
body := stringAsBody(`{"name":"test"}`)
47-
i, err := i.Validate(params, body)
47+
err := i.Validate(params, body)
4848
assert.Nil(t, err)
4949
assert.Equal(t, types.VDBIdPrimary(1), i.dashboardId)
5050
assert.Equal(t, "test", i.name)
5151
})
5252
t.Run("empty name", func(t *testing.T) {
5353
params["dashboard_id"] = "1"
5454
body := stringAsBody(`{"name":""}`)
55-
_, err := i.Validate(params, body)
55+
err := i.Validate(params, body)
5656
assert.NotNil(t, err)
5757
})
5858
}
@@ -63,15 +63,15 @@ func TestPostValidatorDashboardGroups(t *testing.T) {
6363
dashboardId: 0,
6464
name: "test",
6565
}
66-
_, err := h.PostValidatorDashboardGroups(ctx, input)
66+
_, err := h.PostValidatorDashboardGroups(ctx, &input)
6767
assert.Nil(t, err)
6868
})
6969
t.Run("group count reached", func(t *testing.T) {
7070
input := inputPostValidatorDashboardGroups{
7171
dashboardId: 1,
7272
name: "test",
7373
}
74-
_, err := h.PostValidatorDashboardGroups(ctx, input)
74+
_, err := h.PostValidatorDashboardGroups(ctx, &input)
7575
assert.NotNil(t, err)
7676
assert.True(t, errors.Is(err, errConflict))
7777
})
@@ -87,7 +87,7 @@ func TestInputGetValidatorDashboardGroupSummaryValidate(t *testing.T) {
8787
"group_id": "1",
8888
"period": "all_time",
8989
}
90-
i, err := i.Validate(params, nil)
90+
err := i.Validate(params, nil)
9191
assert.Nil(t, err)
9292
assert.Equal(t, types.VDBIdPrimary(1), i.dashboardIdParam)
9393
assert.Equal(t, int64(1), i.groupId)
@@ -98,7 +98,7 @@ func TestInputGetValidatorDashboardGroupSummaryValidate(t *testing.T) {
9898
"group_id": "1",
9999
"period": "all_time",
100100
}
101-
_, err := i.Validate(params, nil)
101+
err := i.Validate(params, nil)
102102
assert.NotNil(t, err)
103103
})
104104
t.Run("empty group_id", func(t *testing.T) {
@@ -107,7 +107,7 @@ func TestInputGetValidatorDashboardGroupSummaryValidate(t *testing.T) {
107107
"group_id": "",
108108
"period": "all_time",
109109
}
110-
_, err := i.Validate(params, nil)
110+
err := i.Validate(params, nil)
111111
assert.NotNil(t, err)
112112
})
113113
t.Run("empty period", func(t *testing.T) {
@@ -116,7 +116,7 @@ func TestInputGetValidatorDashboardGroupSummaryValidate(t *testing.T) {
116116
"group_id": "1",
117117
"period": "",
118118
}
119-
_, err := i.Validate(params, nil)
119+
err := i.Validate(params, nil)
120120
assert.NotNil(t, err)
121121
})
122122
}

0 commit comments

Comments
 (0)