Skip to content

Commit bf138a2

Browse files
committed
no connKeyType
1 parent 846cddb commit bf138a2

File tree

6 files changed

+21
-28
lines changed

6 files changed

+21
-28
lines changed

db/config.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ var (
1313
SerializedTransactionRetries = 10
1414
)
1515

16-
type (
17-
connKeyType struct{} // unique type for this package
18-
serializedTxKeyType struct{} // unique type for this package
19-
)
20-
2116
var (
22-
conn = sqldb.ConnectionWithError(context.Background(), errors.New("database connection not initialized"))
23-
connKey connKeyType
17+
conn = sqldb.ConnectionWithError(context.Background(), errors.New("database connection not initialized"))
18+
connCtxKey struct{}
2419

25-
serializedTxKey serializedTxKeyType
20+
serializedTransactionCtxKey struct{}
2621
)

db/conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func Conn(ctx context.Context) sqldb.Connection {
3030
// The returned connection will use the passed context.
3131
// See sqldb.Connection.WithContext
3232
func ConnDefault(ctx context.Context, defaultConn sqldb.Connection) sqldb.Connection {
33-
c, _ := ctx.Value(connKey).(sqldb.Connection)
33+
c, _ := ctx.Value(&connCtxKey).(sqldb.Connection)
3434
if c == nil {
3535
c = defaultConn
3636
}
@@ -45,7 +45,7 @@ func ConnDefault(ctx context.Context, defaultConn sqldb.Connection) sqldb.Connec
4545
// Passing a nil connection causes Conn(ctx)
4646
// to return the global connection set with SetConn.
4747
func ContextWithConn(ctx context.Context, conn sqldb.Connection) context.Context {
48-
return context.WithValue(ctx, connKey, conn)
48+
return context.WithValue(ctx, &connCtxKey, conn)
4949
}
5050

5151
// ContextWithoutCancel returns a new context that inherits

db/transaction.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func DebugNoTransaction(ctx context.Context, nonTxFunc func(context.Context) err
2525
// Recovered panics are re-paniced and rollback errors after a panic are logged with ErrLogger.
2626
func IsolatedTransaction(ctx context.Context, txFunc func(context.Context) error) error {
2727
return sqldb.IsolatedTransaction(Conn(ctx), nil, func(tx sqldb.Connection) error {
28-
return txFunc(context.WithValue(ctx, connKey, tx))
28+
return txFunc(context.WithValue(ctx, &connCtxKey, tx))
2929
})
3030
}
3131

@@ -38,7 +38,7 @@ func IsolatedTransaction(ctx context.Context, txFunc func(context.Context) error
3838
// Recovered panics are re-paniced and rollback errors after a panic are logged with sqldb.ErrLogger.
3939
func Transaction(ctx context.Context, txFunc func(context.Context) error) error {
4040
return sqldb.Transaction(Conn(ctx), nil, func(tx sqldb.Connection) error {
41-
return txFunc(context.WithValue(ctx, connKey, tx))
41+
return txFunc(context.WithValue(ctx, &connCtxKey, tx))
4242
})
4343
}
4444

@@ -80,14 +80,14 @@ func Transaction(ctx context.Context, txFunc func(context.Context) error) error
8080
func SerializedTransaction(ctx context.Context, txFunc func(context.Context) error) error {
8181
// Pass nested serialized transactions through
8282
if Conn(ctx).IsTransaction() {
83-
if ctx.Value(serializedTxKey) == nil {
83+
if ctx.Value(&serializedTransactionCtxKey) == nil {
8484
return errors.New("SerializedTransaction called from within a non-serialized transaction")
8585
}
8686
return txFunc(ctx)
8787
}
8888

8989
// Add value to context to check for nested serialized transactions
90-
ctx = context.WithValue(ctx, serializedTxKey, struct{}{})
90+
ctx = context.WithValue(ctx, &serializedTransactionCtxKey, struct{}{})
9191

9292
opts := sql.TxOptions{Isolation: sql.LevelSerializable}
9393
for i := 0; i < SerializedTransactionRetries; i++ {
@@ -109,7 +109,7 @@ func SerializedTransaction(ctx context.Context, txFunc func(context.Context) err
109109
// Recovered panics are re-paniced and rollback errors after a panic are logged with sqldb.ErrLogger.
110110
func TransactionOpts(ctx context.Context, opts *sql.TxOptions, txFunc func(context.Context) error) error {
111111
return sqldb.Transaction(Conn(ctx), opts, func(tx sqldb.Connection) error {
112-
return txFunc(context.WithValue(ctx, connKey, tx))
112+
return txFunc(context.WithValue(ctx, &connCtxKey, tx))
113113
})
114114
}
115115

@@ -123,7 +123,7 @@ func TransactionOpts(ctx context.Context, opts *sql.TxOptions, txFunc func(conte
123123
func TransactionReadOnly(ctx context.Context, txFunc func(context.Context) error) error {
124124
opts := sql.TxOptions{ReadOnly: true}
125125
return sqldb.Transaction(Conn(ctx), &opts, func(tx sqldb.Connection) error {
126-
return txFunc(context.WithValue(ctx, connKey, tx))
126+
return txFunc(context.WithValue(ctx, &connCtxKey, tx))
127127
})
128128
}
129129

db/transaction_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestSerializedTransaction(t *testing.T) {
1616
if !Conn(ctx).IsTransaction() {
1717
panic("not in transaction")
1818
}
19-
if ctx.Value(serializedTxKey) == nil {
19+
if ctx.Value(&serializedTransactionCtxKey) == nil {
2020
panic("no SerializedTransaction")
2121
}
2222
return nil
@@ -26,7 +26,7 @@ func TestSerializedTransaction(t *testing.T) {
2626
if !Conn(ctx).IsTransaction() {
2727
panic("not in transaction")
2828
}
29-
if ctx.Value(serializedTxKey) == nil {
29+
if ctx.Value(&serializedTransactionCtxKey) == nil {
3030
panic("no SerializedTransaction")
3131
}
3232
return errors.New("expected error")
@@ -70,7 +70,7 @@ func TestTransaction(t *testing.T) {
7070
if !Conn(ctx).IsTransaction() {
7171
panic("not in transaction")
7272
}
73-
if ctx.Value(serializedTxKey) != nil {
73+
if ctx.Value(&serializedTransactionCtxKey) != nil {
7474
panic("SerializedTransaction")
7575
}
7676
return nil
@@ -80,7 +80,7 @@ func TestTransaction(t *testing.T) {
8080
if !Conn(ctx).IsTransaction() {
8181
panic("not in transaction")
8282
}
83-
if ctx.Value(serializedTxKey) != nil {
83+
if ctx.Value(&serializedTransactionCtxKey) != nil {
8484
panic("SerializedTransaction")
8585
}
8686
return errors.New("expected error")

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/domonda/go-sqldb
33
go 1.18
44

55
require (
6-
github.com/domonda/go-errs v0.0.0-20220429185920-b85918b964f9
6+
github.com/domonda/go-errs v0.0.0-20220527085304-63cf6ad85d71
77
github.com/domonda/go-types v0.0.0-20220525115110-58b4488326ea
88
github.com/go-sql-driver/mysql v1.6.0
99
github.com/lib/pq v1.10.6
@@ -19,5 +19,5 @@ require (
1919
github.com/ungerik/go-reflection v0.0.0-20220113085621-6c5fc1f2694a // indirect
2020
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf // indirect
2121
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
22-
gopkg.in/yaml.v3 v3.0.0 // indirect
22+
gopkg.in/yaml.v3 v3.0.1 // indirect
2323
)

go.sum

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/domonda/go-errs v0.0.0-20220429185920-b85918b964f9 h1:2Me+4z758BNweppw7TjZG69BUBTsiPDgTrxgMH90TBE=
5-
github.com/domonda/go-errs v0.0.0-20220429185920-b85918b964f9/go.mod h1:Gd1uZZ8DXmCjmqwcCmxjThzCzVvCXJ3lWka2ND/kKss=
4+
github.com/domonda/go-errs v0.0.0-20220527085304-63cf6ad85d71 h1:WRag+fUJENLRM8N/wp6gf/0i1aEkLY9prNgoFQsWeso=
5+
github.com/domonda/go-errs v0.0.0-20220527085304-63cf6ad85d71/go.mod h1:suiFfPp8l6I+OOaKgPK/bfX7Ci9ZtFRgPh5VNE0HPao=
66
github.com/domonda/go-pretty v0.0.0-20220317123925-dd9e6bef129a h1:6/Is0KGl5Ot3E8ZLAgAFWYiSRdU+3t3jL38+5yIlCV4=
77
github.com/domonda/go-pretty v0.0.0-20220317123925-dd9e6bef129a/go.mod h1:3QkM8UJdyJMeKZiIo7hYzSkQBpRS3k0gOHw4ysyEIB4=
8-
github.com/domonda/go-types v0.0.0-20220513100758-7818d7fcb7bb h1:/+ltgjE+3/CrcxBxij/4sJgg9EXjGb733wqkTGifhDY=
9-
github.com/domonda/go-types v0.0.0-20220513100758-7818d7fcb7bb/go.mod h1:9+VXi/vJHmCp2Nl/hQB+UtJbPA8aUWAfAulGX86i1EA=
108
github.com/domonda/go-types v0.0.0-20220525115110-58b4488326ea h1:ISVN1HhZvofNGK+DrGr/OHN4Z+4vgbD5jZbbFBdibPQ=
119
github.com/domonda/go-types v0.0.0-20220525115110-58b4488326ea/go.mod h1:qZTRjdjIXo3g+8PUhfpkKbMPGsLVTuF3H7/AX5CzNeQ=
1210
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
@@ -33,5 +31,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
3331
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
3432
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3533
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36-
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
37-
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
35+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)