Skip to content

Commit

Permalink
👔 update: update the context data binding logic
Browse files Browse the repository at this point in the history
- remove Route.Validator field
  • Loading branch information
inhere committed Jul 27, 2023
1 parent 5797efb commit 3f4c23a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 14 deletions.
38 changes: 38 additions & 0 deletions _examples/doc/TODO.md
Original file line number Diff line number Diff line change
@@ -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)
}
```

21 changes: 14 additions & 7 deletions context_binding.go
Original file line number Diff line number Diff line change
@@ -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
//
Expand All @@ -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
Expand All @@ -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:
//
Expand All @@ -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
*************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion pkg/binding/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/binding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func decodeJSON(r io.Reader, ptr any) error {
return err
}

return validating(ptr)
return Validate(ptr)
}
3 changes: 2 additions & 1 deletion pkg/binding/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/binding/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func decodeXML(r io.Reader, obj any) error {
return err
}

return validating(obj)
return Validate(obj)
}
5 changes: 2 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3f4c23a

Please sign in to comment.