Skip to content

Commit

Permalink
💥 up: replace all interface{} to go1.18 any, will not support go<1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 19, 2023
1 parent 8908e05 commit e38d70e
Show file tree
Hide file tree
Showing 23 changed files with 114 additions and 103 deletions.
12 changes: 6 additions & 6 deletions _examples/proxyreq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func doHandle(w http.ResponseWriter, r *http.Request) {
// get target url. eg: https://baidu.com/ss/yy
apiUrl := r.Header.Get("Target-Url")
if apiUrl == "" {
responseJSON(w, 200, map[string]interface{}{
responseJSON(w, 200, map[string]any{
"code": 400,
"msg": "remote target url cannot be empty",
"data": map[string]string{},
Expand All @@ -44,7 +44,7 @@ func doHandle(w http.ResponseWriter, r *http.Request) {
// create request
req, err := http.NewRequest(r.Method, apiUrl, r.Body)
if err != nil {
responseJSON(w, 200, map[string]interface{}{
responseJSON(w, 200, map[string]any{
"code": 400,
"msg": "create request fail, error: " + err.Error(),
"data": map[string]string{},
Expand Down Expand Up @@ -90,16 +90,16 @@ func createHttpClient() *http.Client {
return url.Parse(apiUrl) // 127.0.0.1:8099
}

dialCtx := (&net.Dialer{
dialCtx := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
// DualStack: true,
// FallbackDelay: 5 * time.Second,
}).DialContext
}
transport := &http.Transport{
Proxy: proxy,

DialContext: dialCtx,
DialContext: dialCtx.DialContext,
MaxIdleConns: 100,

IdleConnTimeout: 90 * time.Second,
Expand All @@ -112,7 +112,7 @@ func createHttpClient() *http.Client {
return &http.Client{Transport: transport}
}

func responseJSON(w http.ResponseWriter, status int, data interface{}) {
func responseJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(status)

Expand Down
8 changes: 4 additions & 4 deletions binding/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ type (
// Binder interface
Binder interface {
Name() string
Bind(r *http.Request, obj interface{}) error
Bind(r *http.Request, obj any) error
}

// BinderFunc bind func, implement the Binder() interface
BinderFunc func(r *http.Request, obj interface{}) error
BinderFunc func(r *http.Request, obj any) error

// DataValidator interface
DataValidator interface {
Validate(i interface{}) error
Validate(i any) error
}
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func (fn BinderFunc) Name() string {
}

// BinderFunc implements the Binder interface
func (fn BinderFunc) Bind(r *http.Request, obj interface{}) error {
func (fn BinderFunc) Bind(r *http.Request, obj any) error {
return fn(r, obj)
}

Expand Down
21 changes: 10 additions & 11 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,28 @@ import (
var DefaultMaxMemory int64 = 32 << 20 // 32 MB

// MustBind auto bind request data to an struct ptr
func MustBind(r *http.Request, obj interface{}) {
func MustBind(r *http.Request, obj any) {
err := Auto(r, obj)
if err != nil {
panic(err)
}
}

// Bind auto bind request data to an struct ptr
func Bind(r *http.Request, obj interface{}) error {
func Bind(r *http.Request, obj any) error {
return Auto(r, obj)
}

// Auto bind request data to an struct ptr
//
// body, err := ioutil.ReadAll(c.Request().Body)
// if err != nil {
// c.Logger().Errorf("could not read request body: %v", err)
// }
// c.Set("request_body", body)
// // fix: can not read request body multiple times
// c.Request().Body = ioutil.NopCloser(bytes.NewReader(body))
//
func Auto(r *http.Request, obj interface{}) (err error) {
// body, err := ioutil.ReadAll(c.Request().Body)
// if err != nil {
// c.Logger().Errorf("could not read request body: %v", err)
// }
// c.Set("request_body", body)
// // fix: can not read request body multiple times
// c.Request().Body = ioutil.NopCloser(bytes.NewReader(body))
func Auto(r *http.Request, obj any) (err error) {
method := r.Method

// no body, query data binding. like GET DELETE OPTION ....
Expand Down
6 changes: 3 additions & 3 deletions binding/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (FormBinder) Name() string {
}

// Bind Form data from http.Request
func (b FormBinder) Bind(r *http.Request, ptr interface{}) error {
func (b FormBinder) Bind(r *http.Request, ptr any) error {
err := r.ParseForm()
if err != nil {
return err
Expand All @@ -31,12 +31,12 @@ func (b FormBinder) Bind(r *http.Request, ptr interface{}) error {
}

// BindValues data from url.Values
func (b FormBinder) BindValues(values url.Values, ptr interface{}) error {
func (b FormBinder) BindValues(values url.Values, ptr any) error {
return DecodeUrlValues(values, ptr, b.TagName)
}

// DecodeUrlValues data to struct
func DecodeUrlValues(values map[string][]string, ptr interface{}, tagName string) error {
func DecodeUrlValues(values map[string][]string, ptr any, tagName string) error {
dec := formam.NewDecoder(&formam.DecoderOptions{
TagName: tagName,
})
Expand Down
4 changes: 2 additions & 2 deletions binding/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func (HeaderBinder) Name() string {
}

// Bind Header data binding
func (b HeaderBinder) Bind(r *http.Request, ptr interface{}) error {
func (b HeaderBinder) Bind(r *http.Request, ptr any) error {
return DecodeUrlValues(r.Header, ptr, b.TagName)
}

// BindValues data from headers
func (b HeaderBinder) BindValues(headers map[string][]string, ptr interface{}) error {
func (b HeaderBinder) BindValues(headers map[string][]string, ptr any) error {
return DecodeUrlValues(headers, ptr, b.TagName)
}
6 changes: 3 additions & 3 deletions binding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ func (JSONBinder) Name() string {
}

// Bind JSON data from http.Request
func (JSONBinder) Bind(r *http.Request, ptr interface{}) error {
func (JSONBinder) Bind(r *http.Request, ptr any) error {
return decodeJSON(r.Body, ptr)
}

// BindBytes raw JSON data to struct
func (JSONBinder) BindBytes(bts []byte, ptr interface{}) error {
func (JSONBinder) BindBytes(bts []byte, ptr any) error {
return decodeJSON(strings.NewReader(string(bts)), ptr)
}

func decodeJSON(r io.Reader, ptr interface{}) error {
func decodeJSON(r io.Reader, ptr any) error {
err := json.NewDecoder(r).Decode(ptr)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions binding/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func (QueryBinder) Name() string {
}

// Bind Query data binder
func (b QueryBinder) Bind(r *http.Request, ptr interface{}) error {
func (b QueryBinder) Bind(r *http.Request, ptr any) error {
return DecodeUrlValues(r.URL.Query(), ptr, b.TagName)
}

// BindValues data from url.Values
func (b QueryBinder) BindValues(values url.Values, ptr interface{}) error {
func (b QueryBinder) BindValues(values url.Values, ptr any) error {
return DecodeUrlValues(values, ptr, b.TagName)
}
4 changes: 2 additions & 2 deletions binding/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var Validator DataValidator = &stdValidator{}
type stdValidator struct{}

// Validate the struct data, if fail return error
func (sv *stdValidator) Validate(obj interface{}) error {
func (sv *stdValidator) Validate(obj any) error {
v := validate.New(obj)
if v.Validate() {
return nil
Expand All @@ -27,7 +27,7 @@ func ResetValidator() {
Validator = &stdValidator{}
}

func validating(obj interface{}) error {
func validating(obj any) error {
// if Validator is nil, dont validate.
if Validator == nil {
return nil
Expand Down
6 changes: 3 additions & 3 deletions binding/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ func (XMLBinder) Name() string {
}

// Bind XML data binder
func (XMLBinder) Bind(r *http.Request, obj interface{}) error {
func (XMLBinder) Bind(r *http.Request, obj any) error {
return decodeXML(r.Body, obj)
}

// BindBytes raw JSON data to struct
func (XMLBinder) BindBytes(bts []byte, ptr interface{}) error {
func (XMLBinder) BindBytes(bts []byte, ptr any) error {
return decodeXML(strings.NewReader(string(bts)), ptr)
}

func decodeXML(r io.Reader, obj interface{}) error {
func decodeXML(r io.Reader, obj any) error {
err := xml.NewDecoder(r).Decode(obj)
if err != nil {
return err
Expand Down
22 changes: 11 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const (
abortIndex int8 = 63
)

// M a short name for `map[string]interface{}`
type M map[string]interface{}
// M a short name for `map[string]any`
type M map[string]any

// Context for http server
type Context struct {
Expand All @@ -41,7 +41,7 @@ type Context struct {
// current router instance
router *Router
// context data, you can save some custom data.
data map[string]interface{}
data map[string]any
// all handlers for current request.
// call priority: global -> group -> route -> main handler
// Notice: last always is main handler of the matched route.
Expand Down Expand Up @@ -123,27 +123,27 @@ func (c *Context) Copy() *Context {
// c.Set("key", "value")
// // ...
// val := c.Get("key") // "value"
func (c *Context) Set(key string, val interface{}) {
func (c *Context) Set(key string, val any) {
if c.data == nil {
c.data = make(map[string]interface{})
c.data = make(map[string]any)
}

c.data[key] = val
}

// Get a value from context data
func (c *Context) Get(key string) (v interface{}, ok bool) {
func (c *Context) Get(key string) (v any, ok bool) {
v, ok = c.data[key]
return
}

// MustGet a value from context data
func (c *Context) MustGet(key string) interface{} {
func (c *Context) MustGet(key string) any {
return c.data[key]
}

// Data get all context data
func (c *Context) Data() map[string]interface{} {
func (c *Context) Data() map[string]any {
return c.data
}

Expand Down Expand Up @@ -357,15 +357,15 @@ func (c *Context) SaveFile(file *multipart.FileHeader, dst string) error {
// c.Req = r.WithContext(context.WithValue(r.Context(), "key", "value"))
// // ...
// val := c.ReqCtxValue("key") // "value"
func (c *Context) ReqCtxValue(key interface{}) interface{} {
func (c *Context) ReqCtxValue(key any) any {
return c.Req.Context().Value(key)
}

// WithReqCtxValue with request ctx Value.
// Usage:
//
// ctx.WithReqCtxValue()
func (c *Context) WithReqCtxValue(key, val interface{}) {
func (c *Context) WithReqCtxValue(key, val any) {
r := c.Req
c.Req = r.WithContext(context.WithValue(r.Context(), key, val))
}
Expand Down Expand Up @@ -580,7 +580,7 @@ func (c *Context) Err() error {
// Value returns the value associated with this context for key, or nil
// if no value is associated with key. Successive calls to Value with
// the same key returns the same result.
func (c *Context) Value(key interface{}) interface{} {
func (c *Context) Value(key any) any {
if key == 0 || key == nil {
return c.Req
}
Expand Down
21 changes: 14 additions & 7 deletions context_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import "github.com/gookit/rux/binding"
// ShouldBind bind request data to an struct, will auto call validator
//
// Usage:
//
// err := c.ShouldBind(&user, binding.JSON)
func (c *Context) ShouldBind(obj interface{}, binder binding.Binder) error {
func (c *Context) ShouldBind(obj any, binder binding.Binder) error {
return binder.Bind(c.Req, obj)
}

// MustBind bind request data to an struct, will auto call validator
//
// Usage:
//
// c.MustBind(&user, binding.Json)
func (c *Context) MustBind(obj interface{}, binder binding.Binder) {
func (c *Context) MustBind(obj any, binder binding.Binder) {
err := binder.Bind(c.Req, obj)
if err != nil {
panic(err)
Expand All @@ -24,17 +26,19 @@ func (c *Context) MustBind(obj interface{}, binder binding.Binder) {
// AutoBind auto bind request data to an struct, will auto select binding.Binder by content-type
//
// Usage:
//
// err := c.AutoBind(&user)
func (c *Context) AutoBind(obj interface{}) error {
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()
//
// Usage:
//
// err := c.Bind(&user)
func (c *Context) Bind(obj interface{}) error {
func (c *Context) Bind(obj any) error {
return binding.Auto(c.Req, obj)
}

Expand All @@ -45,23 +49,26 @@ func (c *Context) Bind(obj interface{}) error {
// BindForm request data to an struct, will auto call validator
//
// Usage:
//
// err := c.BindForm(&user)
func (c *Context) BindForm(obj interface{}) error {
func (c *Context) BindForm(obj any) error {
return binding.Form.Bind(c.Req, obj)
}

// BindJSON request data to an struct, will auto call validator
//
// Usage:
//
// err := c.BindJSON(&user)
func (c *Context) BindJSON(obj interface{}) error {
func (c *Context) BindJSON(obj any) error {
return binding.JSON.Bind(c.Req, obj)
}

// BindXML request data to an struct, will auto call validator
//
// Usage:
//
// err := c.BindXML(&user)
func (c *Context) BindXML(obj interface{}) error {
func (c *Context) BindXML(obj any) error {
return binding.XML.Bind(c.Req, obj)
}
Loading

0 comments on commit e38d70e

Please sign in to comment.