Skip to content
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

Merged
merged 18 commits into from
Feb 21, 2018
Merged

[FIX] Cleanup code #1061

merged 18 commits into from
Feb 21, 2018

Conversation

im-kulikov
Copy link
Contributor

  • cleanup
  • fix collides with package name
  • optimize slice creation
  • fix variable name shadowing

- cleanup
- fix `collides with package name`
- optimize slice creation
- fix variable name shadowing
@codecov
Copy link

codecov bot commented Feb 10, 2018

Codecov Report

Merging #1061 into master will increase coverage by 1.46%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
middleware/body_dump.go 82.85% <ø> (+22.85%) ⬆️
middleware/logger.go 84.46% <ø> (ø) ⬆️
middleware/proxy.go 64.35% <0%> (+15.84%) ⬆️
middleware/jwt.go 88.57% <0%> (ø) ⬆️
middleware/csrf.go 76.71% <100%> (ø) ⬆️
bind.go 83.22% <100%> (ø) ⬆️
echo.go 86.38% <100%> (ø) ⬆️
router.go 93.2% <100%> (ø) ⬆️
middleware/basic_auth.go 68.57% <100%> (+9.11%) ⬆️
group.go 93.75% <100%> (ø) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7eec915...626e513. Read the comment docs.

@im-kulikov
Copy link
Contributor Author

@vishr any updates?

@im-kulikov
Copy link
Contributor Author

@vishr any updates before merge?

@im-kulikov
Copy link
Contributor Author

@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 }
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vishr fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo golang src github com labstack echo - bind_test go echo 2018-02-21 10-46-04

Copy link
Contributor

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?

Copy link
Contributor Author

@im-kulikov im-kulikov Feb 21, 2018

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)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Member

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.

Copy link
Contributor Author

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)

Copy link
Contributor Author

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{}
Copy link
Member

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.

Copy link
Contributor Author

@im-kulikov im-kulikov Feb 21, 2018

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)
  }

Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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

Copy link
Member

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{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -93,7 +93,7 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
}
}

realm := ""
var realm string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ineffectual assignment

Copy link
Contributor

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.

Copy link
Contributor Author

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")
Copy link
Member

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.

Copy link
Contributor Author

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)

Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -108,15 +108,15 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
return
}

errc := make(chan error, 2)
errChan := make(chan error, 2)
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

cookie, err := c.Cookie(tag[7:])
if err == nil {
cookie, errCookie := c.Cookie(tag[7:])
if errCookie == nil {
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err variable shadowing

Copy link
Contributor Author

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"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep errors to lowercase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explain?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are ok.

@vishr
Copy link
Member

vishr commented Feb 20, 2018

@im-kulikov I am ok to replace all := short assignment to var for empty initialization. Please make sure we are consistent across the project.

@im-kulikov
Copy link
Contributor Author

@vishr ok. thanks for explain

im-kulikov and others added 6 commits February 21, 2018 10:57
- errors in lowercase
- bytes.NewBuffer()
- `errCookie` -> `err` (hmm.. `err` variable shadowing)
- `errChan` -> `errCh`
@im-kulikov
Copy link
Contributor Author

@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
Copy link
Contributor

@alexaandru alexaandru Feb 21, 2018

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.

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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

@im-kulikov
Copy link
Contributor Author

@vishr there is any updates?

@vishr
Copy link
Member

vishr commented Feb 21, 2018

@im-kulikov I gave a second thought later and have decided as below:

  • Between := vs var and I would like to stick with := as much as possible.
  • For bytes initialization let's revert to new(bytes.Buffer), for reference https://golang.org/pkg/bytes/#NewBuffer
  • Let's keep the internal names as is for now e.g. pnames, pvalues, err

path := r.URL.RawPath
if path == "" {
path = r.URL.Path
rpath := r.URL.RawPath // raw path
Copy link
Member

@vishr vishr Feb 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rawPath is fine here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

cp := func(dst io.Writer, src io.Reader) {
_, err := io.Copy(dst, src)
errc <- err
_, errCopy := io.Copy(dst, src)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you reuse err?

Copy link
Contributor Author

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
```
@im-kulikov
Copy link
Contributor Author

@vishr fixed, something else?

@vishr
Copy link
Member

vishr commented Feb 21, 2018

@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!!! 🎉

@vishr vishr closed this Feb 21, 2018
@im-kulikov
Copy link
Contributor Author

em.. just closed?

@vishr vishr reopened this Feb 21, 2018
@vishr vishr merged commit f49d166 into labstack:master Feb 21, 2018
@vishr
Copy link
Member

vishr commented Feb 21, 2018

@im-kulikov just a mistake - don't worry 😉

@im-kulikov
Copy link
Contributor Author

@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.

@im-kulikov im-kulikov deleted the cleanup-code branch February 21, 2018 18:45
@alexaandru
Copy link
Contributor

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!

@im-kulikov
Copy link
Contributor Author

@alexaandru this one - #1061 (comment)

@vishr
Copy link
Member

vishr commented Feb 21, 2018

@im-kulikov @alexaandru can you guys join gitter chat? @im-kulikov If you are interested in fixing autocert please let me know labstack/armor#30

@im-kulikov
Copy link
Contributor Author

@vishr if it needed :)

vishr pushed a commit that referenced this pull request Mar 12, 2018
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants