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

@@ -29,17 +29,17 @@ type (
// ExecCtxFn defines the sql exec method.
ExecCtxFn func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error)
// IndexQueryFn defines the query method that based on unique indexes.
IndexQueryFn func(conn sqlx.SqlConn, v interface{}) (interface{}, error)
IndexQueryFn func(conn sqlx.SqlConn, v any) (any, error)
// IndexQueryCtxFn defines the query method that based on unique indexes.
IndexQueryCtxFn func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (interface{}, error)
IndexQueryCtxFn func(ctx context.Context, conn sqlx.SqlConn, v any) (any, error)
// PrimaryQueryFn defines the query method that based on primary keys.
PrimaryQueryFn func(conn sqlx.SqlConn, v, primary interface{}) error
PrimaryQueryFn func(conn sqlx.SqlConn, v, primary any) error
// PrimaryQueryCtxFn defines the query method that based on primary keys.
PrimaryQueryCtxFn func(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error
PrimaryQueryCtxFn func(ctx context.Context, conn sqlx.SqlConn, v, primary any) error
// QueryFn defines the query method.
QueryFn func(conn sqlx.SqlConn, v interface{}) error
QueryFn func(conn sqlx.SqlConn, v any) error
// QueryCtxFn defines the query method.
QueryCtxFn func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error
QueryCtxFn func(ctx context.Context, conn sqlx.SqlConn, v any) error
// A CachedConn is a DB connection with cache capability.
CachedConn struct {
@@ -79,12 +79,12 @@ func (cc CachedConn) DelCacheCtx(ctx context.Context, keys ...string) error {
}
// GetCache unmarshals cache with given key into v.
func (cc CachedConn) GetCache(key string, v interface{}) error {
func (cc CachedConn) GetCache(key string, v any) error {
return cc.GetCacheCtx(context.Background(), key, v)
}
// GetCacheCtx unmarshals cache with given key into v.
func (cc CachedConn) GetCacheCtx(ctx context.Context, key string, v interface{}) error {
func (cc CachedConn) GetCacheCtx(ctx context.Context, key string, v any) error {
return cc.cache.GetCtx(ctx, key, v)
}
@@ -112,38 +112,38 @@ func (cc CachedConn) ExecCtx(ctx context.Context, exec ExecCtxFn, keys ...string
}
// ExecNoCache runs exec with given sql statement, without affecting cache.
func (cc CachedConn) ExecNoCache(q string, args ...interface{}) (sql.Result, error) {
func (cc CachedConn) ExecNoCache(q string, args ...any) (sql.Result, error) {
return cc.ExecNoCacheCtx(context.Background(), q, args...)
}
// ExecNoCacheCtx runs exec with given sql statement, without affecting cache.
func (cc CachedConn) ExecNoCacheCtx(ctx context.Context, q string, args ...interface{}) (
func (cc CachedConn) ExecNoCacheCtx(ctx context.Context, q string, args ...any) (
sql.Result, error) {
return cc.db.ExecCtx(ctx, q, args...)
}
// QueryRow unmarshals into v with given key and query func.
func (cc CachedConn) QueryRow(v interface{}, key string, query QueryFn) error {
queryCtx := func(_ context.Context, conn sqlx.SqlConn, v interface{}) error {
func (cc CachedConn) QueryRow(v any, key string, query QueryFn) error {
queryCtx := func(_ context.Context, conn sqlx.SqlConn, v any) error {
return query(conn, v)
}
return cc.QueryRowCtx(context.Background(), v, key, queryCtx)
}
// QueryRowCtx unmarshals into v with given key and query func.
func (cc CachedConn) QueryRowCtx(ctx context.Context, v interface{}, key string, query QueryCtxFn) error {
return cc.cache.TakeCtx(ctx, v, key, func(v interface{}) error {
func (cc CachedConn) QueryRowCtx(ctx context.Context, v any, key string, query QueryCtxFn) error {
return cc.cache.TakeCtx(ctx, v, key, func(v any) error {
return query(ctx, cc.db, v)
})
}
// QueryRowIndex unmarshals into v with given key.
func (cc CachedConn) QueryRowIndex(v interface{}, key string, keyer func(primary interface{}) string,
func (cc CachedConn) QueryRowIndex(v any, key string, keyer func(primary any) string,
indexQuery IndexQueryFn, primaryQuery PrimaryQueryFn) error {
indexQueryCtx := func(_ context.Context, conn sqlx.SqlConn, v interface{}) (interface{}, error) {
indexQueryCtx := func(_ context.Context, conn sqlx.SqlConn, v any) (any, error) {
return indexQuery(conn, v)
}
primaryQueryCtx := func(_ context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
primaryQueryCtx := func(_ context.Context, conn sqlx.SqlConn, v, primary any) error {
return primaryQuery(conn, v, primary)
}
@@ -151,14 +151,14 @@ func (cc CachedConn) QueryRowIndex(v interface{}, key string, keyer func(primary
}
// QueryRowIndexCtx unmarshals into v with given key.
func (cc CachedConn) QueryRowIndexCtx(ctx context.Context, v interface{}, key string,
keyer func(primary interface{}) string, indexQuery IndexQueryCtxFn,
func (cc CachedConn) QueryRowIndexCtx(ctx context.Context, v any, key string,
keyer func(primary any) string, indexQuery IndexQueryCtxFn,
primaryQuery PrimaryQueryCtxFn) error {
var primaryKey interface{}
var primaryKey any
var found bool
if err := cc.cache.TakeWithExpireCtx(ctx, &primaryKey, key,
func(val interface{}, expire time.Duration) (err error) {
func(val any, expire time.Duration) (err error) {
primaryKey, err = indexQuery(ctx, cc.db, v)
if err != nil {
return
@@ -175,42 +175,42 @@ func (cc CachedConn) QueryRowIndexCtx(ctx context.Context, v interface{}, key st
return nil
}
return cc.cache.TakeCtx(ctx, v, keyer(primaryKey), func(v interface{}) error {
return cc.cache.TakeCtx(ctx, v, keyer(primaryKey), func(v any) error {
return primaryQuery(ctx, cc.db, v, primaryKey)
})
}
// QueryRowNoCache unmarshals into v with given statement.
func (cc CachedConn) QueryRowNoCache(v interface{}, q string, args ...interface{}) error {
func (cc CachedConn) QueryRowNoCache(v any, q string, args ...any) error {
return cc.QueryRowNoCacheCtx(context.Background(), v, q, args...)
}
// QueryRowNoCacheCtx unmarshals into v with given statement.
func (cc CachedConn) QueryRowNoCacheCtx(ctx context.Context, v interface{}, q string,
args ...interface{}) error {
func (cc CachedConn) QueryRowNoCacheCtx(ctx context.Context, v any, q string,
args ...any) error {
return cc.db.QueryRowCtx(ctx, v, q, args...)
}
// QueryRowsNoCache unmarshals into v with given statement.
// It doesn't use cache, because it might cause consistency problem.
func (cc CachedConn) QueryRowsNoCache(v interface{}, q string, args ...interface{}) error {
func (cc CachedConn) QueryRowsNoCache(v any, q string, args ...any) error {
return cc.QueryRowsNoCacheCtx(context.Background(), v, q, args...)
}
// QueryRowsNoCacheCtx unmarshals into v with given statement.
// It doesn't use cache, because it might cause consistency problem.
func (cc CachedConn) QueryRowsNoCacheCtx(ctx context.Context, v interface{}, q string,
args ...interface{}) error {
func (cc CachedConn) QueryRowsNoCacheCtx(ctx context.Context, v any, q string,
args ...any) error {
return cc.db.QueryRowsCtx(ctx, v, q, args...)
}
// SetCache sets v into cache with given key.
func (cc CachedConn) SetCache(key string, val interface{}) error {
func (cc CachedConn) SetCache(key string, val any) error {
return cc.SetCacheCtx(context.Background(), key, val)
}
// SetCacheCtx sets v into cache with given key.
func (cc CachedConn) SetCacheCtx(ctx context.Context, key string, val interface{}) error {
func (cc CachedConn) SetCacheCtx(ctx context.Context, key string, val any) error {
return cc.cache.SetCtx(ctx, key, val)
}

View File

@@ -57,7 +57,7 @@ func TestStat(t *testing.T) {
for i := 0; i < 10; i++ {
var str string
err = c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v interface{}) error {
err = c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v any) error {
*v.(*string) = "zero"
return nil
})
@@ -87,24 +87,24 @@ func TestCachedConn_QueryRowIndex_NoCache(t *testing.T) {
}, cache.WithExpiry(time.Second*10))
var str string
err = c.QueryRowIndex(&str, "index", func(s interface{}) string {
err = c.QueryRowIndex(&str, "index", func(s any) string {
return fmt.Sprintf("%s/1234", s)
}, func(conn sqlx.SqlConn, v interface{}) (interface{}, error) {
}, func(conn sqlx.SqlConn, v any) (any, error) {
*v.(*string) = "zero"
return "primary", errors.New("foo")
}, func(conn sqlx.SqlConn, v, pri interface{}) error {
}, func(conn sqlx.SqlConn, v, pri any) error {
assert.Equal(t, "primary", pri)
*v.(*string) = "xin"
return nil
})
assert.NotNil(t, err)
err = c.QueryRowIndex(&str, "index", func(s interface{}) string {
err = c.QueryRowIndex(&str, "index", func(s any) string {
return fmt.Sprintf("%s/1234", s)
}, func(conn sqlx.SqlConn, v interface{}) (interface{}, error) {
}, func(conn sqlx.SqlConn, v any) (any, error) {
*v.(*string) = "zero"
return "primary", nil
}, func(conn sqlx.SqlConn, v, pri interface{}) error {
}, func(conn sqlx.SqlConn, v, pri any) error {
assert.Equal(t, "primary", pri)
*v.(*string) = "xin"
return nil
@@ -130,12 +130,12 @@ func TestCachedConn_QueryRowIndex_HasCache(t *testing.T) {
var str string
r.Set("index", `"primary"`)
err = c.QueryRowIndex(&str, "index", func(s interface{}) string {
err = c.QueryRowIndex(&str, "index", func(s any) string {
return fmt.Sprintf("%s/1234", s)
}, func(conn sqlx.SqlConn, v interface{}) (interface{}, error) {
}, func(conn sqlx.SqlConn, v any) (any, error) {
assert.Fail(t, "should not go here")
return "primary", nil
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
}, func(conn sqlx.SqlConn, v, primary any) error {
*v.(*string) = "xin"
assert.Equal(t, "primary", primary)
return nil
@@ -163,7 +163,7 @@ func TestCachedConn_QueryRowIndex_HasCache_IntPrimary(t *testing.T) {
)
tests := []struct {
name string
primary interface{}
primary any
primaryCache string
}{
{
@@ -220,12 +220,12 @@ func TestCachedConn_QueryRowIndex_HasCache_IntPrimary(t *testing.T) {
var str string
r.Set("index", test.primaryCache)
err = c.QueryRowIndex(&str, "index", func(s interface{}) string {
err = c.QueryRowIndex(&str, "index", func(s any) string {
return fmt.Sprintf("%v/1234", s)
}, func(conn sqlx.SqlConn, v interface{}) (interface{}, error) {
}, func(conn sqlx.SqlConn, v any) (any, error) {
assert.Fail(t, "should not go here")
return test.primary, nil
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
}, func(conn sqlx.SqlConn, v, primary any) error {
*v.(*string) = "xin"
assert.Equal(t, primary, primary)
return nil
@@ -260,12 +260,12 @@ func TestCachedConn_QueryRowIndex_HasWrongCache(t *testing.T) {
var str string
r.Set(k, v)
err = c.QueryRowIndex(&str, "index", func(s interface{}) string {
err = c.QueryRowIndex(&str, "index", func(s any) string {
return fmt.Sprintf("%s/1234", s)
}, func(conn sqlx.SqlConn, v interface{}) (interface{}, error) {
}, func(conn sqlx.SqlConn, v any) (any, error) {
*v.(*string) = "xin"
return "primary", nil
}, func(conn sqlx.SqlConn, v, primary interface{}) error {
}, func(conn sqlx.SqlConn, v, primary any) error {
*v.(*string) = "xin"
assert.Equal(t, "primary", primary)
return nil
@@ -292,7 +292,7 @@ func TestStatCacheFails(t *testing.T) {
for i := 0; i < 20; i++ {
var str string
err := c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v interface{}) error {
err := c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v any) error {
return errors.New("db failed")
})
assert.NotNil(t, err)
@@ -314,7 +314,7 @@ func TestStatDbFails(t *testing.T) {
for i := 0; i < 20; i++ {
var str string
err = c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v interface{}) error {
err = c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v any) error {
return errors.New("db failed")
})
assert.NotNil(t, err)
@@ -339,7 +339,7 @@ func TestStatFromMemory(t *testing.T) {
wait.Add(4)
go func() {
var str string
err := c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v interface{}) error {
err := c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v any) error {
*v.(*string) = "zero"
return nil
})
@@ -355,7 +355,7 @@ func TestStatFromMemory(t *testing.T) {
go func() {
var str string
wait.Done()
err := c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v interface{}) error {
err := c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v any) error {
*v.(*string) = "zero"
return nil
})
@@ -368,7 +368,7 @@ func TestStatFromMemory(t *testing.T) {
for i := 0; i < 5; i++ {
go func() {
var str string
err := c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v interface{}) error {
err := c.QueryRow(&str, "name", func(conn sqlx.SqlConn, v any) error {
*v.(*string) = "zero"
return nil
})
@@ -397,7 +397,7 @@ func TestCachedConnQueryRow(t *testing.T) {
var user string
var ran bool
c := NewNodeConn(&conn, r, cache.WithExpiry(time.Second*30))
err = c.QueryRow(&user, key, func(conn sqlx.SqlConn, v interface{}) error {
err = c.QueryRow(&user, key, func(conn sqlx.SqlConn, v any) error {
ran = true
user = value
return nil
@@ -426,7 +426,7 @@ func TestCachedConnQueryRowFromCache(t *testing.T) {
var ran bool
c := NewNodeConn(&conn, r, cache.WithExpiry(time.Second*30))
assert.Nil(t, c.SetCache(key, value))
err = c.QueryRow(&user, key, func(conn sqlx.SqlConn, v interface{}) error {
err = c.QueryRow(&user, key, func(conn sqlx.SqlConn, v any) error {
ran = true
user = value
return nil
@@ -452,7 +452,7 @@ func TestQueryRowNotFound(t *testing.T) {
var ran int
c := NewNodeConn(&conn, r, cache.WithExpiry(time.Second*30))
for i := 0; i < 20; i++ {
err = c.QueryRow(&user, key, func(conn sqlx.SqlConn, v interface{}) error {
err = c.QueryRow(&user, key, func(conn sqlx.SqlConn, v any) error {
ran++
return sql.ErrNoRows
})
@@ -551,7 +551,7 @@ func TestQueryRowNoCache(t *testing.T) {
)
var user string
var ran bool
conn := dummySqlConn{queryRow: func(v interface{}, q string, args ...interface{}) error {
conn := dummySqlConn{queryRow: func(v any, q string, args ...any) error {
user = value
ran = true
return nil
@@ -583,10 +583,10 @@ func resetStats() {
}
type dummySqlConn struct {
queryRow func(interface{}, string, ...interface{}) error
queryRow func(any, string, ...any) error
}
func (d dummySqlConn) ExecCtx(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
func (d dummySqlConn) ExecCtx(ctx context.Context, query string, args ...any) (sql.Result, error) {
return nil, nil
}
@@ -594,15 +594,15 @@ func (d dummySqlConn) PrepareCtx(ctx context.Context, query string) (sqlx.StmtSe
return nil, nil
}
func (d dummySqlConn) QueryRowPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (d dummySqlConn) QueryRowPartialCtx(ctx context.Context, v any, query string, args ...any) error {
return nil
}
func (d dummySqlConn) QueryRowsCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (d dummySqlConn) QueryRowsCtx(ctx context.Context, v any, query string, args ...any) error {
return nil
}
func (d dummySqlConn) QueryRowsPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (d dummySqlConn) QueryRowsPartialCtx(ctx context.Context, v any, query string, args ...any) error {
return nil
}
@@ -610,7 +610,7 @@ func (d dummySqlConn) TransactCtx(ctx context.Context, fn func(context.Context,
return nil
}
func (d dummySqlConn) Exec(query string, args ...interface{}) (sql.Result, error) {
func (d dummySqlConn) Exec(query string, args ...any) (sql.Result, error) {
return nil, nil
}
@@ -618,26 +618,26 @@ func (d dummySqlConn) Prepare(query string) (sqlx.StmtSession, error) {
return nil, nil
}
func (d dummySqlConn) QueryRow(v interface{}, query string, args ...interface{}) error {
func (d dummySqlConn) QueryRow(v any, query string, args ...any) error {
return d.QueryRowCtx(context.Background(), v, query, args...)
}
func (d dummySqlConn) QueryRowCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
func (d dummySqlConn) QueryRowCtx(_ context.Context, v any, query string, args ...any) error {
if d.queryRow != nil {
return d.queryRow(v, query, args...)
}
return nil
}
func (d dummySqlConn) QueryRowPartial(v interface{}, query string, args ...interface{}) error {
func (d dummySqlConn) QueryRowPartial(v any, query string, args ...any) error {
return nil
}
func (d dummySqlConn) QueryRows(v interface{}, query string, args ...interface{}) error {
func (d dummySqlConn) QueryRows(v any, query string, args ...any) error {
return nil
}
func (d dummySqlConn) QueryRowsPartial(v interface{}, query string, args ...interface{}) error {
func (d dummySqlConn) QueryRowsPartial(v any, query string, args ...any) error {
return nil
}
@@ -656,20 +656,20 @@ type trackedConn struct {
transactValue bool
}
func (c *trackedConn) Exec(query string, args ...interface{}) (sql.Result, error) {
func (c *trackedConn) Exec(query string, args ...any) (sql.Result, error) {
return c.ExecCtx(context.Background(), query, args...)
}
func (c *trackedConn) ExecCtx(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
func (c *trackedConn) ExecCtx(ctx context.Context, query string, args ...any) (sql.Result, error) {
c.execValue = true
return c.dummySqlConn.ExecCtx(ctx, query, args...)
}
func (c *trackedConn) QueryRows(v interface{}, query string, args ...interface{}) error {
func (c *trackedConn) QueryRows(v any, query string, args ...any) error {
return c.QueryRowsCtx(context.Background(), v, query, args...)
}
func (c *trackedConn) QueryRowsCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (c *trackedConn) QueryRowsCtx(ctx context.Context, v any, query string, args ...any) error {
c.queryRowsValue = true
return c.dummySqlConn.QueryRowsCtx(ctx, v, query, args...)
}