diff --git a/_examples/proxyreq/main.go b/_examples/proxyreq/main.go index a6c05c7..7676590 100644 --- a/_examples/proxyreq/main.go +++ b/_examples/proxyreq/main.go @@ -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{}, @@ -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{}, @@ -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, @@ -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) diff --git a/binding/binder.go b/binding/binder.go index 7886171..e820892 100644 --- a/binding/binder.go +++ b/binding/binder.go @@ -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 } ) @@ -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) } diff --git a/binding/binding.go b/binding/binding.go index 1ec6104..d42a0d5 100644 --- a/binding/binding.go +++ b/binding/binding.go @@ -11,7 +11,7 @@ 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) @@ -19,21 +19,20 @@ func MustBind(r *http.Request, obj interface{}) { } // 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 .... diff --git a/binding/form.go b/binding/form.go index e88eb71..ed2e31f 100644 --- a/binding/form.go +++ b/binding/form.go @@ -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 @@ -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, }) diff --git a/binding/header.go b/binding/header.go index cd9294e..0b4891a 100644 --- a/binding/header.go +++ b/binding/header.go @@ -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) } diff --git a/binding/json.go b/binding/json.go index d8b5d72..504c822 100644 --- a/binding/json.go +++ b/binding/json.go @@ -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 diff --git a/binding/query.go b/binding/query.go index 3bac035..bbfba22 100644 --- a/binding/query.go +++ b/binding/query.go @@ -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) } diff --git a/binding/validate.go b/binding/validate.go index f4408ad..3d08298 100644 --- a/binding/validate.go +++ b/binding/validate.go @@ -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 @@ -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 diff --git a/binding/xml.go b/binding/xml.go index 2879a0b..f82c6ed 100644 --- a/binding/xml.go +++ b/binding/xml.go @@ -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 diff --git a/context.go b/context.go index be20f98..a3c4cad 100644 --- a/context.go +++ b/context.go @@ -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 { @@ -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. @@ -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 } @@ -357,7 +357,7 @@ 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) } @@ -365,7 +365,7 @@ func (c *Context) ReqCtxValue(key interface{}) interface{} { // 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)) } @@ -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 } diff --git a/context_binding.go b/context_binding.go index a699ffa..ed4d605 100644 --- a/context_binding.go +++ b/context_binding.go @@ -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) @@ -24,8 +26,9 @@ 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) } @@ -33,8 +36,9 @@ func (c *Context) AutoBind(obj interface{}) error { // 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) } @@ -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) } diff --git a/context_render.go b/context_render.go index d05ac1a..01ec79d 100644 --- a/context_render.go +++ b/context_render.go @@ -13,18 +13,18 @@ import ( ) // ShouldRender render and response to client -func (c *Context) ShouldRender(status int, obj interface{}, renderer render.Renderer) error { +func (c *Context) ShouldRender(status int, obj any, renderer render.Renderer) error { c.SetStatus(status) return renderer.Render(c.Resp, obj) } // MustRender render and response to client -func (c *Context) MustRender(status int, obj interface{}, renderer render.Renderer) { +func (c *Context) MustRender(status int, obj any, renderer render.Renderer) { c.Respond(status, obj, renderer) } // Respond render and response to client -func (c *Context) Respond(status int, obj interface{}, renderer render.Renderer) { +func (c *Context) Respond(status int, obj any, renderer render.Renderer) { c.SetStatus(status) err := renderer.Render(c.Resp, obj) @@ -104,7 +104,7 @@ func (c *Context) Stream(status int, contentType string, r io.Reader) { } // JSON writes out a JSON response. -func (c *Context) JSON(status int, obj interface{}) { +func (c *Context) JSON(status int, obj any) { c.Respond(status, obj, render.JSONRenderer{}) } @@ -114,7 +114,7 @@ func (c *Context) JSONBytes(status int, bs []byte) { } // XML output xml response. -func (c *Context) XML(status int, obj interface{}, indents ...string) { +func (c *Context) XML(status int, obj any, indents ...string) { var indent string if len(indents) > 0 && indents[0] != "" { indent = indents[0] @@ -124,7 +124,7 @@ func (c *Context) XML(status int, obj interface{}, indents ...string) { } // JSONP is JSONP response. -func (c *Context) JSONP(status int, callback string, obj interface{}) { +func (c *Context) JSONP(status int, callback string, obj any) { c.Respond(status, obj, render.JSONPRenderer{Callback: callback}) } @@ -156,7 +156,8 @@ func (c *Context) FileContent(file string, names ...string) { // Attachment a file to response. // Usage: -// c.Attachment("path/to/some.zip", "new-name.zip") +// +// c.Attachment("path/to/some.zip", "new-name.zip") func (c *Context) Attachment(srcFile, outName string) { c.dispositionContent(c.Resp, http.StatusOK, outName, false) c.FileContent(srcFile) @@ -164,7 +165,8 @@ func (c *Context) Attachment(srcFile, outName string) { // Inline file content. // Usage: -// c.Inline("testdata/site.md", "new-name.md") +// +// c.Inline("testdata/site.md", "new-name.md") func (c *Context) Inline(srcFile, outName string) { c.dispositionContent(c.Resp, http.StatusOK, outName, true) c.FileContent(srcFile) @@ -172,8 +174,9 @@ func (c *Context) Inline(srcFile, outName string) { // Binary serve data as Binary response. // Usage: -// in, _ := os.Open("./README.md") -// r.Binary(http.StatusOK, in, "readme.md", true) +// +// in, _ := os.Open("./README.md") +// r.Binary(http.StatusOK, in, "readme.md", true) func (c *Context) Binary(status int, in io.ReadSeeker, outName string, inline bool) { c.dispositionContent(c.Resp, status, outName, inline) diff --git a/dispatch_test.go b/dispatch_test.go index dfc12b4..5d48292 100644 --- a/dispatch_test.go +++ b/dispatch_test.go @@ -35,7 +35,7 @@ func (a *aStr) reset() { a.str = "" } -func (a *aStr) set(s ...interface{}) { +func (a *aStr) set(s ...any) { a.str = fmt.Sprint(s...) } diff --git a/extends.go b/extends.go index 8ad92b8..baaa1ae 100644 --- a/extends.go +++ b/extends.go @@ -14,12 +14,12 @@ import ( // Renderer interface type Renderer interface { - Render(io.Writer, string, interface{}, *Context) error + Render(io.Writer, string, any, *Context) error } // Validator interface type Validator interface { - Validate(i interface{}) error + Validate(i any) error } /************************************************************* @@ -29,7 +29,7 @@ type Validator interface { // Render context template. // // please use ShouldRender() instead -func (c *Context) Render(status int, name string, data interface{}) (err error) { +func (c *Context) Render(status int, name string, data any) (err error) { if c.router.Renderer == nil { return errors.New("renderer not registered") } @@ -46,7 +46,7 @@ func (c *Context) Render(status int, name string, data interface{}) (err error) // Validate context validator // // Deprecated: please use ShouldBind() instead, it will auto call validator. -func (c *Context) Validate(i interface{}) error { +func (c *Context) Validate(i any) error { if c.Router().Validator == nil { return errors.New("validator not registered") } diff --git a/extends_test.go b/extends_test.go index f3b22bb..0cd6a1f 100644 --- a/extends_test.go +++ b/extends_test.go @@ -177,7 +177,7 @@ func TestBuildRequestUrl_ErrorArgs(t *testing.T) { type MyValidator string -func (mv *MyValidator) Validate(v interface{}) error { +func (mv *MyValidator) Validate(v any) error { var rt = reflect.TypeOf(v) var rv = reflect.ValueOf(v) var field = rt.Elem().Field(0) @@ -226,7 +226,7 @@ func TestContext_Validator(t *testing.T) { type MyRenderer string -func (mr *MyRenderer) Render(w io.Writer, name string, data interface{}, ctx *Context) error { +func (mr *MyRenderer) Render(w io.Writer, name string, data any, ctx *Context) error { tpl, err := template.New(name).Funcs(template.FuncMap{ "Upper": strings.ToUpper, }).Parse("{{.Name|Upper}}, ID is {{ .ID}}") @@ -244,7 +244,7 @@ func TestContext_Renderer(t *testing.T) { r.Renderer = new(MyRenderer) r.Any("/renderer", func(c *Context) { - c.Render(200, "index", M{ + _ = c.Render(200, "index", M{ "ID": 100, "Name": "admin", }) diff --git a/render/json.go b/render/json.go index 0cd9017..6e0fc19 100644 --- a/render/json.go +++ b/render/json.go @@ -9,7 +9,7 @@ import ( // JSONRenderer for response JSON content to client type JSONRenderer struct { - // Data interface{} + // Data any // Indent string for encode Indent string // NotEscape HTML string @@ -24,7 +24,7 @@ func NewJSONIndented() JSONRenderer { } // Render JSON to client -func (r JSONRenderer) Render(w http.ResponseWriter, obj interface{}) (err error) { +func (r JSONRenderer) Render(w http.ResponseWriter, obj any) (err error) { writeContentType(w, httpctype.JSON) enc := json.NewEncoder(w) @@ -40,12 +40,12 @@ func (r JSONRenderer) Render(w http.ResponseWriter, obj interface{}) (err error) } // JSON response rendering -func JSON(w http.ResponseWriter, obj interface{}) error { +func JSON(w http.ResponseWriter, obj any) error { return JSONRenderer{}.Render(w, obj) } // JSONIndented response rendering with indent -func JSONIndented(w http.ResponseWriter, obj interface{}) error { +func JSONIndented(w http.ResponseWriter, obj any) error { return JSONRenderer{Indent: PrettyIndent}.Render(w, obj) } @@ -55,7 +55,7 @@ type JSONPRenderer struct { } // Render JSONP to client -func (r JSONPRenderer) Render(w http.ResponseWriter, obj interface{}) (err error) { +func (r JSONPRenderer) Render(w http.ResponseWriter, obj any) (err error) { writeContentType(w, httpctype.JSONP) if _, err = w.Write([]byte(r.Callback + "(")); err != nil { @@ -72,6 +72,6 @@ func (r JSONPRenderer) Render(w http.ResponseWriter, obj interface{}) (err error } // JSONP response rendering -func JSONP(callback string, obj interface{}, w http.ResponseWriter) error { +func JSONP(callback string, obj any, w http.ResponseWriter) error { return JSONPRenderer{Callback: callback}.Render(w, obj) } diff --git a/render/render.go b/render/render.go index 0adae3a..2bef21d 100644 --- a/render/render.go +++ b/render/render.go @@ -17,14 +17,14 @@ var FallbackType = httpctype.MIMEText // Renderer interface type Renderer interface { - Render(w http.ResponseWriter, obj interface{}) error + Render(w http.ResponseWriter, obj any) error } // RendererFunc definition -type RendererFunc func(w http.ResponseWriter, obj interface{}) error +type RendererFunc func(w http.ResponseWriter, obj any) error // Render to http.ResponseWriter -func (fn RendererFunc) Render(w http.ResponseWriter, obj interface{}) error { +func (fn RendererFunc) Render(w http.ResponseWriter, obj any) error { return fn(w, obj) } @@ -64,7 +64,7 @@ func Blob(w http.ResponseWriter, contentType string, data []byte) (err error) { } // Auto render data to response -func Auto(w http.ResponseWriter, r *http.Request, obj interface{}) (err error) { +func Auto(w http.ResponseWriter, r *http.Request, obj any) (err error) { accepts := parseAccept(r.Header.Get("Accept")) // fallback use FallbackType @@ -107,7 +107,7 @@ func Auto(w http.ResponseWriter, r *http.Request, obj interface{}) (err error) { return } -func responseText(w http.ResponseWriter, obj interface{}) error { +func responseText(w http.ResponseWriter, obj any) error { switch typVal := obj.(type) { case string: return Text(w, typVal) diff --git a/render/template.go b/render/template.go index 02ab344..124eb73 100644 --- a/render/template.go +++ b/render/template.go @@ -12,7 +12,7 @@ type ViewRenderer struct { } // Render template to client -func (r ViewRenderer) Render(w http.ResponseWriter, obj interface{}) (err error) { +func (r ViewRenderer) Render(w http.ResponseWriter, obj any) (err error) { writeContentType(w, httpctype.HTML) return } diff --git a/render/xml.go b/render/xml.go index 36ba89a..ada10a1 100644 --- a/render/xml.go +++ b/render/xml.go @@ -9,12 +9,12 @@ import ( // XMLRenderer for response XML content to client type XMLRenderer struct { - // Data interface{} + // Data any Indent string } // Render XML to client -func (r XMLRenderer) Render(w http.ResponseWriter, obj interface{}) error { +func (r XMLRenderer) Render(w http.ResponseWriter, obj any) error { writeContentType(w, httpctype.XML) enc := xml.NewEncoder(w) @@ -31,11 +31,11 @@ func (r XMLRenderer) Render(w http.ResponseWriter, obj interface{}) error { } // XML response rendering -func XML(w http.ResponseWriter, obj interface{}) error { +func XML(w http.ResponseWriter, obj any) error { return XMLRenderer{}.Render(w, obj) } // XMLPretty response rendering with indent -func XMLPretty(w http.ResponseWriter, obj interface{}) error { +func XMLPretty(w http.ResponseWriter, obj any) error { return XMLRenderer{Indent: PrettyIndent}.Render(w, obj) } diff --git a/route.go b/route.go index ac2e921..ee2ae8d 100644 --- a/route.go +++ b/route.go @@ -80,7 +80,7 @@ type Route struct { handlers HandlersChain // Opts some options data for the route - Opts map[string]interface{} + Opts map[string]any // defaults } @@ -192,7 +192,7 @@ func (r *Route) Info() RouteInfo { } // ToURL build request URL, can with path vars -func (r *Route) ToURL(buildArgs ...interface{}) *url.URL { +func (r *Route) ToURL(buildArgs ...any) *url.URL { var URLBuilder *BuildRequestURL //noinspection GoNilness path := r.path @@ -229,12 +229,12 @@ func (r *Route) ToURL(buildArgs ...interface{}) *url.URL { } // BuildRequestURL alias of the method BuildRequestURL() -func (r *Router) BuildRequestURL(name string, buildArgs ...interface{}) *url.URL { +func (r *Router) BuildRequestURL(name string, buildArgs ...any) *url.URL { return r.BuildURL(name, buildArgs...) } // BuildURL build Request URL one arg can be set buildRequestURL or rux.M -func (r *Router) BuildURL(name string, buildArgs ...interface{}) *url.URL { +func (r *Router) BuildURL(name string, buildArgs ...any) *url.URL { route := r.GetRoute(name) if route == nil { panicf("BuildRequestURL get route is nil(name: %s)", name) @@ -264,11 +264,13 @@ func (r *Route) goodInfo() { // check custom var regex string. // ERROR: -// "{id:(\d+)}" -> "(\d+)" +// +// "{id:(\d+)}" -> "(\d+)" // // RIGHT: -// "{id:\d+}" -// "{id:(?:\d+)}" +// +// "{id:\d+}" +// "{id:(?:\d+)}" func (r *Route) goodRegexString(n, v string) { pos := strings.IndexByte(v, '(') diff --git a/router.go b/router.go index 1bd551f..d17cfba 100644 --- a/router.go +++ b/router.go @@ -129,7 +129,7 @@ func New(options ...func(*Router)) *Router { // with some options router.WithOptions(options...) - router.ctxPool.New = func() interface{} { + router.ctxPool.New = func() any { return &Context{index: -1, router: router} } @@ -273,7 +273,7 @@ func (r *Router) Controller(basePath string, controller ControllerFace, middles // GET /resource/{id}/edit edit resource_edit // PUT/PATCH /resource/{id} update resource_update // DELETE /resource/{id} delete resource_delete -func (r *Router) Resource(basePath string, controller interface{}, middles ...HandlerFunc) { +func (r *Router) Resource(basePath string, controller any, middles ...HandlerFunc) { cv := reflect.ValueOf(controller) ct := cv.Type() diff --git a/testdata/idea.md b/testdata/idea.md index 509f810..b1041d3 100644 --- a/testdata/idea.md +++ b/testdata/idea.md @@ -37,7 +37,7 @@ func (*MyController) About(c *rux.Context) { ```text // AutoLoad auto register routes by a controller struct -func (r *Router) AutoLoad(prefix string, obj interface{}, middles ...HandlerFunc) { +func (r *Router) AutoLoad(prefix string, obj any, middles ...HandlerFunc) { cv := reflect.ValueOf(obj) if cv.Kind() != reflect.Ptr { panic("autoload controller must type ptr") diff --git a/utils.go b/utils.go index fb468f1..836865e 100644 --- a/utils.go +++ b/utils.go @@ -49,7 +49,7 @@ func isFixedPath(s string) bool { return strings.IndexByte(s, '{') < 0 && strings.IndexByte(s, '[') < 0 } -func simpleFmtPath(path string) string { +func simpleFmtPath(path string) string { path = strings.TrimSpace(path) if path == "" { @@ -110,7 +110,7 @@ func quotePointChar(path string) string { return path } -func nameOfFunction(f interface{}) string { +func nameOfFunction(f any) string { return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() } @@ -121,7 +121,7 @@ func debugPrintRoute(route *Route) { debugPrint(route.String()) } -func panicf(f string, v ...interface{}) { +func panicf(f string, v ...any) { panic(fmt.Sprintf(f, v...)) } @@ -131,7 +131,7 @@ func debugPrintError(err error) { } } -func debugPrint(f string, v ...interface{}) { +func debugPrint(f string, v ...any) { if debug { // fmt.Printf("[RUX-DEBUG] %s %s\n", time.Now().Format("2006-01-02 15:04:05"), msg) color.Printf("[RUX-DEBUG] %s\n", fmt.Sprintf(f, v...)) @@ -176,7 +176,7 @@ func formatMethodsWithDefault(methods []string, defMethod string) []string { return methods } -func toString(i interface{}) string { +func toString(i any) string { if i == nil { return "" }