Skip to content

Commit

Permalink
improve code quality (#1792)
Browse files Browse the repository at this point in the history
* Merge variable declaration with assignment
* Fix unnecessary typecasting on `bytes.Buffer`
* Remove unnecessary wrapping of function call
  • Loading branch information
withshubh authored Feb 26, 2021
1 parent b0f56ea commit 6a666ac
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 23 deletions.
12 changes: 4 additions & 8 deletions binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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())

Expand All @@ -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())
Expand Down Expand Up @@ -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())

Expand Down
2 changes: 1 addition & 1 deletion middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))
Expand Down
4 changes: 1 addition & 3 deletions middleware/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions middleware/rate_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 6a666ac

Please # to comment.