From 3f4c23adef14b2981baf2a692e6fea5a81a2668e Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 27 Jul 2023 10:14:27 +0800 Subject: [PATCH] :necktie: update: update the context data binding logic - remove Route.Validator field --- _examples/doc/TODO.md | 38 ++++++++++++++++++++++++++++++++++++++ context_binding.go | 21 ++++++++++++++------- pkg/binding/form.go | 2 +- pkg/binding/json.go | 2 +- pkg/binding/validate.go | 3 ++- pkg/binding/xml.go | 2 +- router.go | 5 ++--- 7 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 _examples/doc/TODO.md diff --git a/_examples/doc/TODO.md b/_examples/doc/TODO.md new file mode 100644 index 0000000..47e3c89 --- /dev/null +++ b/_examples/doc/TODO.md @@ -0,0 +1,38 @@ +# TODO + +## new server + +```go +srv := rux.NewServer() +``` + +## context + +```go +type ServiceProvider interface { + Service(name string) any +} + +type ServiceProviderFunc(name string) any + +// ServiceProviderFunc implements ServiceProvider +func (f ServiceProviderFunc) Service(name string) any { + return f(name) +} + +type Context struct { + // add service provider + ProviderFunc ServiceProvider + + // app + srv *Server + app *Server + app *Router +} + +// Service returns a service by name. eg: c.Service("db") +func (c *Context) Service(name string) any { + return c.ProviderFunc(name) +} +``` + diff --git a/context_binding.go b/context_binding.go index a6fedbc..1352cf3 100644 --- a/context_binding.go +++ b/context_binding.go @@ -1,6 +1,9 @@ package rux -import "github.com/gookit/rux/pkg/binding" +import ( + "github.com/gookit/goutil" + "github.com/gookit/rux/pkg/binding" +) // ShouldBind bind request data to a struct, will auto call validator // @@ -17,10 +20,7 @@ func (c *Context) ShouldBind(obj any, binder binding.Binder) error { // // c.MustBind(&user, binding.Json) func (c *Context) MustBind(obj any, binder binding.Binder) { - err := binder.Bind(c.Req, obj) - if err != nil { - panic(err) - } + goutil.PanicErr(binder.Bind(c.Req, obj)) } // AutoBind auto bind request data to a struct, will auto select binding.Binder by content-type @@ -32,8 +32,8 @@ func (c *Context) AutoBind(obj any) error { return binding.Auto(c.Req, obj) } -// Bind auto bind request data to an struct, will auto select binding.Binder by content-type -// Alias method of the Bind() +// Bind auto bind request data to a struct, will auto select binding.Binder by content-type. +// Alias method of the AutoBind() // // Usage: // @@ -42,6 +42,13 @@ func (c *Context) Bind(obj any) error { return binding.Auto(c.Req, obj) } +// Validate input struct or map data. should call Bind() before validate. +// +// Recommended use ShouldBind() instead, it will binding and validate. +func (c *Context) Validate(obj any) error { + return binding.Validate(obj) +} + /************************************************************* * quick context data binding *************************************************************/ diff --git a/pkg/binding/form.go b/pkg/binding/form.go index 5c68f7b..74eb6df 100644 --- a/pkg/binding/form.go +++ b/pkg/binding/form.go @@ -44,5 +44,5 @@ func DecodeUrlValues(values map[string][]string, ptr any, tagName string) error if err := dec.Decode(values, ptr); err != nil { return err } - return validating(ptr) + return Validate(ptr) } diff --git a/pkg/binding/json.go b/pkg/binding/json.go index 504c822..8df183d 100644 --- a/pkg/binding/json.go +++ b/pkg/binding/json.go @@ -31,5 +31,5 @@ func decodeJSON(r io.Reader, ptr any) error { return err } - return validating(ptr) + return Validate(ptr) } diff --git a/pkg/binding/validate.go b/pkg/binding/validate.go index fb3e845..d485b9b 100644 --- a/pkg/binding/validate.go +++ b/pkg/binding/validate.go @@ -27,7 +27,8 @@ func ResetValidator() { Validator = &stdValidator{} } -func validating(obj any) error { +// Validate bounded data +func Validate(obj any) error { // if Validator is nil, dont validate. if Validator == nil { return nil diff --git a/pkg/binding/xml.go b/pkg/binding/xml.go index f82c6ed..0ea57e9 100644 --- a/pkg/binding/xml.go +++ b/pkg/binding/xml.go @@ -32,5 +32,5 @@ func decodeXML(r io.Reader, obj any) error { return err } - return validating(obj) + return Validate(obj) } diff --git a/router.go b/router.go index 5fb8fc0..a8dc658 100644 --- a/router.go +++ b/router.go @@ -99,15 +99,14 @@ type Router struct { // // Extends tools // + // Renderer template(view) interface // Deprecated: will be removed Renderer Renderer - // Validator validator interface - // Deprecated: will be removed - Validator Validator } // New router instance, can with some options. +// // Quick start: // // r := New()