-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[FIX] Cleanup code #1061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FIX] Cleanup code #1061
Conversation
- cleanup - fix `collides with package name` - optimize slice creation - fix variable name shadowing
Codecov Report
@@ Coverage Diff @@
## master #1061 +/- ##
==========================================
+ Coverage 79% 80.46% +1.46%
==========================================
Files 27 27
Lines 1881 1879 -2
==========================================
+ Hits 1486 1512 +26
+ Misses 281 259 -22
+ Partials 114 108 -6
Continue to review full report at Codecov.
|
@vishr any updates? |
@vishr any updates before merge? |
@vishr any updates? |
bind_test.go
Outdated
@@ -135,7 +135,7 @@ func TestBindForm(t *testing.T) { | |||
rec := httptest.NewRecorder() | |||
c := e.NewContext(req, rec) | |||
req.Header.Set(HeaderContentType, MIMEApplicationForm) | |||
obj := []struct{ Field string }{} | |||
var obj []struct{ Field string } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer without var
across the project unless it is really needed, so lets stick to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vishr fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for short variable declaration form (:=
) in general, but maybe with the exception for empty slices? Just like https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices recommends.
While we're at it, maybe we should add a note about the coding style to use to the "Contributing" section of the README?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix it with (just more simple):
err := c.Bind(&[]struct{ Field string }{})
without variable declaration
echo.go
Outdated
@@ -506,7 +506,7 @@ func (e *Echo) URL(h HandlerFunc, params ...interface{}) string { | |||
|
|||
// Reverse generates an URL from route name and provided parameters. | |||
func (e *Echo) Reverse(name string, params ...interface{}) string { | |||
uri := new(bytes.Buffer) | |||
uri := bytes.NewBuffer(nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, please search and replace similar statements across the project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think var uri bytes.Buffer
is more reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var uri bytes.Buffer
not equal uri := bytes.NewBuffer(nil)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -530,7 +530,7 @@ func (e *Echo) Reverse(name string, params ...interface{}) string { | |||
|
|||
// Routes returns the registered routes. | |||
func (e *Echo) Routes() []*Route { | |||
routes := []*Route{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This statement should be fine as we are reassigning the variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
routes := make([]*Route, 0, len(e.router.routes))
this is prealloc space for this part:
for _, v := range e.router.routes {
routes = append(routes, v)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you preallocate it then would it be simpler to do this instead:
routes := make([]*Route, len(e.router.routes))
for i, v := range e.router.routes {
routes[i] = v
}
You can avoid the append call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexaandru e.router.routes
is map[string]*Route
that's why i
is a string
, not an integer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stand corrected @im-kulikov
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looked into append function so it uses the preallocated capacity, so we are good 👍
@@ -92,7 +92,7 @@ func (g *Group) Match(methods []string, path string, handler HandlerFunc, middle | |||
|
|||
// Group creates a new sub-group with prefix and optional sub-group-level middleware. | |||
func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group { | |||
m := []MiddlewareFunc{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
group.go
Outdated
@@ -113,7 +113,7 @@ func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...Midd | |||
// Combine into a new slice to avoid accidentally passing the same slice for | |||
// multiple routes, which would lead to later add() calls overwriting the | |||
// middleware from earlier calls. | |||
m := []MiddlewareFunc{} | |||
var m []MiddlewareFunc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
middleware/basic_auth.go
Outdated
@@ -93,7 +93,7 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc { | |||
} | |||
} | |||
|
|||
realm := "" | |||
var realm string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ineffectual assignment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
realm := defaultRealm
if config.Realm != defaultRealm {
realm = strconv.Quote(config.Realm)
}
or maybe have defaultRealm unquoted and the entire variable and if can go away, and on the response we can simply use strconv.Quote(config.Realm)
directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks.. fixed
ErrValidatorNotRegistered = errors.New("validator not registered") | ||
ErrRendererNotRegistered = errors.New("renderer not registered") | ||
ErrInvalidRedirectCode = errors.New("invalid redirect status code") | ||
ErrCookieNotFound = errors.New("cookie not found") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We keep all errors to lower case as it makes it easy to append further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error strings should not start with a capital letter
(https://github.com/golang/go/wiki/Errors)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant the same, please search and replace across the project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌 no problem
@@ -59,8 +59,8 @@ func (r *Router) Add(method, path string, h HandlerFunc) { | |||
if path[0] != '/' { | |||
path = "/" + path | |||
} | |||
ppath := path // Pristine path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above and let's revert all the renaming for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
middleware/proxy.go
Outdated
@@ -108,15 +108,15 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler { | |||
return | |||
} | |||
|
|||
errc := make(chan error, 2) | |||
errChan := make(chan error, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert the naming to original for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't it more readable and clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's make it errCh
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
middleware/logger.go
Outdated
cookie, err := c.Cookie(tag[7:]) | ||
if err == nil { | ||
cookie, errCookie := c.Cookie(tag[7:]) | ||
if errCookie == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's revert to original names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err
variable shadowing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted
@@ -116,7 +116,7 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc { | |||
config.keyFunc = func(t *jwt.Token) (interface{}, error) { | |||
// Check the signing method | |||
if t.Method.Alg() != config.SigningMethod { | |||
return nil, fmt.Errorf("Unexpected jwt signing method=%v", t.Header["alg"]) | |||
return nil, fmt.Errorf("unexpected jwt signing method=%v", t.Header["alg"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep errors to lowercase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are ok.
@im-kulikov I am ok to replace all |
@vishr ok. thanks for explain |
- errors in lowercase - bytes.NewBuffer() - `errCookie` -> `err` (hmm.. `err` variable shadowing) - `errChan` -> `errCh`
@vishr hi! can you check now.. |
router.go
Outdated
@@ -59,8 +59,8 @@ func (r *Router) Add(method, path string, h HandlerFunc) { | |||
if path[0] != '/' { | |||
path = "/" + path | |||
} | |||
ppath := path // Pristine path | |||
pnames := []string{} // Param names | |||
pPath := path // Pristine path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm all for short variable names, but if we need to comment variable names, maybe we should at least consider using longer names? These names seems to be used away enough from where they were declared that they qualify for longer names. paramNames
, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is internal name.. and has comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We stick to short names as Go guidelines recommend - specially used internally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll revert this part
@vishr there is any updates? |
@im-kulikov I gave a second thought later and have decided as below:
|
path := r.URL.RawPath | ||
if path == "" { | ||
path = r.URL.Path | ||
rpath := r.URL.RawPath // raw path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rawPath
is fine here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path
collides with imported package name.. that's why I use rpath
with a comment
router.go
Outdated
ppath := path // Pristine path | ||
pnames := []string{} // Param names | ||
ppath := path // Pristine path | ||
var pNames []string // Param names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this and rename to pnames
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
middleware/proxy.go
Outdated
cp := func(dst io.Writer, src io.Reader) { | ||
_, err := io.Copy(dst, src) | ||
errc <- err | ||
_, errCopy := io.Copy(dst, src) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you reuse err
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@alexaandru if you just don't know how it works, please.. don't give advice ``` panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x12e9b34] goroutine 50 [running]: testing.tRunner.func1(0xc420518000) /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:742 +0x29d panic(0x1349aa0, 0x1593f70) /usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229 github.com/labstack/echo/middleware.CSRFWithConfig.func1.1(0x13ff9a0, 0xc4205005b0, 0x13babed, 0x13) /Users/kulikov/.golang/src/github.com/labstack/echo/middleware/csrf.go:129 +0xe4 github.com/labstack/echo/middleware.TestCSRF(0xc420518000) /Users/kulikov/.golang/src/github.com/labstack/echo/middleware/csrf_test.go:28 +0x2da testing.tRunner(0xc420518000, 0x13ce290) /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:777 +0xd0 created by testing.(*T).Run /usr/local/Cellar/go/1.10/libexec/src/testing/testing.go:824 +0x2e0 FAIL github.com/labstack/echo/middleware 0.119s ```
@vishr fixed, something else? |
@im-kulikov thanks for your contribution, I really appreciated it. On a side note, and lets be polite in the conversation @alexaandru is new to the project and Go (I believe), everybody here is to learn, collaborate and have fun!!! 🎉 |
em.. just closed? |
@im-kulikov just a mistake - don't worry 😉 |
@vishr @alexaandru Oh.. I'm sorry if I hurt or hooked you. I was referring to the fact that it is not necessary to give advice without verifying assumptions.. I had to go crazy and look for a problem. |
I'm all good @im-kulikov @vishr haven't seen anything even remotely unpolite :) and I'm sorry if any of my advices caused troubles. May I know which one that was? Cheers! |
@alexaandru this one - #1061 (comment) |
@im-kulikov @alexaandru can you guys join gitter chat? @im-kulikov If you are interested in fixing |
@vishr if it needed :) |
Enhancements: Implemented Response#After() Dynamically add/remove proxy targets Rewrite rules for proxy middleware Add ability to extract key from a form field Implemented rewrite middleware Adds a separate flag for the 'http/https server started on' message (#1043) Applied a little DRY to the redirect middleware (#1053) and tests (#1055) Simplify dep fetching (#1062) Add custom time stamp format #1046 (#1066) Update property name & default value & comment about custom logger Add X-Requested-With header constant Return error of context.File in c.contentDisposition Updated deps Updated README.md Fixes: Fixed Response#Before() Fixed #990 Fix href url from armor to echo (#1051) Fixed #1054 Fixed #1052, dropped param alias feature Avoid redirect loop in HTTPSRedirect middleware (#1058) Fix deferring body close in middleware/compress test (#1063) Cleanup code (#1061) FIX - We must close gzip.Reader, only if no error (#1069) Fix formatting (#1071) Can be a fix for auto TLS
collides with package name