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

@@ -20,32 +20,32 @@ type (
// DelCtx deletes cached values with keys.
DelCtx(ctx context.Context, keys ...string) error
// Get gets the cache with key and fills into v.
Get(key string, val interface{}) error
Get(key string, val any) error
// GetCtx gets the cache with key and fills into v.
GetCtx(ctx context.Context, key string, val interface{}) error
GetCtx(ctx context.Context, key string, val any) error
// IsNotFound checks if the given error is the defined errNotFound.
IsNotFound(err error) bool
// Set sets the cache with key and v, using c.expiry.
Set(key string, val interface{}) error
Set(key string, val any) error
// SetCtx sets the cache with key and v, using c.expiry.
SetCtx(ctx context.Context, key string, val interface{}) error
SetCtx(ctx context.Context, key string, val any) error
// SetWithExpire sets the cache with key and v, using given expire.
SetWithExpire(key string, val interface{}, expire time.Duration) error
SetWithExpire(key string, val any, expire time.Duration) error
// SetWithExpireCtx sets the cache with key and v, using given expire.
SetWithExpireCtx(ctx context.Context, key string, val interface{}, expire time.Duration) error
SetWithExpireCtx(ctx context.Context, key string, val any, expire time.Duration) error
// Take takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
Take(val interface{}, key string, query func(val interface{}) error) error
Take(val any, key string, query func(val any) error) error
// TakeCtx takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
TakeCtx(ctx context.Context, val interface{}, key string, query func(val interface{}) error) error
TakeCtx(ctx context.Context, val any, key string, query func(val any) error) error
// TakeWithExpire takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
TakeWithExpire(val interface{}, key string, query func(val interface{}, expire time.Duration) error) error
TakeWithExpire(val any, key string, query func(val any, expire time.Duration) error) error
// TakeWithExpireCtx takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
TakeWithExpireCtx(ctx context.Context, val interface{}, key string,
query func(val interface{}, expire time.Duration) error) error
TakeWithExpireCtx(ctx context.Context, val any, key string,
query func(val any, expire time.Duration) error) error
}
cacheCluster struct {
@@ -97,7 +97,7 @@ func (cc cacheCluster) DelCtx(ctx context.Context, keys ...string) error {
return c.(Cache).DelCtx(ctx, key)
default:
var be errorx.BatchError
nodes := make(map[interface{}][]string)
nodes := make(map[any][]string)
for _, key := range keys {
c, ok := cc.dispatcher.Get(key)
if !ok {
@@ -118,12 +118,12 @@ func (cc cacheCluster) DelCtx(ctx context.Context, keys ...string) error {
}
// Get gets the cache with key and fills into v.
func (cc cacheCluster) Get(key string, val interface{}) error {
func (cc cacheCluster) Get(key string, val any) error {
return cc.GetCtx(context.Background(), key, val)
}
// GetCtx gets the cache with key and fills into v.
func (cc cacheCluster) GetCtx(ctx context.Context, key string, val interface{}) error {
func (cc cacheCluster) GetCtx(ctx context.Context, key string, val any) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
@@ -138,12 +138,12 @@ func (cc cacheCluster) IsNotFound(err error) bool {
}
// Set sets the cache with key and v, using c.expiry.
func (cc cacheCluster) Set(key string, val interface{}) error {
func (cc cacheCluster) Set(key string, val any) error {
return cc.SetCtx(context.Background(), key, val)
}
// SetCtx sets the cache with key and v, using c.expiry.
func (cc cacheCluster) SetCtx(ctx context.Context, key string, val interface{}) error {
func (cc cacheCluster) SetCtx(ctx context.Context, key string, val any) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
@@ -153,12 +153,12 @@ func (cc cacheCluster) SetCtx(ctx context.Context, key string, val interface{})
}
// SetWithExpire sets the cache with key and v, using given expire.
func (cc cacheCluster) SetWithExpire(key string, val interface{}, expire time.Duration) error {
func (cc cacheCluster) SetWithExpire(key string, val any, expire time.Duration) error {
return cc.SetWithExpireCtx(context.Background(), key, val, expire)
}
// SetWithExpireCtx sets the cache with key and v, using given expire.
func (cc cacheCluster) SetWithExpireCtx(ctx context.Context, key string, val interface{}, expire time.Duration) error {
func (cc cacheCluster) SetWithExpireCtx(ctx context.Context, key string, val any, expire time.Duration) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
@@ -169,13 +169,13 @@ func (cc cacheCluster) SetWithExpireCtx(ctx context.Context, key string, val int
// Take takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
func (cc cacheCluster) Take(val interface{}, key string, query func(val interface{}) error) error {
func (cc cacheCluster) Take(val any, key string, query func(val any) error) error {
return cc.TakeCtx(context.Background(), val, key, query)
}
// TakeCtx takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
func (cc cacheCluster) TakeCtx(ctx context.Context, val interface{}, key string, query func(val interface{}) error) error {
func (cc cacheCluster) TakeCtx(ctx context.Context, val any, key string, query func(val any) error) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
@@ -186,13 +186,13 @@ func (cc cacheCluster) TakeCtx(ctx context.Context, val interface{}, key string,
// TakeWithExpire takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
func (cc cacheCluster) TakeWithExpire(val interface{}, key string, query func(val interface{}, expire time.Duration) error) error {
func (cc cacheCluster) TakeWithExpire(val any, key string, query func(val any, expire time.Duration) error) error {
return cc.TakeWithExpireCtx(context.Background(), val, key, query)
}
// TakeWithExpireCtx takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
func (cc cacheCluster) TakeWithExpireCtx(ctx context.Context, val interface{}, key string, query func(val interface{}, expire time.Duration) error) error {
func (cc cacheCluster) TakeWithExpireCtx(ctx context.Context, val any, key string, query func(val any, expire time.Duration) error) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound

View File

@@ -44,11 +44,11 @@ func (mc *mockedNode) DelCtx(_ context.Context, keys ...string) error {
return be.Err()
}
func (mc *mockedNode) Get(key string, val interface{}) error {
func (mc *mockedNode) Get(key string, val any) error {
return mc.GetCtx(context.Background(), key, val)
}
func (mc *mockedNode) GetCtx(ctx context.Context, key string, val interface{}) error {
func (mc *mockedNode) GetCtx(ctx context.Context, key string, val any) error {
bs, ok := mc.vals[key]
if ok {
return json.Unmarshal(bs, val)
@@ -61,11 +61,11 @@ func (mc *mockedNode) IsNotFound(err error) bool {
return errors.Is(err, mc.errNotFound)
}
func (mc *mockedNode) Set(key string, val interface{}) error {
func (mc *mockedNode) Set(key string, val any) error {
return mc.SetCtx(context.Background(), key, val)
}
func (mc *mockedNode) SetCtx(ctx context.Context, key string, val interface{}) error {
func (mc *mockedNode) SetCtx(ctx context.Context, key string, val any) error {
data, err := json.Marshal(val)
if err != nil {
return err
@@ -75,19 +75,19 @@ func (mc *mockedNode) SetCtx(ctx context.Context, key string, val interface{}) e
return nil
}
func (mc *mockedNode) SetWithExpire(key string, val interface{}, expire time.Duration) error {
func (mc *mockedNode) SetWithExpire(key string, val any, expire time.Duration) error {
return mc.SetWithExpireCtx(context.Background(), key, val, expire)
}
func (mc *mockedNode) SetWithExpireCtx(ctx context.Context, key string, val interface{}, expire time.Duration) error {
func (mc *mockedNode) SetWithExpireCtx(ctx context.Context, key string, val any, expire time.Duration) error {
return mc.Set(key, val)
}
func (mc *mockedNode) Take(val interface{}, key string, query func(val interface{}) error) error {
func (mc *mockedNode) Take(val any, key string, query func(val any) error) error {
return mc.TakeCtx(context.Background(), val, key, query)
}
func (mc *mockedNode) TakeCtx(ctx context.Context, val interface{}, key string, query func(val interface{}) error) error {
func (mc *mockedNode) TakeCtx(ctx context.Context, val any, key string, query func(val any) error) error {
if _, ok := mc.vals[key]; ok {
return mc.GetCtx(ctx, key, val)
}
@@ -99,12 +99,12 @@ func (mc *mockedNode) TakeCtx(ctx context.Context, val interface{}, key string,
return mc.SetCtx(ctx, key, val)
}
func (mc *mockedNode) TakeWithExpire(val interface{}, key string, query func(val interface{}, expire time.Duration) error) error {
func (mc *mockedNode) TakeWithExpire(val any, key string, query func(val any, expire time.Duration) error) error {
return mc.TakeWithExpireCtx(context.Background(), val, key, query)
}
func (mc *mockedNode) TakeWithExpireCtx(ctx context.Context, val interface{}, key string, query func(val interface{}, expire time.Duration) error) error {
return mc.Take(val, key, func(val interface{}) error {
func (mc *mockedNode) TakeWithExpireCtx(ctx context.Context, val any, key string, query func(val any, expire time.Duration) error) error {
return mc.Take(val, key, func(val any) error {
return query(val, 0)
})
}
@@ -279,13 +279,13 @@ func TestCache_Balance(t *testing.T) {
for i := 0; i < total/10; i++ {
var val int
if i%2 == 0 {
assert.Nil(t, c.Take(&val, strconv.Itoa(i*10), func(val interface{}) error {
assert.Nil(t, c.Take(&val, strconv.Itoa(i*10), func(val any) error {
*val.(*int) = i
count++
return nil
}))
} else {
assert.Nil(t, c.TakeWithExpire(&val, strconv.Itoa(i*10), func(val interface{}, expire time.Duration) error {
assert.Nil(t, c.TakeWithExpire(&val, strconv.Itoa(i*10), func(val any, expire time.Duration) error {
*val.(*int) = i
count++
return nil
@@ -307,10 +307,10 @@ func TestCacheNoNode(t *testing.T) {
assert.NotNil(t, c.Get("foo", nil))
assert.NotNil(t, c.Set("foo", nil))
assert.NotNil(t, c.SetWithExpire("foo", nil, time.Second))
assert.NotNil(t, c.Take(nil, "foo", func(val interface{}) error {
assert.NotNil(t, c.Take(nil, "foo", func(val any) error {
return nil
}))
assert.NotNil(t, c.TakeWithExpire(nil, "foo", func(val interface{}, duration time.Duration) error {
assert.NotNil(t, c.TakeWithExpire(nil, "foo", func(val any, duration time.Duration) error {
return nil
}))
}

View File

@@ -89,12 +89,12 @@ func (c cacheNode) DelCtx(ctx context.Context, keys ...string) error {
}
// Get gets the cache with key and fills into v.
func (c cacheNode) Get(key string, val interface{}) error {
func (c cacheNode) Get(key string, val any) error {
return c.GetCtx(context.Background(), key, val)
}
// GetCtx gets the cache with key and fills into v.
func (c cacheNode) GetCtx(ctx context.Context, key string, val interface{}) error {
func (c cacheNode) GetCtx(ctx context.Context, key string, val any) error {
err := c.doGetCache(ctx, key, val)
if err == errPlaceholder {
return c.errNotFound
@@ -109,22 +109,22 @@ func (c cacheNode) IsNotFound(err error) bool {
}
// Set sets the cache with key and v, using c.expiry.
func (c cacheNode) Set(key string, val interface{}) error {
func (c cacheNode) Set(key string, val any) error {
return c.SetCtx(context.Background(), key, val)
}
// SetCtx sets the cache with key and v, using c.expiry.
func (c cacheNode) SetCtx(ctx context.Context, key string, val interface{}) error {
func (c cacheNode) SetCtx(ctx context.Context, key string, val any) error {
return c.SetWithExpireCtx(ctx, key, val, c.aroundDuration(c.expiry))
}
// SetWithExpire sets the cache with key and v, using given expire.
func (c cacheNode) SetWithExpire(key string, val interface{}, expire time.Duration) error {
func (c cacheNode) SetWithExpire(key string, val any, expire time.Duration) error {
return c.SetWithExpireCtx(context.Background(), key, val, expire)
}
// SetWithExpireCtx sets the cache with key and v, using given expire.
func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val interface{},
func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val any,
expire time.Duration) error {
data, err := jsonx.Marshal(val)
if err != nil {
@@ -141,34 +141,34 @@ func (c cacheNode) String() string {
// Take takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
func (c cacheNode) Take(val interface{}, key string, query func(val interface{}) error) error {
func (c cacheNode) Take(val any, key string, query func(val any) error) error {
return c.TakeCtx(context.Background(), val, key, query)
}
// TakeCtx takes the result from cache first, if not found,
// query from DB and set cache using c.expiry, then return the result.
func (c cacheNode) TakeCtx(ctx context.Context, val interface{}, key string,
query func(val interface{}) error) error {
return c.doTake(ctx, val, key, query, func(v interface{}) error {
func (c cacheNode) TakeCtx(ctx context.Context, val any, key string,
query func(val any) error) error {
return c.doTake(ctx, val, key, query, func(v any) error {
return c.SetCtx(ctx, key, v)
})
}
// TakeWithExpire takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
func (c cacheNode) TakeWithExpire(val interface{}, key string, query func(val interface{},
func (c cacheNode) TakeWithExpire(val any, key string, query func(val any,
expire time.Duration) error) error {
return c.TakeWithExpireCtx(context.Background(), val, key, query)
}
// TakeWithExpireCtx takes the result from cache first, if not found,
// query from DB and set cache using given expire, then return the result.
func (c cacheNode) TakeWithExpireCtx(ctx context.Context, val interface{}, key string,
query func(val interface{}, expire time.Duration) error) error {
func (c cacheNode) TakeWithExpireCtx(ctx context.Context, val any, key string,
query func(val any, expire time.Duration) error) error {
expire := c.aroundDuration(c.expiry)
return c.doTake(ctx, val, key, func(v interface{}) error {
return c.doTake(ctx, val, key, func(v any) error {
return query(v, expire)
}, func(v interface{}) error {
}, func(v any) error {
return c.SetWithExpireCtx(ctx, key, v, expire)
})
}
@@ -184,7 +184,7 @@ func (c cacheNode) asyncRetryDelCache(keys ...string) {
}, keys...)
}
func (c cacheNode) doGetCache(ctx context.Context, key string, v interface{}) error {
func (c cacheNode) doGetCache(ctx context.Context, key string, v any) error {
c.stat.IncrementTotal()
data, err := c.rds.GetCtx(ctx, key)
if err != nil {
@@ -205,10 +205,10 @@ func (c cacheNode) doGetCache(ctx context.Context, key string, v interface{}) er
return c.processCache(ctx, key, data, v)
}
func (c cacheNode) doTake(ctx context.Context, v interface{}, key string,
query func(v interface{}) error, cacheVal func(v interface{}) error) error {
func (c cacheNode) doTake(ctx context.Context, v any, key string,
query func(v any) error, cacheVal func(v any) error) error {
logger := logx.WithContext(ctx)
val, fresh, err := c.barrier.DoEx(key, func() (interface{}, error) {
val, fresh, err := c.barrier.DoEx(key, func() (any, error) {
if err := c.doGetCache(ctx, key, v); err != nil {
if err == errPlaceholder {
return nil, c.errNotFound
@@ -255,7 +255,7 @@ func (c cacheNode) doTake(ctx context.Context, v interface{}, key string,
return jsonx.Unmarshal(val.([]byte), v)
}
func (c cacheNode) processCache(ctx context.Context, key, data string, v interface{}) error {
func (c cacheNode) processCache(ctx context.Context, key, data string, v any) error {
err := jsonx.Unmarshal([]byte(data), v)
if err == nil {
return nil

View File

@@ -62,7 +62,7 @@ func TestCacheNode_DelCache(t *testing.T) {
ticker := timex.NewFakeTicker()
var err error
timingWheel, err = collection.NewTimingWheelWithTicker(
time.Millisecond, timingWheelSlots, func(key, value interface{}) {
time.Millisecond, timingWheelSlots, func(key, value any) {
clean(key, value)
}, ticker)
assert.NoError(t, err)
@@ -146,7 +146,7 @@ func TestCacheNode_Take(t *testing.T) {
cn := NewNode(store, syncx.NewSingleFlight(), NewStat("any"), errTestNotFound,
WithExpiry(time.Second), WithNotFoundExpiry(time.Second))
var str string
err = cn.Take(&str, "any", func(v interface{}) error {
err = cn.Take(&str, "any", func(v any) error {
*v.(*string) = "value"
return nil
})
@@ -167,7 +167,7 @@ func TestCacheNode_TakeBadRedis(t *testing.T) {
cn := NewNode(redis.New(r.Addr()), syncx.NewSingleFlight(), NewStat("any"),
errTestNotFound, WithExpiry(time.Second), WithNotFoundExpiry(time.Second))
var str string
assert.Error(t, cn.Take(&str, "any", func(v interface{}) error {
assert.Error(t, cn.Take(&str, "any", func(v any) error {
*v.(*string) = "value"
return nil
}))
@@ -188,7 +188,7 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
errNotFound: errTestNotFound,
}
var str string
err = cn.Take(&str, "any", func(v interface{}) error {
err = cn.Take(&str, "any", func(v any) error {
return errTestNotFound
})
assert.True(t, cn.IsNotFound(err))
@@ -198,7 +198,7 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
assert.Equal(t, `*`, val)
store.Set("any", "*")
err = cn.Take(&str, "any", func(v interface{}) error {
err = cn.Take(&str, "any", func(v any) error {
return nil
})
assert.True(t, cn.IsNotFound(err))
@@ -206,7 +206,7 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
store.Del("any")
errDummy := errors.New("dummy")
err = cn.Take(&str, "any", func(v interface{}) error {
err = cn.Take(&str, "any", func(v any) error {
return errDummy
})
assert.Equal(t, errDummy, err)
@@ -227,7 +227,7 @@ func TestCacheNode_TakeWithExpire(t *testing.T) {
errNotFound: errors.New("any"),
}
var str string
err = cn.TakeWithExpire(&str, "any", func(v interface{}, expire time.Duration) error {
err = cn.TakeWithExpire(&str, "any", func(v any, expire time.Duration) error {
*v.(*string) = "value"
return nil
})
@@ -277,7 +277,7 @@ func TestCacheValueWithBigInt(t *testing.T) {
)
assert.Nil(t, cn.Set(key, value))
var val interface{}
var val any
assert.Nil(t, cn.Get(key, &val))
assert.Equal(t, strconv.FormatInt(value, 10), fmt.Sprintf("%v", val))
}

View File

@@ -48,7 +48,7 @@ func AddCleanTask(task func() error, keys ...string) {
}, time.Second)
}
func clean(key, value interface{}) {
func clean(key, value any) {
taskRunner.Schedule(func() {
dt := value.(delayTask)
err := dt.task()