chore: change interface{} to any (#2818)

* chore: change interface{} to any

* chore: update goctl version to 1.5.0

* chore: update goctl deps
This commit is contained in:
Kevin Wan
2023-01-24 16:32:02 +08:00
committed by GitHub
parent 7e0ac77139
commit ae87114282
221 changed files with 1910 additions and 2207 deletions

View File

@@ -15,8 +15,8 @@ type (
// An ImmutableResource is used to manage an immutable resource.
ImmutableResource struct {
fetch func() (interface{}, error)
resource interface{}
fetch func() (any, error)
resource any
err error
lock sync.RWMutex
refreshInterval time.Duration
@@ -25,7 +25,7 @@ type (
)
// NewImmutableResource returns an ImmutableResource.
func NewImmutableResource(fn func() (interface{}, error), opts ...ImmutableResourceOption) *ImmutableResource {
func NewImmutableResource(fn func() (any, error), opts ...ImmutableResourceOption) *ImmutableResource {
// cannot use executors.LessExecutor because of cycle imports
ir := ImmutableResource{
fetch: fn,
@@ -39,7 +39,7 @@ func NewImmutableResource(fn func() (interface{}, error), opts ...ImmutableResou
}
// Get gets the immutable resource, fetches automatically if not loaded.
func (ir *ImmutableResource) Get() (interface{}, error) {
func (ir *ImmutableResource) Get() (any, error) {
ir.lock.RLock()
resource := ir.resource
ir.lock.RUnlock()

View File

@@ -10,7 +10,7 @@ import (
func TestImmutableResource(t *testing.T) {
var count int
r := NewImmutableResource(func() (interface{}, error) {
r := NewImmutableResource(func() (any, error) {
count++
return "hello", nil
})
@@ -29,7 +29,7 @@ func TestImmutableResource(t *testing.T) {
func TestImmutableResourceError(t *testing.T) {
var count int
r := NewImmutableResource(func() (interface{}, error) {
r := NewImmutableResource(func() (any, error) {
count++
return nil, errors.New("any")
})
@@ -58,7 +58,7 @@ func TestImmutableResourceError(t *testing.T) {
func TestImmutableResourceErrorRefreshAlways(t *testing.T) {
var count int
r := NewImmutableResource(func() (interface{}, error) {
r := NewImmutableResource(func() (any, error) {
count++
return nil, errors.New("any")
}, WithRefreshIntervalOnFailure(0))

View File

@@ -10,7 +10,7 @@ type (
// A ------->calls F with key and executes<------->returns
// B ------------------>calls F with key<--------->executes<---->returns
LockedCalls interface {
Do(key string, fn func() (interface{}, error)) (interface{}, error)
Do(key string, fn func() (any, error)) (any, error)
}
lockedGroup struct {
@@ -26,7 +26,7 @@ func NewLockedCalls() LockedCalls {
}
}
func (lg *lockedGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {
func (lg *lockedGroup) Do(key string, fn func() (any, error)) (any, error) {
begin:
lg.mu.Lock()
if wg, ok := lg.m[key]; ok {
@@ -38,7 +38,7 @@ begin:
return lg.makeCall(key, fn)
}
func (lg *lockedGroup) makeCall(key string, fn func() (interface{}, error)) (interface{}, error) {
func (lg *lockedGroup) makeCall(key string, fn func() (any, error)) (any, error) {
var wg sync.WaitGroup
wg.Add(1)
lg.m[key] = &wg

View File

@@ -10,7 +10,7 @@ import (
func TestLockedCallDo(t *testing.T) {
g := NewLockedCalls()
v, err := g.Do("key", func() (interface{}, error) {
v, err := g.Do("key", func() (any, error) {
return "bar", nil
})
if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want {
@@ -24,7 +24,7 @@ func TestLockedCallDo(t *testing.T) {
func TestLockedCallDoErr(t *testing.T) {
g := NewLockedCalls()
someErr := errors.New("some error")
v, err := g.Do("key", func() (interface{}, error) {
v, err := g.Do("key", func() (any, error) {
return nil, someErr
})
if err != someErr {
@@ -39,7 +39,7 @@ func TestLockedCallDoDupSuppress(t *testing.T) {
g := NewLockedCalls()
c := make(chan string)
var calls int
fn := func() (interface{}, error) {
fn := func() (any, error) {
calls++
ret := calls
<-c

View File

@@ -4,14 +4,14 @@ import "sync"
// A ManagedResource is used to manage a resource that might be broken and refetched, like a connection.
type ManagedResource struct {
resource interface{}
resource any
lock sync.RWMutex
generate func() interface{}
equals func(a, b interface{}) bool
generate func() any
equals func(a, b any) bool
}
// NewManagedResource returns a ManagedResource.
func NewManagedResource(generate func() interface{}, equals func(a, b interface{}) bool) *ManagedResource {
func NewManagedResource(generate func() any, equals func(a, b any) bool) *ManagedResource {
return &ManagedResource{
generate: generate,
equals: equals,
@@ -19,7 +19,7 @@ func NewManagedResource(generate func() interface{}, equals func(a, b interface{
}
// MarkBroken marks the resource broken.
func (mr *ManagedResource) MarkBroken(resource interface{}) {
func (mr *ManagedResource) MarkBroken(resource any) {
mr.lock.Lock()
defer mr.lock.Unlock()
@@ -29,7 +29,7 @@ func (mr *ManagedResource) MarkBroken(resource interface{}) {
}
// Take takes the resource, if not loaded, generates it.
func (mr *ManagedResource) Take() interface{} {
func (mr *ManagedResource) Take() any {
mr.lock.RLock()
resource := mr.resource
mr.lock.RUnlock()

View File

@@ -9,9 +9,9 @@ import (
func TestManagedResource(t *testing.T) {
var count int32
resource := NewManagedResource(func() interface{} {
resource := NewManagedResource(func() any {
return atomic.AddInt32(&count, 1)
}, func(a, b interface{}) bool {
}, func(a, b any) bool {
return a == b
})

View File

@@ -12,7 +12,7 @@ type (
PoolOption func(*Pool)
node struct {
item interface{}
item any
next *node
lastUsed time.Duration
}
@@ -29,13 +29,13 @@ type (
lock sync.Locker
cond *sync.Cond
head *node
create func() interface{}
destroy func(interface{})
create func() any
destroy func(any)
}
)
// NewPool returns a Pool.
func NewPool(n int, create func() interface{}, destroy func(interface{}), opts ...PoolOption) *Pool {
func NewPool(n int, create func() any, destroy func(any), opts ...PoolOption) *Pool {
if n <= 0 {
panic("pool size can't be negative or zero")
}
@@ -57,7 +57,7 @@ func NewPool(n int, create func() interface{}, destroy func(interface{}), opts .
}
// Get gets a resource.
func (p *Pool) Get() interface{} {
func (p *Pool) Get() any {
p.lock.Lock()
defer p.lock.Unlock()
@@ -84,7 +84,7 @@ func (p *Pool) Get() interface{} {
}
// Put puts a resource back.
func (p *Pool) Put(x interface{}) {
func (p *Pool) Put(x any) {
if x == nil {
return
}

View File

@@ -75,7 +75,7 @@ func TestPoolPopTooMany(t *testing.T) {
func TestPoolPopFirst(t *testing.T) {
var value int32
stack := NewPool(limit, func() interface{} {
stack := NewPool(limit, func() any {
return atomic.AddInt32(&value, 1)
}, destroy)
@@ -88,7 +88,7 @@ func TestPoolPopFirst(t *testing.T) {
func TestPoolWithMaxAge(t *testing.T) {
var value int32
stack := NewPool(limit, func() interface{} {
stack := NewPool(limit, func() any {
return atomic.AddInt32(&value, 1)
}, destroy, WithMaxAge(time.Millisecond))
@@ -107,9 +107,9 @@ func TestNewPoolPanics(t *testing.T) {
})
}
func create() interface{} {
func create() any {
return 1
}
func destroy(_ interface{}) {
func destroy(_ any) {
}

View File

@@ -43,7 +43,7 @@ func (manager *ResourceManager) Close() error {
// GetResource returns the resource associated with given key.
func (manager *ResourceManager) GetResource(key string, create func() (io.Closer, error)) (io.Closer, error) {
val, err := manager.singleFlight.Do(key, func() (interface{}, error) {
val, err := manager.singleFlight.Do(key, func() (any, error) {
manager.lock.RLock()
resource, ok := manager.resources[key]
manager.lock.RUnlock()

View File

@@ -10,13 +10,13 @@ type (
// A ------->calls F with key<------------------->returns val
// B --------------------->calls F with key------>returns val
SingleFlight interface {
Do(key string, fn func() (interface{}, error)) (interface{}, error)
DoEx(key string, fn func() (interface{}, error)) (interface{}, bool, error)
Do(key string, fn func() (any, error)) (any, error)
DoEx(key string, fn func() (any, error)) (any, bool, error)
}
call struct {
wg sync.WaitGroup
val interface{}
val any
err error
}
@@ -33,7 +33,7 @@ func NewSingleFlight() SingleFlight {
}
}
func (g *flightGroup) Do(key string, fn func() (interface{}, error)) (interface{}, error) {
func (g *flightGroup) Do(key string, fn func() (any, error)) (any, error) {
c, done := g.createCall(key)
if done {
return c.val, c.err
@@ -43,7 +43,7 @@ func (g *flightGroup) Do(key string, fn func() (interface{}, error)) (interface{
return c.val, c.err
}
func (g *flightGroup) DoEx(key string, fn func() (interface{}, error)) (val interface{}, fresh bool, err error) {
func (g *flightGroup) DoEx(key string, fn func() (any, error)) (val any, fresh bool, err error) {
c, done := g.createCall(key)
if done {
return c.val, false, c.err
@@ -69,7 +69,7 @@ func (g *flightGroup) createCall(key string) (c *call, done bool) {
return c, false
}
func (g *flightGroup) makeCall(c *call, key string, fn func() (interface{}, error)) {
func (g *flightGroup) makeCall(c *call, key string, fn func() (any, error)) {
defer func() {
g.lock.Lock()
delete(g.calls, key)

View File

@@ -11,7 +11,7 @@ import (
func TestExclusiveCallDo(t *testing.T) {
g := NewSingleFlight()
v, err := g.Do("key", func() (interface{}, error) {
v, err := g.Do("key", func() (any, error) {
return "bar", nil
})
if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want {
@@ -25,7 +25,7 @@ func TestExclusiveCallDo(t *testing.T) {
func TestExclusiveCallDoErr(t *testing.T) {
g := NewSingleFlight()
someErr := errors.New("some error")
v, err := g.Do("key", func() (interface{}, error) {
v, err := g.Do("key", func() (any, error) {
return nil, someErr
})
if err != someErr {
@@ -40,7 +40,7 @@ func TestExclusiveCallDoDupSuppress(t *testing.T) {
g := NewSingleFlight()
c := make(chan string)
var calls int32
fn := func() (interface{}, error) {
fn := func() (any, error) {
atomic.AddInt32(&calls, 1)
return <-c, nil
}
@@ -79,7 +79,7 @@ func TestExclusiveCallDoDiffDupSuppress(t *testing.T) {
wg.Add(1)
go func(k string) {
<-broadcast // get all goroutines ready
_, err := g.Do(k, func() (interface{}, error) {
_, err := g.Do(k, func() (any, error) {
atomic.AddInt32(&calls, 1)
time.Sleep(10 * time.Millisecond)
return nil, nil
@@ -105,7 +105,7 @@ func TestExclusiveCallDoExDupSuppress(t *testing.T) {
g := NewSingleFlight()
c := make(chan string)
var calls int32
fn := func() (interface{}, error) {
fn := func() (any, error) {
atomic.AddInt32(&calls, 1)
return <-c, nil
}