Skip to content

Commit

Permalink
binding: support parse URL parameters into struct fields (#32)
Browse files Browse the repository at this point in the history
* bind data support args in url

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

* add URL() for binding data from url

add URL() for binding data from url

* Update binding.go

* Update go.mod

* Update binding.go

* Update binding.go

Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
  • Loading branch information
xmh19936688 and unknwon authored Feb 25, 2020
1 parent d92aca2 commit 1fa49ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,25 @@ func Json(jsonStruct interface{}, ifacePtr ...interface{}) macaron.Handler {
}
}

// URL is the middleware to parse URL parameters into struct fields.
func URL(obj interface{}, ifacePtr ...interface{}) macaron.Handler {
return func(ctx *macaron.Context) {
var errors Errors

ensureNotPointer(obj)
obj := reflect.New(reflect.TypeOf(obj))

val := obj.Elem()
for k, v := range ctx.AllParams() {
field := val.FieldByName(k[1:])
if field.IsValid() {
errors = setWithProperType(field.Kind(), v, field, k, errors)
}
}
validateAndMap(obj, ctx, errors, ifacePtr...)
}
}

// RawValidate is same as Validate but does not require a HTTP context,
// and can be used independently just for validation.
// This function does not support Validator interface.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337
github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e
golang.org/x/tools v0.0.0-20190805222050-c5a2fd39b72a // indirect
gopkg.in/macaron.v1 v1.3.4
gopkg.in/macaron.v1 v1.3.5
)

0 comments on commit 1fa49ad

Please sign in to comment.