Skip to content

Commit 6a666ac

Browse files
authored
improve code quality (#1792)
* Merge variable declaration with assignment * Fix unnecessary typecasting on `bytes.Buffer` * Remove unnecessary wrapping of function call
1 parent b0f56ea commit 6a666ac

File tree

5 files changed

+11
-23
lines changed

5 files changed

+11
-23
lines changed

binder.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ type ValueBinder struct {
101101
// QueryParamsBinder creates query parameter value binder
102102
func QueryParamsBinder(c Context) *ValueBinder {
103103
return &ValueBinder{
104-
failFast: true,
105-
ValueFunc: func(sourceParam string) string {
106-
return c.QueryParam(sourceParam)
107-
},
104+
failFast: true,
105+
ValueFunc: c.QueryParam,
108106
ValuesFunc: func(sourceParam string) []string {
109107
values, ok := c.QueryParams()[sourceParam]
110108
if !ok {
@@ -119,10 +117,8 @@ func QueryParamsBinder(c Context) *ValueBinder {
119117
// PathParamsBinder creates path parameter value binder
120118
func PathParamsBinder(c Context) *ValueBinder {
121119
return &ValueBinder{
122-
failFast: true,
123-
ValueFunc: func(sourceParam string) string {
124-
return c.Param(sourceParam)
125-
},
120+
failFast: true,
121+
ValueFunc: c.Param,
126122
ValuesFunc: func(sourceParam string) []string {
127123
// path parameter should not have multiple values so getting values does not make sense but lets not error out here
128124
value := c.Param(sourceParam)

context_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,7 @@ func TestContextRedirect(t *testing.T) {
649649
}
650650

651651
func TestContextStore(t *testing.T) {
652-
var c Context
653-
c = new(context)
652+
var c Context = new(context)
654653
c.Set("name", "Jon Snow")
655654
testify.Equal(t, "Jon Snow", c.Get("name"))
656655
}
@@ -687,8 +686,7 @@ func TestContextHandler(t *testing.T) {
687686
}
688687

689688
func TestContext_SetHandler(t *testing.T) {
690-
var c Context
691-
c = new(context)
689+
var c Context = new(context)
692690

693691
testify.Nil(t, c.Handler())
694692

@@ -701,8 +699,7 @@ func TestContext_SetHandler(t *testing.T) {
701699
func TestContext_Path(t *testing.T) {
702700
path := "/pa/th"
703701

704-
var c Context
705-
c = new(context)
702+
var c Context = new(context)
706703

707704
c.SetPath(path)
708705
testify.Equal(t, path, c.Path())
@@ -736,8 +733,7 @@ func TestContext_QueryString(t *testing.T) {
736733
}
737734

738735
func TestContext_Request(t *testing.T) {
739-
var c Context
740-
c = new(context)
736+
var c Context = new(context)
741737

742738
testify.Nil(t, c.Request())
743739

middleware/logger_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestLoggerCustomTimestamp(t *testing.T) {
164164
e.ServeHTTP(rec, req)
165165

166166
var objs map[string]*json.RawMessage
167-
if err := json.Unmarshal([]byte(buf.String()), &objs); err != nil {
167+
if err := json.Unmarshal(buf.Bytes(), &objs); err != nil {
168168
panic(err)
169169
}
170170
loggedTime := *(*string)(unsafe.Pointer(objs["time"]))

middleware/rate_limiter.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,4 @@ func (store *RateLimiterMemoryStore) cleanupStaleVisitors() {
263263
/*
264264
actual time method which is mocked in test file
265265
*/
266-
var now = func() time.Time {
267-
return time.Now()
268-
}
266+
var now = time.Now

middleware/rate_limiter_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,7 @@ func TestRateLimiterMemoryStore_Allow(t *testing.T) {
350350

351351
func TestRateLimiterMemoryStore_cleanupStaleVisitors(t *testing.T) {
352352
var inMemoryStore = NewRateLimiterMemoryStoreWithConfig(RateLimiterMemoryStoreConfig{Rate: 1, Burst: 3})
353-
now = func() time.Time {
354-
return time.Now()
355-
}
353+
now = time.Now
356354
fmt.Println(now())
357355
inMemoryStore.visitors = map[string]*Visitor{
358356
"A": {

0 commit comments

Comments
 (0)