From 6a666acd5ce75bfa79b029bab9b6e13ae0e5573b Mon Sep 17 00:00:00 2001 From: Shubhendra Singh Chauhan Date: Fri, 26 Feb 2021 15:34:34 +0530 Subject: [PATCH] improve code quality (#1792) * Merge variable declaration with assignment * Fix unnecessary typecasting on `bytes.Buffer` * Remove unnecessary wrapping of function call --- binder.go | 12 ++++-------- context_test.go | 12 ++++-------- middleware/logger_test.go | 2 +- middleware/rate_limiter.go | 4 +--- middleware/rate_limiter_test.go | 4 +--- 5 files changed, 11 insertions(+), 23 deletions(-) diff --git a/binder.go b/binder.go index 9f0ca654e..0900ce8dc 100644 --- a/binder.go +++ b/binder.go @@ -101,10 +101,8 @@ type ValueBinder struct { // QueryParamsBinder creates query parameter value binder func QueryParamsBinder(c Context) *ValueBinder { return &ValueBinder{ - failFast: true, - ValueFunc: func(sourceParam string) string { - return c.QueryParam(sourceParam) - }, + failFast: true, + ValueFunc: c.QueryParam, ValuesFunc: func(sourceParam string) []string { values, ok := c.QueryParams()[sourceParam] if !ok { @@ -119,10 +117,8 @@ func QueryParamsBinder(c Context) *ValueBinder { // PathParamsBinder creates path parameter value binder func PathParamsBinder(c Context) *ValueBinder { return &ValueBinder{ - failFast: true, - ValueFunc: func(sourceParam string) string { - return c.Param(sourceParam) - }, + failFast: true, + ValueFunc: c.Param, ValuesFunc: func(sourceParam string) []string { // path parameter should not have multiple values so getting values does not make sense but lets not error out here value := c.Param(sourceParam) diff --git a/context_test.go b/context_test.go index 417d4a749..963c91e04 100644 --- a/context_test.go +++ b/context_test.go @@ -649,8 +649,7 @@ func TestContextRedirect(t *testing.T) { } func TestContextStore(t *testing.T) { - var c Context - c = new(context) + var c Context = new(context) c.Set("name", "Jon Snow") testify.Equal(t, "Jon Snow", c.Get("name")) } @@ -687,8 +686,7 @@ func TestContextHandler(t *testing.T) { } func TestContext_SetHandler(t *testing.T) { - var c Context - c = new(context) + var c Context = new(context) testify.Nil(t, c.Handler()) @@ -701,8 +699,7 @@ func TestContext_SetHandler(t *testing.T) { func TestContext_Path(t *testing.T) { path := "/pa/th" - var c Context - c = new(context) + var c Context = new(context) c.SetPath(path) testify.Equal(t, path, c.Path()) @@ -736,8 +733,7 @@ func TestContext_QueryString(t *testing.T) { } func TestContext_Request(t *testing.T) { - var c Context - c = new(context) + var c Context = new(context) testify.Nil(t, c.Request()) diff --git a/middleware/logger_test.go b/middleware/logger_test.go index b196bc6c8..4d4515b19 100644 --- a/middleware/logger_test.go +++ b/middleware/logger_test.go @@ -164,7 +164,7 @@ func TestLoggerCustomTimestamp(t *testing.T) { e.ServeHTTP(rec, req) var objs map[string]*json.RawMessage - if err := json.Unmarshal([]byte(buf.String()), &objs); err != nil { + if err := json.Unmarshal(buf.Bytes(), &objs); err != nil { panic(err) } loggedTime := *(*string)(unsafe.Pointer(objs["time"])) diff --git a/middleware/rate_limiter.go b/middleware/rate_limiter.go index 7d1abfcb9..46a310d96 100644 --- a/middleware/rate_limiter.go +++ b/middleware/rate_limiter.go @@ -263,6 +263,4 @@ func (store *RateLimiterMemoryStore) cleanupStaleVisitors() { /* actual time method which is mocked in test file */ -var now = func() time.Time { - return time.Now() -} +var now = time.Now diff --git a/middleware/rate_limiter_test.go b/middleware/rate_limiter_test.go index 2e57bf175..89d9a6edc 100644 --- a/middleware/rate_limiter_test.go +++ b/middleware/rate_limiter_test.go @@ -350,9 +350,7 @@ func TestRateLimiterMemoryStore_Allow(t *testing.T) { func TestRateLimiterMemoryStore_cleanupStaleVisitors(t *testing.T) { var inMemoryStore = NewRateLimiterMemoryStoreWithConfig(RateLimiterMemoryStoreConfig{Rate: 1, Burst: 3}) - now = func() time.Time { - return time.Now() - } + now = time.Now fmt.Println(now()) inMemoryStore.visitors = map[string]*Visitor{ "A": {