Skip to content

Commit

Permalink
chore: Use 'any' instead of 'interface{}' (valyala#1666)
Browse files Browse the repository at this point in the history
gofmt -w -r "interface{} -> any" -l .
  • Loading branch information
alexandear authored Nov 24, 2023
1 parent d3397c6 commit f196617
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 60 deletions.
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ linters:
- nonamedreturns
- paralleltest
- perfsprint
- revive
- stylecheck
- testableexamples
- testpackage
Expand Down Expand Up @@ -66,3 +65,8 @@ linters-settings:

# Show all issues with the same text.
max-same-issues: 0

revive:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
rules:
- name: use-any
2 changes: 1 addition & 1 deletion args.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ReleaseArgs(a *Args) {
}

var argsPool = &sync.Pool{
New: func() interface{} {
New: func() any {
return &Args{}
},
}
Expand Down
6 changes: 3 additions & 3 deletions brotli.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ func WriteBrotliLevel(w io.Writer, p []byte, level int) (int, error) {

var (
stacklessWriteBrotliOnce sync.Once
stacklessWriteBrotliFunc func(ctx interface{}) bool
stacklessWriteBrotliFunc func(ctx any) bool
)

func stacklessWriteBrotli(ctx interface{}) {
func stacklessWriteBrotli(ctx any) {
stacklessWriteBrotliOnce.Do(func() {
stacklessWriteBrotliFunc = stackless.NewFunc(nonblockingWriteBrotli)
})
stacklessWriteBrotliFunc(ctx)
}

func nonblockingWriteBrotli(ctxv interface{}) {
func nonblockingWriteBrotli(ctxv any) {
ctx := ctxv.(*compressCtx)
zw := acquireRealBrotliWriter(ctx.w, ctx.level)

Expand Down
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ func (c *HostClient) releaseConn(cc *clientConn) {
}

func (c *HostClient) acquireWriter(conn net.Conn) *bufio.Writer {
var v interface{}
var v any
if c.clientWriterPool != nil {
v = c.clientWriterPool.Get()
if v == nil {
Expand Down Expand Up @@ -1749,7 +1749,7 @@ func (c *HostClient) releaseWriter(bw *bufio.Writer) {
}

func (c *HostClient) acquireReader(conn net.Conn) *bufio.Reader {
var v interface{}
var v any
if c.clientReaderPool != nil {
v = c.clientReaderPool.Get()
if v == nil {
Expand Down
12 changes: 6 additions & 6 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ func WriteGzipLevel(w io.Writer, p []byte, level int) (int, error) {

var (
stacklessWriteGzipOnce sync.Once
stacklessWriteGzipFunc func(ctx interface{}) bool
stacklessWriteGzipFunc func(ctx any) bool
)

func stacklessWriteGzip(ctx interface{}) {
func stacklessWriteGzip(ctx any) {
stacklessWriteGzipOnce.Do(func() {
stacklessWriteGzipFunc = stackless.NewFunc(nonblockingWriteGzip)
})
stacklessWriteGzipFunc(ctx)
}

func nonblockingWriteGzip(ctxv interface{}) {
func nonblockingWriteGzip(ctxv any) {
ctx := ctxv.(*compressCtx)
zw := acquireRealGzipWriter(ctx.w, ctx.level)

Expand Down Expand Up @@ -282,17 +282,17 @@ func WriteDeflateLevel(w io.Writer, p []byte, level int) (int, error) {

var (
stacklessWriteDeflateOnce sync.Once
stacklessWriteDeflateFunc func(ctx interface{}) bool
stacklessWriteDeflateFunc func(ctx any) bool
)

func stacklessWriteDeflate(ctx interface{}) {
func stacklessWriteDeflate(ctx any) {
stacklessWriteDeflateOnce.Do(func() {
stacklessWriteDeflateFunc = stackless.NewFunc(nonblockingWriteDeflate)
})
stacklessWriteDeflateFunc(ctx)
}

func nonblockingWriteDeflate(ctxv interface{}) {
func nonblockingWriteDeflate(ctxv any) {
ctx := ctxv.(*compressCtx)
zw := acquireRealDeflateWriter(ctx.w, ctx.level)

Expand Down
2 changes: 1 addition & 1 deletion cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func ReleaseCookie(c *Cookie) {
}

var cookiePool = &sync.Pool{
New: func() interface{} {
New: func() any {
return &Cookie{}
},
}
Expand Down
4 changes: 2 additions & 2 deletions expvarhandler/expvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestExpvarHandlerBasic(t *testing.T) {
t.Parallel()

expvar.Publish("customVar", expvar.Func(func() interface{} {
expvar.Publish("customVar", expvar.Func(func() any {
return "foobar"
}))

Expand All @@ -24,7 +24,7 @@ func TestExpvarHandlerBasic(t *testing.T) {

body := ctx.Response.Body()

var m map[string]interface{}
var m map[string]any
if err := json.Unmarshal(body, &m); err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion fasthttpadaptor/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestNewFastHTTPHandler(t *testing.T) {
}
}

func setContextValueMiddleware(next fasthttp.RequestHandler, key string, value interface{}) fasthttp.RequestHandler {
func setContextValueMiddleware(next fasthttp.RequestHandler, key string, value any) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
ctx.SetUserValue(key, value)
next(ctx)
Expand Down
2 changes: 1 addition & 1 deletion fasthttputil/pipeconns.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func releaseByteBuffer(b *byteBuffer) {
}

var byteBufferPool = &sync.Pool{
New: func() interface{} {
New: func() any {
return &byteBuffer{
b: make([]byte, 1024),
}
Expand Down
2 changes: 1 addition & 1 deletion fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TestLogger struct {
t *testing.T
}

func (t TestLogger) Printf(format string, args ...interface{}) {
func (t TestLogger) Printf(format string, args ...any) {
t.t.Logf(format, args...)
}

Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ func copyZeroAlloc(w io.Writer, r io.Reader) (int64, error) {
}

var copyBufPool = sync.Pool{
New: func() interface{} {
New: func() any {
return make([]byte, 4096)
},
}
Expand Down
2 changes: 1 addition & 1 deletion prefork/prefork.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
// Logger is used for logging formatted messages.
type Logger interface {
// Printf must have the same semantics as log.Printf.
Printf(format string, args ...interface{})
Printf(format string, args ...any)
}

// Prefork implements fasthttp server prefork
Expand Down
20 changes: 10 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ func (ctx *RequestCtx) Hijacked() bool {
// All the values are removed from ctx after returning from the top
// RequestHandler. Additionally, Close method is called on each value
// implementing io.Closer before removing the value from ctx.
func (ctx *RequestCtx) SetUserValue(key interface{}, value interface{}) {
func (ctx *RequestCtx) SetUserValue(key any, value any) {
ctx.userValues.Set(key, value)
}

Expand All @@ -687,26 +687,26 @@ func (ctx *RequestCtx) SetUserValue(key interface{}, value interface{}) {
// functions involved in request processing.
//
// All the values stored in ctx are deleted after returning from RequestHandler.
func (ctx *RequestCtx) SetUserValueBytes(key []byte, value interface{}) {
func (ctx *RequestCtx) SetUserValueBytes(key []byte, value any) {
ctx.userValues.SetBytes(key, value)
}

// UserValue returns the value stored via SetUserValue* under the given key.
func (ctx *RequestCtx) UserValue(key interface{}) interface{} {
func (ctx *RequestCtx) UserValue(key any) any {
return ctx.userValues.Get(key)
}

// UserValueBytes returns the value stored via SetUserValue*
// under the given key.
func (ctx *RequestCtx) UserValueBytes(key []byte) interface{} {
func (ctx *RequestCtx) UserValueBytes(key []byte) any {
return ctx.userValues.GetBytes(key)
}

// VisitUserValues calls visitor for each existing userValue with a key that is a string or []byte.
//
// visitor must not retain references to key and value after returning.
// Make key and/or value copies if you need storing them after returning.
func (ctx *RequestCtx) VisitUserValues(visitor func([]byte, interface{})) {
func (ctx *RequestCtx) VisitUserValues(visitor func([]byte, any)) {
for i, n := 0, len(ctx.userValues); i < n; i++ {
kv := &ctx.userValues[i]
if _, ok := kv.key.(string); ok {
Expand All @@ -719,7 +719,7 @@ func (ctx *RequestCtx) VisitUserValues(visitor func([]byte, interface{})) {
//
// visitor must not retain references to key and value after returning.
// Make key and/or value copies if you need storing them after returning.
func (ctx *RequestCtx) VisitUserValuesAll(visitor func(interface{}, interface{})) {
func (ctx *RequestCtx) VisitUserValuesAll(visitor func(any, any)) {
for i, n := 0, len(ctx.userValues); i < n; i++ {
kv := &ctx.userValues[i]
visitor(kv.key, kv.value)
Expand All @@ -732,7 +732,7 @@ func (ctx *RequestCtx) ResetUserValues() {
}

// RemoveUserValue removes the given key and the value under it in ctx.
func (ctx *RequestCtx) RemoveUserValue(key interface{}) {
func (ctx *RequestCtx) RemoveUserValue(key any) {
ctx.userValues.Remove(key)
}

Expand Down Expand Up @@ -854,7 +854,7 @@ func (r *firstByteReader) Read(b []byte) (int, error) {
// Logger is used for logging formatted messages.
type Logger interface {
// Printf must have the same semantics as log.Printf.
Printf(format string, args ...interface{})
Printf(format string, args ...any)
}

var ctxLoggerLock sync.Mutex
Expand All @@ -864,7 +864,7 @@ type ctxLogger struct {
logger Logger
}

func (cl *ctxLogger) Printf(format string, args ...interface{}) {
func (cl *ctxLogger) Printf(format string, args ...any) {
msg := fmt.Sprintf(format, args...)
ctxLoggerLock.Lock()
cl.logger.Printf("%.3f %s - %s", time.Since(cl.ctx.ConnTime()).Seconds(), cl.ctx.String(), msg)
Expand Down Expand Up @@ -2747,7 +2747,7 @@ func (ctx *RequestCtx) Err() error {
//
// This method is present to make RequestCtx implement the context interface.
// This method is the same as calling ctx.UserValue(key)
func (ctx *RequestCtx) Value(key interface{}) interface{} {
func (ctx *RequestCtx) Value(key any) any {
return ctx.UserValue(key)
}

Expand Down
4 changes: 2 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ func TestRequestCtxUserValue(t *testing.T) {
}
}
vlen := 0
ctx.VisitUserValues(func(key []byte, value interface{}) {
ctx.VisitUserValues(func(key []byte, value any) {
vlen++
v := ctx.UserValue(key)
if v != value {
Expand Down Expand Up @@ -4292,7 +4292,7 @@ type testLogger struct {
out string
}

func (cl *testLogger) Printf(format string, args ...interface{}) {
func (cl *testLogger) Printf(format string, args ...any) {
cl.lock.Lock()
cl.out += fmt.Sprintf(format, args...)[6:] + "\n"
cl.lock.Unlock()
Expand Down
8 changes: 4 additions & 4 deletions stackless/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
//
// The stackless wrapper returns false if the call cannot be processed
// at the moment due to high load.
func NewFunc(f func(ctx interface{})) func(ctx interface{}) bool {
func NewFunc(f func(ctx any)) func(ctx any) bool {
if f == nil {
// developer sanity-check
panic("BUG: f cannot be nil")
Expand All @@ -33,7 +33,7 @@ func NewFunc(f func(ctx interface{})) func(ctx interface{}) bool {
}
var once sync.Once

return func(ctx interface{}) bool {
return func(ctx any) bool {
once.Do(onceInit)
fw := getFuncWork()
fw.ctx = ctx
Expand All @@ -50,7 +50,7 @@ func NewFunc(f func(ctx interface{})) func(ctx interface{}) bool {
}
}

func funcWorker(funcWorkCh <-chan *funcWork, f func(ctx interface{})) {
func funcWorker(funcWorkCh <-chan *funcWork, f func(ctx any)) {
for fw := range funcWorkCh {
f(fw.ctx)
fw.done <- struct{}{}
Expand All @@ -75,6 +75,6 @@ func putFuncWork(fw *funcWork) {
var funcWorkPool sync.Pool

type funcWork struct {
ctx interface{}
ctx any
done chan struct{}
}
6 changes: 3 additions & 3 deletions stackless/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestNewFuncSimple(t *testing.T) {
t.Parallel()

var n uint64
f := NewFunc(func(ctx interface{}) {
f := NewFunc(func(ctx any) {
atomic.AddUint64(&n, uint64(ctx.(int)))
})

Expand All @@ -30,10 +30,10 @@ func TestNewFuncMulti(t *testing.T) {
t.Parallel()

var n1, n2 uint64
f1 := NewFunc(func(ctx interface{}) {
f1 := NewFunc(func(ctx any) {
atomic.AddUint64(&n1, uint64(ctx.(int)))
})
f2 := NewFunc(func(ctx interface{}) {
f2 := NewFunc(func(ctx any) {
atomic.AddUint64(&n2, uint64(ctx.(int)))
})

Expand Down
2 changes: 1 addition & 1 deletion stackless/func_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func BenchmarkFuncOverhead(b *testing.B) {
var n uint64
f := NewFunc(func(ctx interface{}) {
f := NewFunc(func(ctx any) {
atomic.AddUint64(&n, *(ctx.(*uint64)))
})
b.RunParallel(func(pb *testing.PB) {
Expand Down
6 changes: 3 additions & 3 deletions stackless/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ var errHighLoad = errors.New("cannot compress data due to high load")

var (
stacklessWriterFuncOnce sync.Once
stacklessWriterFuncFunc func(ctx interface{}) bool
stacklessWriterFuncFunc func(ctx any) bool
)

func stacklessWriterFunc(ctx interface{}) bool {
func stacklessWriterFunc(ctx any) bool {
stacklessWriterFuncOnce.Do(func() {
stacklessWriterFuncFunc = NewFunc(writerFunc)
})
return stacklessWriterFuncFunc(ctx)
}

func writerFunc(ctx interface{}) {
func writerFunc(ctx any) {
w := ctx.(*writer)
switch w.op {
case opWrite:
Expand Down
2 changes: 1 addition & 1 deletion streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func releaseRequestStream(rs *requestStream) {
}

var requestStreamPool = sync.Pool{
New: func() interface{} {
New: func() any {
return &requestStream{}
},
}
2 changes: 1 addition & 1 deletion tcpdialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (d *TCPDialer) tcpAddrsClean() {
for {
time.Sleep(time.Second)
t := time.Now()
d.tcpAddrsMap.Range(func(k, v interface{}) bool {
d.tcpAddrsMap.Range(func(k, v any) bool {
if e, ok := v.(*tcpAddrEntry); ok && t.Sub(e.resolveTime) > expireDuration {
d.tcpAddrsMap.Delete(k)
}
Expand Down
2 changes: 1 addition & 1 deletion uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ReleaseURI(u *URI) {
}

var uriPool = &sync.Pool{
New: func() interface{} {
New: func() any {
return &URI{}
},
}
Expand Down
Loading

0 comments on commit f196617

Please sign in to comment.