-
Notifications
You must be signed in to change notification settings - Fork 17
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
binding: support parse URL parameters into struct fields #32
Conversation
support args in url like '/apth/:ID ' can be bind into struct
Thanks for the PR! Unfortunately, I do not think it fits the use cases of this package, which is used for form/content binding. |
How about make the funcs variable? Then developers can customize them as +type BindHandler func(formStruct interface{}, ifacePtr ...interface{}) macaron.Handler
+var (
+ FormBindHandler BindHandler = Form
+ MultipartFormBindHandler BindHandler = MultipartForm
+ JsonBindHandler BindHandler = Json
+)
func bind(ctx *macaron.Context, obj interface{}, ifacePtr ...interface{}) {
contentType := ctx.Req.Header.Get("Content-Type")
if ctx.Req.Method == "POST" || ctx.Req.Method == "PUT" || ctx.Req.Method == "PATCH" || ctx.Req.Method == "DELETE" {
switch {
case strings.Contains(contentType, "form-urlencoded"):
- ctx.Invoke(Form(obj, ifacePtr...))
+ ctx.Invoke(FormBindHandler(obj, ifacePtr...))
case strings.Contains(contentType, "multipart/form-data"):
- ctx.Invoke(MultipartForm(obj, ifacePtr...))
+ ctx.Invoke(MultipartFormBindHandler(obj, ifacePtr...))
case strings.Contains(contentType, "json"):
- ctx.Invoke(Json(obj, ifacePtr...))
+ ctx.Invoke(JsonBindHandler(obj, ifacePtr...))
default:
var errors Errors
if contentType == "" {
errors.Add([]string{}, ERR_CONTENT_TYPE, "Empty Content-Type")
} else {
errors.Add([]string{}, ERR_CONTENT_TYPE, "Unsupported Content-Type")
}
ctx.Map(errors)
ctx.Map(obj) // Map a fake struct so handler won't panic.
}
} else {
- ctx.Invoke(Form(obj, ifacePtr...))
+ ctx.Invoke(FormBindHandler(obj, ifacePtr...))
}
} |
Hmm, thanks for the brainstorming! I'm very cautious to add global variables recently. What about adding another top level function |
add URL() for binding data from url
Thanks for the update, one last issue commented :) |
support args in url like '/path/:ID ' can be bind into struct