diff --git a/atreugo.go b/atreugo.go index adfd20e..c81bce9 100644 --- a/atreugo.go +++ b/atreugo.go @@ -63,7 +63,7 @@ func New(cfg Config) *Atreugo { } if cfg.PanicView != nil { - r.router.PanicHandler = func(ctx *fasthttp.RequestCtx, err interface{}) { + r.router.PanicHandler = func(ctx *fasthttp.RequestCtx, err any) { actx := AcquireRequestCtx(ctx) cfg.PanicView(actx, err) ReleaseRequestCtx(actx) diff --git a/atreugo_test.go b/atreugo_test.go index c2997ea..f63b505 100644 --- a/atreugo_test.go +++ b/atreugo_test.go @@ -48,7 +48,7 @@ func Test_New(t *testing.T) { //nolint:funlen,gocognit,gocyclo err bool } - jsonMarshalFunc := func(_ io.Writer, _ interface{}) error { + jsonMarshalFunc := func(_ io.Writer, _ any) error { return nil } notFoundView := func(_ *RequestCtx) error { @@ -59,7 +59,7 @@ func Test_New(t *testing.T) { //nolint:funlen,gocognit,gocyclo } panicErr := errors.New("error") - panicView := func(ctx *RequestCtx, err interface{}) { + panicView := func(ctx *RequestCtx, err any) { ctx.Error(fmt.Sprint(err), fasthttp.StatusInternalServerError) } diff --git a/context.go b/context.go index c6cac76..e41a1e8 100644 --- a/context.go +++ b/context.go @@ -16,7 +16,7 @@ var ( attachedCtxKey = fmt.Sprintf("__attachedCtx::%s__", bytes.Rand(make([]byte, 15))) requestCtxPool = sync.Pool{ - New: func() interface{} { + New: func() any { ctx := new(RequestCtx) ctx.jsonMarshalFunc = defaultJSONMarshalFunc @@ -104,7 +104,7 @@ func (ctx *RequestCtx) MatchedRoutePath() []byte { // the same key returns the same result. // // WARNING: The provided key should not be of type string or any other built-in -// to avoid extra allocating when assigning to an interface{}, context keys often +// to avoid extra allocating when assigning to an any, context keys often // have concrete type struct{}. Alternatively, exported context key variables' static // type should be a pointer or interface. // @@ -119,7 +119,7 @@ func (ctx *RequestCtx) MatchedRoutePath() []byte { // ctx.Value("myKey") // // to avoid extra allocation. -func (ctx *RequestCtx) Value(key interface{}) interface{} { +func (ctx *RequestCtx) Value(key any) any { if atomic.CompareAndSwapInt32(&ctx.searchingOnAttachedCtx, 0, 1) { defer atomic.StoreInt32(&ctx.searchingOnAttachedCtx, 0) diff --git a/errors.go b/errors.go index 0dc12cf..17086a0 100644 --- a/errors.go +++ b/errors.go @@ -8,7 +8,7 @@ func wrapError(err error, message string) error { return fmt.Errorf("%s: %w", message, err) } -func wrapErrorf(err error, message string, args ...interface{}) error { +func wrapErrorf(err error, message string, args ...any) error { message = fmt.Sprintf(message, args...) return wrapError(err, message) diff --git a/response.go b/response.go index e8fd7ff..f819cd5 100644 --- a/response.go +++ b/response.go @@ -8,12 +8,12 @@ import ( "github.com/valyala/fasthttp" ) -func defaultJSONMarshalFunc(w io.Writer, body interface{}) error { +func defaultJSONMarshalFunc(w io.Writer, body any) error { return json.NewEncoder(w).Encode(body) // nolint:wrapcheck } // JSONResponse return response with body in json format. -func (ctx *RequestCtx) JSONResponse(body interface{}, statusCode ...int) error { +func (ctx *RequestCtx) JSONResponse(body any, statusCode ...int) error { ctx.Response.Header.SetContentType("application/json") if len(statusCode) > 0 { diff --git a/response_test.go b/response_test.go index 0f41b6f..09fa3dd 100644 --- a/response_test.go +++ b/response_test.go @@ -24,7 +24,7 @@ func (cj customJSON) MarshalJSON() ([]byte, error) { func TestJSONResponse(t *testing.T) { //nolint:funlen type args struct { - body interface{} + body any statusCode int jsonMarshalFunc JSONMarshalFunc } @@ -74,7 +74,7 @@ func TestJSONResponse(t *testing.T) { //nolint:funlen args: args{ body: "my custom response", statusCode: 200, - jsonMarshalFunc: func(w io.Writer, value interface{}) error { + jsonMarshalFunc: func(w io.Writer, value any) error { _, err := w.Write([]byte(fmt.Sprint(value))) return err // nolint:wrapcheck diff --git a/router_test.go b/router_test.go index 5c97f42..3201c2e 100644 --- a/router_test.go +++ b/router_test.go @@ -47,7 +47,7 @@ func randomHTTPMethod() string { return httpMethods[n.Int64()] } -func catchPanic(testFunc func()) (recv interface{}) { +func catchPanic(testFunc func()) (recv any) { defer func() { recv = recover() }() diff --git a/types.go b/types.go index 0f11669..5265ec7 100644 --- a/types.go +++ b/types.go @@ -15,15 +15,15 @@ import ( // Logger is used for logging messages. type Logger interface { - Print(v ...interface{}) - Printf(format string, args ...interface{}) + Print(v ...any) + Printf(format string, args ...any) } type preforkServer interface { ListenAndServe(addr string) error } -type JSONMarshalFunc func(w io.Writer, value interface{}) error +type JSONMarshalFunc func(w io.Writer, value any) error // Atreugo implements high performance HTTP server // @@ -557,7 +557,7 @@ type View func(*RequestCtx) error type ErrorView func(*RequestCtx, error, int) // PanicView must process panics recovered from views, if it's defined in configuration. -type PanicView func(*RequestCtx, interface{}) +type PanicView func(*RequestCtx, any) // Middleware must process all incoming requests before/after defined views. type Middleware View @@ -586,4 +586,4 @@ type Middlewares struct { type PathRewriteFunc func(ctx *RequestCtx) []byte // JSON is a map whose key is a string and whose value an interface. -type JSON map[string]interface{} +type JSON map[string]any diff --git a/utils.go b/utils.go index d5385a2..60813e2 100644 --- a/utils.go +++ b/utils.go @@ -9,7 +9,7 @@ import ( "github.com/valyala/fasthttp/prefork" ) -func panicf(s string, args ...interface{}) { +func panicf(s string, args ...any) { panic(fmt.Sprintf(s, args...)) } @@ -25,11 +25,11 @@ func viewToHandler(view View, errorView ErrorView) fasthttp.RequestHandler { } } -func isEqual(v1, v2 interface{}) bool { +func isEqual(v1, v2 any) bool { return reflect.ValueOf(v1).Pointer() == reflect.ValueOf(v2).Pointer() } -func isNil(v interface{}) bool { +func isNil(v any) bool { return reflect.ValueOf(v).IsNil() }