Skip to content

Commit

Permalink
refactor: change interface{} to any
Browse files Browse the repository at this point in the history
  • Loading branch information
savsgio committed Jul 4, 2024
1 parent 8cf43fe commit b984460
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion atreugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions atreugo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
//
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}()
Expand Down
10 changes: 5 additions & 5 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...))
}

Expand All @@ -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()
}

Expand Down

0 comments on commit b984460

Please sign in to comment.