From 8ccd4ec81f7f40d55486bcc51917360f093eeda3 Mon Sep 17 00:00:00 2001 From: Md Mushfiqur Rahim <20mahin2020@gmail.com> Date: Mon, 15 Jun 2026 07:30:31 +0600 Subject: [PATCH] refactor: replace interface{} with any (Go 1.18+) --- crdb/common.go | 2 +- crdb/crdbgorm/gorm.go | 2 +- crdb/crdbgorm/gorm_test.go | 6 +++--- crdb/crdbpgx/pgx.go | 2 +- crdb/crdbpgx/pgx_test.go | 6 +++--- crdb/crdbpgxv5/pgx.go | 2 +- crdb/crdbpgxv5/pgx_test.go | 6 +++--- crdb/crdbsqlx/sqlx.go | 2 +- crdb/crdbsqlx/sqlx_test.go | 6 +++--- crdb/testing_util.go | 10 +++++----- crdb/tx.go | 8 ++++---- crdb/tx_test.go | 8 ++++---- 12 files changed, 30 insertions(+), 30 deletions(-) diff --git a/crdb/common.go b/crdb/common.go index 29063ce..d273593 100644 --- a/crdb/common.go +++ b/crdb/common.go @@ -22,7 +22,7 @@ import ( // Tx abstracts the operations needed by ExecuteInTx so that different // frameworks (e.g. go's sql package, pgx, gorm) can be used with ExecuteInTx. type Tx interface { - Exec(context.Context, string, ...interface{}) error + Exec(context.Context, string, ...any) error Commit(context.Context) error Rollback(context.Context) error } diff --git a/crdb/crdbgorm/gorm.go b/crdb/crdbgorm/gorm.go index ec6417f..3aff73f 100644 --- a/crdb/crdbgorm/gorm.go +++ b/crdb/crdbgorm/gorm.go @@ -45,7 +45,7 @@ type gormTxAdapter struct { var _ crdb.Tx = gormTxAdapter{} // Exec is part of the crdb.Tx interface. -func (tx gormTxAdapter) Exec(_ context.Context, q string, args ...interface{}) error { +func (tx gormTxAdapter) Exec(_ context.Context, q string, args ...any) error { return tx.db.Exec(q, args...).Error } diff --git a/crdb/crdbgorm/gorm_test.go b/crdb/crdbgorm/gorm_test.go index d916488..5c1c63a 100644 --- a/crdb/crdbgorm/gorm_test.go +++ b/crdb/crdbgorm/gorm_test.go @@ -98,14 +98,14 @@ func (t gormWriteSkewTest) Init(context.Context) error { } // ExecuteTx is part of the crdb.WriteSkewTest interface. -func (t gormWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx interface{}) error) error { +func (t gormWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx any) error) error { return ExecuteTx(ctx, t.db, nil /* opts */, func(tx *gorm.DB) error { return fn(tx) }) } // GetBalances is part of the crdb.WriteSkewTest interface. -func (t gormWriteSkewTest) GetBalances(_ context.Context, txi interface{}) (int, int, error) { +func (t gormWriteSkewTest) GetBalances(_ context.Context, txi any) (int, int, error) { tx := txi.(*gorm.DB) var accounts []Account @@ -118,7 +118,7 @@ func (t gormWriteSkewTest) GetBalances(_ context.Context, txi interface{}) (int, // UpdateBalance is part of the crdb.WriteSkewInterface. func (t gormWriteSkewTest) UpdateBalance( - _ context.Context, txi interface{}, accountID, delta int, + _ context.Context, txi any, accountID, delta int, ) error { tx := txi.(*gorm.DB) var acc Account diff --git a/crdb/crdbpgx/pgx.go b/crdb/crdbpgx/pgx.go index caa83e7..923de0c 100644 --- a/crdb/crdbpgx/pgx.go +++ b/crdb/crdbpgx/pgx.go @@ -59,7 +59,7 @@ func (tx pgxTxAdapter) Rollback(ctx context.Context) error { } // Exec is part of the crdb.Tx interface. -func (tx pgxTxAdapter) Exec(ctx context.Context, q string, args ...interface{}) error { +func (tx pgxTxAdapter) Exec(ctx context.Context, q string, args ...any) error { _, err := tx.tx.Exec(ctx, q, args...) return err } diff --git a/crdb/crdbpgx/pgx_test.go b/crdb/crdbpgx/pgx_test.go index 2ec88d1..5b95901 100644 --- a/crdb/crdbpgx/pgx_test.go +++ b/crdb/crdbpgx/pgx_test.go @@ -61,14 +61,14 @@ INSERT INTO d.t (acct, balance) VALUES (1, 100), (2, 100); var _ crdb.WriteSkewTest = pgxWriteSkewTest{} // ExecuteTx is part of the crdb.WriteSkewTest interface. -func (t pgxWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx interface{}) error) error { +func (t pgxWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx any) error) error { return ExecuteTx(ctx, t.pool, pgx.TxOptions{}, func(tx pgx.Tx) error { return fn(tx) }) } // GetBalances is part of the crdb.WriteSkewTest interface. -func (t pgxWriteSkewTest) GetBalances(ctx context.Context, txi interface{}) (int, int, error) { +func (t pgxWriteSkewTest) GetBalances(ctx context.Context, txi any) (int, int, error) { tx := txi.(pgx.Tx) var rows pgx.Rows rows, err := tx.Query(ctx, `SELECT balance FROM d.t WHERE acct IN (1, 2);`) @@ -92,7 +92,7 @@ func (t pgxWriteSkewTest) GetBalances(ctx context.Context, txi interface{}) (int // UpdateBalance is part of the crdb.WriteSkewInterface. func (t pgxWriteSkewTest) UpdateBalance( - ctx context.Context, txi interface{}, acct, delta int, + ctx context.Context, txi any, acct, delta int, ) error { tx := txi.(pgx.Tx) _, err := tx.Exec(ctx, `UPDATE d.t SET balance=balance+$1 WHERE acct=$2;`, delta, acct) diff --git a/crdb/crdbpgxv5/pgx.go b/crdb/crdbpgxv5/pgx.go index 76235d0..5e6f73d 100644 --- a/crdb/crdbpgxv5/pgx.go +++ b/crdb/crdbpgxv5/pgx.go @@ -59,7 +59,7 @@ func (tx pgxTxAdapter) Rollback(ctx context.Context) error { } // Exec is part of the crdb.Tx interface. -func (tx pgxTxAdapter) Exec(ctx context.Context, q string, args ...interface{}) error { +func (tx pgxTxAdapter) Exec(ctx context.Context, q string, args ...any) error { _, err := tx.tx.Exec(ctx, q, args...) return err } diff --git a/crdb/crdbpgxv5/pgx_test.go b/crdb/crdbpgxv5/pgx_test.go index 120d26f..fb02e3f 100644 --- a/crdb/crdbpgxv5/pgx_test.go +++ b/crdb/crdbpgxv5/pgx_test.go @@ -61,14 +61,14 @@ INSERT INTO d.t (acct, balance) VALUES (1, 100), (2, 100); var _ crdb.WriteSkewTest = pgxWriteSkewTest{} // ExecuteTx is part of the crdb.WriteSkewTest interface. -func (t pgxWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx interface{}) error) error { +func (t pgxWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx any) error) error { return ExecuteTx(ctx, t.pool, pgx.TxOptions{}, func(tx pgx.Tx) error { return fn(tx) }) } // GetBalances is part of the crdb.WriteSkewTest interface. -func (t pgxWriteSkewTest) GetBalances(ctx context.Context, txi interface{}) (int, int, error) { +func (t pgxWriteSkewTest) GetBalances(ctx context.Context, txi any) (int, int, error) { tx := txi.(pgx.Tx) var rows pgx.Rows rows, err := tx.Query(ctx, `SELECT balance FROM d.t WHERE acct IN (1, 2);`) @@ -92,7 +92,7 @@ func (t pgxWriteSkewTest) GetBalances(ctx context.Context, txi interface{}) (int // UpdateBalance is part of the crdb.WriteSkewInterface. func (t pgxWriteSkewTest) UpdateBalance( - ctx context.Context, txi interface{}, acct, delta int, + ctx context.Context, txi any, acct, delta int, ) error { tx := txi.(pgx.Tx) _, err := tx.Exec(ctx, `UPDATE d.t SET balance=balance+$1 WHERE acct=$2;`, delta, acct) diff --git a/crdb/crdbsqlx/sqlx.go b/crdb/crdbsqlx/sqlx.go index 4d5665b..897262c 100644 --- a/crdb/crdbsqlx/sqlx.go +++ b/crdb/crdbsqlx/sqlx.go @@ -45,7 +45,7 @@ type sqlxTxAdapter struct { var _ crdb.Tx = sqlxTxAdapter{} -func (s sqlxTxAdapter) Exec(ctx context.Context, query string, args ...interface{}) error { +func (s sqlxTxAdapter) Exec(ctx context.Context, query string, args ...any) error { _, err := s.Tx.ExecContext(ctx, query, args...) return err } diff --git a/crdb/crdbsqlx/sqlx_test.go b/crdb/crdbsqlx/sqlx_test.go index 3aa0f88..97553bd 100644 --- a/crdb/crdbsqlx/sqlx_test.go +++ b/crdb/crdbsqlx/sqlx_test.go @@ -64,14 +64,14 @@ INSERT INTO d.t (acct, balance) VALUES (1, 100), (2, 100); var _ crdb.WriteSkewTest = sqlxConnSkewTest{} // ExecuteTx is part of the crdb.WriteSkewTest interface. -func (t sqlxConnSkewTest) ExecuteTx(ctx context.Context, fn func(tx interface{}) error) error { +func (t sqlxConnSkewTest) ExecuteTx(ctx context.Context, fn func(tx any) error) error { return ExecuteTx(ctx, t.db, nil /* txOptions */, func(tx *sqlx.Tx) error { return fn(tx) }) } // GetBalances is part of the crdb.WriteSkewTest interface. -func (t sqlxConnSkewTest) GetBalances(ctx context.Context, txi interface{}) (int, int, error) { +func (t sqlxConnSkewTest) GetBalances(ctx context.Context, txi any) (int, int, error) { tx := txi.(*sqlx.Tx) rows, err := tx.QueryContext(ctx, `SELECT balance FROM d.t WHERE acct IN (1, 2);`) if err != nil { @@ -94,7 +94,7 @@ func (t sqlxConnSkewTest) GetBalances(ctx context.Context, txi interface{}) (int // UpdateBalance is part of the crdb.WriteSkewInterface. func (t sqlxConnSkewTest) UpdateBalance( - ctx context.Context, txi interface{}, acct, delta int, + ctx context.Context, txi any, acct, delta int, ) error { tx := txi.(*sqlx.Tx) _, err := tx.ExecContext(ctx, `UPDATE d.t SET balance=balance+$1 WHERE acct=$2;`, delta, acct) diff --git a/crdb/testing_util.go b/crdb/testing_util.go index 47a1ea4..05814b8 100644 --- a/crdb/testing_util.go +++ b/crdb/testing_util.go @@ -25,9 +25,9 @@ import ( // to be written once and run for any framework supported by this library. type WriteSkewTest interface { Init(context.Context) error - ExecuteTx(ctx context.Context, fn func(tx interface{}) error) error - GetBalances(ctx context.Context, tx interface{}) (bal1, bal2 int, err error) - UpdateBalance(ctx context.Context, tx interface{}, acct, delta int) error + ExecuteTx(ctx context.Context, fn func(tx any) error) error + GetBalances(ctx context.Context, tx any) (bal1, bal2 int, err error) + UpdateBalance(ctx context.Context, tx any, acct, delta int) error } // ExecuteTxGenericTest represents the structure of a test for the ExecuteTx @@ -47,7 +47,7 @@ func ExecuteTxGenericTest(ctx context.Context, framework WriteSkewTest) error { errCh := make(chan error, 1) go func() { *iter = 0 - errCh <- framework.ExecuteTx(ctx, func(tx interface{}) (retErr error) { + errCh <- framework.ExecuteTx(ctx, func(tx any) (retErr error) { defer func() { if retErr == nil { return @@ -103,7 +103,7 @@ func ExecuteTxGenericTest(ctx context.Context, framework WriteSkewTest) error { } var bal1, bal2 int - err := framework.ExecuteTx(ctx, func(txi interface{}) error { + err := framework.ExecuteTx(ctx, func(txi any) error { var err error bal1, bal2, err = framework.GetBalances(ctx, txi) return err diff --git a/crdb/tx.go b/crdb/tx.go index 308f75e..a71898c 100644 --- a/crdb/tx.go +++ b/crdb/tx.go @@ -103,7 +103,7 @@ func Execute(fn func() error) (err error) { // ExecuteCtxFunc represents a function that takes a context and variadic // arguments and returns an error. It's used with ExecuteCtx to enable retryable // operations with configurable parameters. -type ExecuteCtxFunc func(context.Context, ...interface{}) error +type ExecuteCtxFunc func(context.Context, ...any) error // ExecuteCtx runs fn and retries it as needed, respecting a retry policy // obtained from the context. It is used to add configurable retry handling to @@ -134,7 +134,7 @@ type ExecuteCtxFunc func(context.Context, ...interface{}) error // `Cause() error` (github.com/pkg/errors) or `Unwrap() error` (Go 1.13+). // For example: // -// crdb.ExecuteCtx(ctx, func(ctx context.Context, args ...interface{}) error { +// crdb.ExecuteCtx(ctx, func(ctx context.Context, args ...any) error { // id := args[0].(int) // rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE id = $1", id) // if err != nil { @@ -144,7 +144,7 @@ type ExecuteCtxFunc func(context.Context, ...interface{}) error // // ... // return nil // }, userID) -func ExecuteCtx(ctx context.Context, fn ExecuteCtxFunc, args ...interface{}) (err error) { +func ExecuteCtx(ctx context.Context, fn ExecuteCtxFunc, args ...any) (err error) { // establish the retry policy retryPolicy := getRetryPolicy(ctx) // set up the retry policy state @@ -312,7 +312,7 @@ type stdlibTxnAdapter struct { var _ Tx = stdlibTxnAdapter{} // Exec is part of the tx interface. -func (tx stdlibTxnAdapter) Exec(ctx context.Context, q string, args ...interface{}) error { +func (tx stdlibTxnAdapter) Exec(ctx context.Context, q string, args ...any) error { _, err := tx.tx.ExecContext(ctx, q, args...) return err } diff --git a/crdb/tx_test.go b/crdb/tx_test.go index 40cbbc5..b972226 100644 --- a/crdb/tx_test.go +++ b/crdb/tx_test.go @@ -53,7 +53,7 @@ func TestExecuteCtx(t *testing.T) { {"no args", 1, 3, false, nil}, } - fn := func(ctx context.Context, args ...interface{}) error { + fn := func(ctx context.Context, args ...any) error { if len(args) == 0 { _, err := db.ExecContext(ctx, `INSERT INTO test_retry VALUES (3)`) return err @@ -179,13 +179,13 @@ INSERT INTO d.t (acct, balance) VALUES (1, 100), (2, 100); return err } -func (t stdlibWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx interface{}) error) error { +func (t stdlibWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx any) error) error { return ExecuteTx(ctx, t.db, nil /* opts */, func(tx *sql.Tx) error { return fn(tx) }) } -func (t stdlibWriteSkewTest) GetBalances(ctx context.Context, txi interface{}) (int, int, error) { +func (t stdlibWriteSkewTest) GetBalances(ctx context.Context, txi any) (int, int, error) { tx := txi.(*sql.Tx) var rows *sql.Rows rows, err := tx.QueryContext(ctx, `SELECT balance FROM d.t WHERE acct IN (1, 2);`) @@ -208,7 +208,7 @@ func (t stdlibWriteSkewTest) GetBalances(ctx context.Context, txi interface{}) ( } func (t stdlibWriteSkewTest) UpdateBalance( - ctx context.Context, txi interface{}, acct, delta int, + ctx context.Context, txi any, acct, delta int, ) error { tx := txi.(*sql.Tx) _, err := tx.ExecContext(ctx, `UPDATE d.t SET balance=balance+$1 WHERE acct=$2;`, delta, acct)