[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
| 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.
We prefer without var across the project unless it is really needed, so lets stick to it.
There was a problem hiding this comment.
+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.
I'll fix it with (just more simple):
err := c.Bind(&[]struct{ Field string }{})without variable declaration
echo.go
Outdated
| // 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.
For consistency, please search and replace similar statements across the project.
There was a problem hiding this comment.
I think var uri bytes.Buffer is more reasonable.
There was a problem hiding this comment.
var uri bytes.Buffer not equal uri := bytes.NewBuffer(nil)
|
|
||
| // Routes returns the registered routes. | ||
| func (e *Echo) Routes() []*Route { | ||
| routes := []*Route{} |
There was a problem hiding this comment.
This statement should be fine as we are reassigning the variable.
There was a problem hiding this comment.
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.
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.
@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.
Looked into append function so it uses the preallocated capacity, so we are good 👍
|
|
||
| // 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{} |
group.go
Outdated
| // 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.
middleware/basic_auth.go
Outdated
| } | ||
|
|
||
| realm := "" | ||
| var realm string |
There was a problem hiding this comment.
ineffectual assignment
There was a problem hiding this comment.
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.
| 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.
We keep all errors to lower case as it makes it easy to append further.
There was a problem hiding this comment.
Error strings should not start with a capital letter (https://github.com/golang/go/wiki/Errors)
There was a problem hiding this comment.
I meant the same, please search and replace across the project.
| if path[0] != '/' { | ||
| path = "/" + path | ||
| } | ||
| ppath := path // Pristine path |
There was a problem hiding this comment.
Same as above and let's revert all the renaming for consistency.
middleware/proxy.go
Outdated
| } | ||
|
|
||
| errc := make(chan error, 2) | ||
| errChan := make(chan error, 2) |
There was a problem hiding this comment.
Please revert the naming to original for consistency.
There was a problem hiding this comment.
isn't it more readable and clear?
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.
err variable shadowing
| // 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"]) |
|
@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
| } | ||
| ppath := path // Pristine path | ||
| pnames := []string{} // Param names | ||
| pPath := path // Pristine path |
There was a problem hiding this comment.
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.
this is internal name.. and has comment
There was a problem hiding this comment.
We stick to short names as Go guidelines recommend - specially used internally.
There was a problem hiding this comment.
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.
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.
Please revert this and rename to pnames.
middleware/proxy.go
Outdated
| cp := func(dst io.Writer, src io.Reader) { | ||
| _, err := io.Copy(dst, src) | ||
| errc <- err | ||
| _, errCopy := io.Copy(dst, src) |
@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