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

Compatible way to enhance context.Param(), supportted from GET POST COOKIE Route/Var #1627

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

xiuno
Copy link

@xiuno xiuno commented Nov 6, 2018

Compatible way to enhance context.Param(), support fetch data from Route:key, _GET, _POST, _COOKIE.

In some nodejs frameworks, such as express koa, we can easily use param to get parameters of different ways(GET POST COOKIE...). Considering Gin's design principles, I feel a little wasteful for param to only get parameters on the route. So we can get the parameters conveniently.

Before I got the int in POST, I thought it was not convenient to have a mandatory conversion or bind to display.

Before:

age, err: = strconv.Atoi (ctx.PostForm ("age"))
if err! = nil {
    age = 0
}

Now:

age: = ctx.ParamInt ("age")

@codecov
Copy link

codecov bot commented Nov 6, 2018

Codecov Report

Merging #1627 into master will increase coverage by 0.25%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1627      +/-   ##
==========================================
+ Coverage   99.28%   99.53%   +0.25%     
==========================================
  Files          40       22      -18     
  Lines        1958      431    -1527     
==========================================
- Hits         1944      429    -1515     
+ Misses         10        1       -9     
+ Partials        4        1       -3
Impacted Files Coverage Δ
logger.go
debug.go
tree.go
utils.go
response_writer.go
fs.go
response_writer_1.8.go
path.go
context_17.go
auth.go
... and 7 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 66b47a8...45cf4f0. Read the comment docs.

@thinkerou
Copy link
Member

you should have one utils package, like:

package utils 

func StringToInt(s string) (i int) {
	i, err := strconv.Atoi(s)
	if err != nil {
		return 0
	} else {
		return i
	}
}

and like the follow use it:

utils.StringToInt(ctx.PostForm ("age"))

I think framework should not have these functions, it should keep simple. do you think?
cc @appleboy

Copy link

@thoeni thoeni left a comment

Choose a reason for hiding this comment

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

I don't think this is strictly needed as you can just have these conversions in your application, it doesn't belong to Gin, I guess?

@@ -289,6 +289,69 @@ func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)
return
}

// Force Type convert
func StringToInt(s string) (i int) {
Copy link

@thoeni thoeni Feb 14, 2019

Choose a reason for hiding this comment

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

This could be:

func StringToInt(s string) (i int) {
	i, _ = strconv.Atoi(s)
	return
}

or even better:

func StringToInt(s string) int {
	i, _ := strconv.Atoi(s)
	return i
}

as you shouldn't use named returns...

@@ -16,7 +16,7 @@ import (
"os"
"strings"
"time"

"strconv"
Copy link
Member

Choose a reason for hiding this comment

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

move internal package to top.

}
}
}
return v
Copy link
Member

Choose a reason for hiding this comment

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

if v := c.Query(key); v != "" {
	return v
}

if v := c. PostForm(key); v != "" {
	return v
}

...

@thinkerou thinkerou added this to the 1.x milestone Mar 1, 2019
@zdpdpdp
Copy link

zdpdpdp commented Mar 25, 2019

i support this.

In gin framework , we can see these functions of *context like below

// GetStringSlice returns the value associated with the key as a slice of strings.
func (c *Context) GetStringSlice(key string) (ss []string) {
      //
}

// GetStringMap returns the value associated with the key as a map of interfaces.
func (c *Context) GetStringMap(key string) (sm map[string]interface{}) {
	//
}

these also can use utils function to convert, but gin support convenient functions to call

so why not support a high call frequency function like ctx.ParamInt()?

cc @thinkerou

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.

5 participants