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

binding: support parse URL parameters into struct fields #32

Merged
merged 6 commits into from
Feb 25, 2020

Conversation

xmh19936688
Copy link
Contributor

@xmh19936688 xmh19936688 commented Feb 24, 2020

support args in url like '/path/:ID ' can be bind into struct

support args in url like '/apth/:ID ' can be bind into struct
@unknwon
Copy link
Contributor

unknwon commented Feb 24, 2020

Thanks for the PR!

Unfortunately, I do not think it fits the use cases of this package, which is used for form/content binding.

@xmh19936688
Copy link
Contributor Author

xmh19936688 commented Feb 25, 2020

How about make the funcs variable? Then developers can customize them as macaron.ReturnHandler.
Like this:

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

@unknwon
Copy link
Contributor

unknwon commented Feb 25, 2020

Hmm, thanks for the brainstorming! I'm very cautious to add global variables recently.

What about adding another top level function binding.URL(...), which specifically binds URL parameters to a struct? Which seems minimum work for both this package and users.

binding.go Outdated Show resolved Hide resolved
@unknwon
Copy link
Contributor

unknwon commented Feb 25, 2020

Thanks for the update, one last issue commented :)

@unknwon unknwon changed the title bind data support args in url binding: support parse URL parameters into struct fields Feb 25, 2020
@unknwon unknwon merged commit 1fa49ad into go-macaron:master Feb 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants