diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 262421d..3b3b635 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -139,5 +139,3 @@ jobs: uses: golangci/golangci-lint-action@v3 with: args: --timeout=3m -v - # TODO remove, temporary to restore - skip-cache: true diff --git a/.golangci.yml b/.golangci.yml index 7b29c07..177a71f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -95,6 +95,20 @@ issues: linters: - staticcheck text: "SA1029: should not use built-in type string as key for value; define your own type to avoid collisions" + - path: .+/context\.go + linters: + - ireturn + - path: .+/transaction\.go + linters: + - ireturn + source: \) Begin\(ctx + - path: .+/settings\.go + linters: + - ireturn + source: "(?:EnrichBy|SetPropagation|SetCtxKey|CtxKey|SetCancelable|SetTimeout)" + - linters: + - gochecknoglobals + source: "DefaultCtxGetter" - &internal_text path: ^internal text: "var-naming: don't use an underscore in package name" diff --git a/gorm/context.go b/gorm/context.go index 2f916b6..6bcf5b8 100644 --- a/gorm/context.go +++ b/gorm/context.go @@ -1,8 +1,6 @@ //go:build go1.16 // +build go1.16 -//nolint:ireturn,nolintlint // return Tr for external usage. -//revive:disable:package-comments package gorm import ( @@ -15,8 +13,6 @@ import ( ) // DefaultCtxGetter is the CtxGetter with settings.DefaultCtxKey. -// -//nolint:gochecknoglobals var DefaultCtxGetter = NewCtxGetter(trmcontext.DefaultManager) // CtxGetter gets Tr from trm.СtxManager by casting trm.Transaction to *gorm.DB. @@ -24,11 +20,12 @@ type CtxGetter struct { ctxManager trm.СtxManager } -//revive:disable:exported +// NewCtxGetter returns *CtxGetter to get *gorm.DB from context.Context. func NewCtxGetter(c trm.СtxManager) *CtxGetter { return &CtxGetter{ctxManager: c} } +// DefaultTrOrDB returns Tr(*gorm.DB) from context.Context or DB(*gorm.DB) otherwise. func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db *gorm.DB) *gorm.DB { if tr := c.ctxManager.Default(ctx); tr != nil { return c.convert(tr) @@ -37,6 +34,7 @@ func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db *gorm.DB) *gorm.DB { return db } +// TrOrDB returns Tr(*gorm.DB) from context.Context by trm.CtxKey or DB(*gorm.DB) otherwise. func (c *CtxGetter) TrOrDB(ctx context.Context, key trm.CtxKey, db *gorm.DB) *gorm.DB { if tr := c.ctxManager.ByKey(ctx, key); tr != nil { return c.convert(tr) diff --git a/gorm/settings.go b/gorm/settings.go index 1e55721..a649978 100644 --- a/gorm/settings.go +++ b/gorm/settings.go @@ -50,8 +50,8 @@ func MustSettings(trms trm.Settings, oo ...Opt) Settings { return s } -//revive:disable:exported -func (s Settings) EnrichBy(in trm.Settings) (res trm.Settings) { //nolint:ireturn,nolintlint +// EnrichBy fills nil properties from external Settings. +func (s Settings) EnrichBy(in trm.Settings) (res trm.Settings) { external, ok := in.(Settings) if ok { if s.TxOpts() == nil { diff --git a/gorm/transaction.go b/gorm/transaction.go index e5b488d..18693e8 100644 --- a/gorm/transaction.go +++ b/gorm/transaction.go @@ -83,7 +83,7 @@ func (t *Transaction) Transaction() interface{} { } // Begin nested transaction by save point. -func (t *Transaction) Begin(ctx context.Context, s trm.Settings) (context.Context, trm.Transaction, error) { //nolint:ireturn,nolintlint +func (t *Transaction) Begin(ctx context.Context, s trm.Settings) (context.Context, trm.Transaction, error) { return NewDefaultFactory(t.tx)(ctx, s) } diff --git a/mongo/settings.go b/mongo/settings.go index 61b7555..c22ef62 100644 --- a/mongo/settings.go +++ b/mongo/settings.go @@ -61,8 +61,8 @@ func MustSettings(trms trm.Settings, oo ...Opt) Settings { return s } -//revive:disable:exported -func (s Settings) EnrichBy(in trm.Settings) trm.Settings { //nolint:ireturn,nolintlint +// EnrichBy fills nil properties from external Settings. +func (s Settings) EnrichBy(in trm.Settings) trm.Settings { external, ok := in.(Settings) if ok { if s.SessionOpts() == nil { diff --git a/pgxv4/context.go b/pgxv4/context.go index c2cd888..bcb4f0b 100644 --- a/pgxv4/context.go +++ b/pgxv4/context.go @@ -1,8 +1,6 @@ //go:build go1.16 // +build go1.16 -//nolint:ireturn,nolintlint // return Tr for external usage. -//revive:disable:package-comments package pgxv4 import ( @@ -13,8 +11,6 @@ import ( ) // DefaultCtxGetter is the CtxGetter with settings.DefaultCtxKey. -// -//nolint:gochecknoglobals var DefaultCtxGetter = NewCtxGetter(trmcontext.DefaultManager) // CtxGetter gets Tr from trm.СtxManager by casting trm.Transaction to Tr. @@ -22,11 +18,12 @@ type CtxGetter struct { ctxManager trm.СtxManager } -//revive:disable:exported +// NewCtxGetter returns *CtxGetter to get Tr from context.Context. func NewCtxGetter(c trm.СtxManager) *CtxGetter { return &CtxGetter{ctxManager: c} } +// DefaultTrOrDB returns Tr from context.Context or DB(Tr) otherwise. func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db Tr) Tr { if tr := c.ctxManager.Default(ctx); tr != nil { return c.convert(tr) @@ -35,6 +32,7 @@ func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db Tr) Tr { return db } +// TrOrDB returns Tr from context.Context by trm.CtxKey or DB(Tr) otherwise. func (c *CtxGetter) TrOrDB(ctx context.Context, key trm.CtxKey, db Tr) Tr { if tr := c.ctxManager.ByKey(ctx, key); tr != nil { return c.convert(tr) diff --git a/pgxv4/settings.go b/pgxv4/settings.go index c178397..f749198 100644 --- a/pgxv4/settings.go +++ b/pgxv4/settings.go @@ -54,8 +54,8 @@ func MustSettings(trms trm.Settings, oo ...Opt) Settings { return s } -//revive:disable:exported -func (s Settings) EnrichBy(in trm.Settings) trm.Settings { //nolint:ireturn,nolintlint +// EnrichBy fills nil properties from external Settings. +func (s Settings) EnrichBy(in trm.Settings) trm.Settings { external, ok := in.(Settings) if ok { var emptyTrOpts pgx.TxOptions diff --git a/pgxv4/transaction.go b/pgxv4/transaction.go index 9961124..55673ce 100644 --- a/pgxv4/transaction.go +++ b/pgxv4/transaction.go @@ -55,7 +55,7 @@ func (t *Transaction) Transaction() interface{} { } // Begin nested transaction by save point. -func (t *Transaction) Begin(ctx context.Context, _ trm.Settings) (context.Context, trm.Transaction, error) { //nolint:ireturn,nolintlint +func (t *Transaction) Begin(ctx context.Context, _ trm.Settings) (context.Context, trm.Transaction, error) { tx, err := t.tx.Begin(ctx) if err != nil { return ctx, nil, err diff --git a/pgxv5/context.go b/pgxv5/context.go index 6659d0d..988b243 100644 --- a/pgxv5/context.go +++ b/pgxv5/context.go @@ -1,8 +1,6 @@ //go:build go1.19 // +build go1.19 -//nolint:ireturn,nolintlint // return Tr for external usage. -//revive:disable:package-comments package pgxv5 import ( @@ -13,8 +11,6 @@ import ( ) // DefaultCtxGetter is the CtxGetter with settings.DefaultCtxKey. -// -//nolint:gochecknoglobals var DefaultCtxGetter = NewCtxGetter(trmcontext.DefaultManager) // CtxGetter gets Tr from trm.СtxManager by casting trm.Transaction to Tr. @@ -22,11 +18,12 @@ type CtxGetter struct { ctxManager trm.СtxManager } -//revive:disable:exported +// NewCtxGetter returns *CtxGetter to get Tr from context.Context. func NewCtxGetter(c trm.СtxManager) *CtxGetter { return &CtxGetter{ctxManager: c} } +// DefaultTrOrDB returns Tr from context.Context or DB(Tr) otherwise. func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db Tr) Tr { if tr := c.ctxManager.Default(ctx); tr != nil { return c.convert(tr) @@ -35,6 +32,7 @@ func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db Tr) Tr { return db } +// TrOrDB returns Tr from context.Context by trm.CtxKey or DB(Tr) otherwise. func (c *CtxGetter) TrOrDB(ctx context.Context, key trm.CtxKey, db Tr) Tr { if tr := c.ctxManager.ByKey(ctx, key); tr != nil { return c.convert(tr) diff --git a/pgxv5/settings.go b/pgxv5/settings.go index 0a21c41..82ed2cb 100644 --- a/pgxv5/settings.go +++ b/pgxv5/settings.go @@ -55,8 +55,8 @@ func MustSettings(trms trm.Settings, oo ...Opt) Settings { return s } -//revive:disable:exported -func (s Settings) EnrichBy(in trm.Settings) trm.Settings { //nolint:ireturn,nolintlint +// EnrichBy fills nil properties from external Settings. +func (s Settings) EnrichBy(in trm.Settings) trm.Settings { external, ok := in.(Settings) if ok { var emptyTrOpts pgx.TxOptions diff --git a/pgxv5/transaction.go b/pgxv5/transaction.go index 27a550d..7e713f0 100644 --- a/pgxv5/transaction.go +++ b/pgxv5/transaction.go @@ -55,7 +55,7 @@ func (t *Transaction) Transaction() interface{} { } // Begin nested transaction by save point. -func (t *Transaction) Begin(ctx context.Context, _ trm.Settings) (context.Context, trm.Transaction, error) { //nolint:ireturn,nolintlint +func (t *Transaction) Begin(ctx context.Context, _ trm.Settings) (context.Context, trm.Transaction, error) { tx, err := t.tx.Begin(ctx) if err != nil { return ctx, nil, err diff --git a/redis/context.go b/redis/context.go index cc63923..3084007 100644 --- a/redis/context.go +++ b/redis/context.go @@ -1,5 +1,3 @@ -//nolint:ireturn,nolintlint // return Tr for external usage. -//revive:disable:package-comments package redis import ( @@ -12,8 +10,6 @@ import ( ) // DefaultCtxGetter is the CtxGetter with settings.DefaultCtxKey. -// -//nolint:gochecknoglobals var DefaultCtxGetter = NewCtxGetter(trmcontext.DefaultManager) // CtxGetter gets redis.Pipeliner from trm.СtxManager by casting trm.Transaction to redis.UniversalClient. @@ -21,11 +17,12 @@ type CtxGetter struct { ctxManager trm.СtxManager } -//revive:disable:exported +// NewCtxGetter returns *CtxGetter to get Cmdable from context.Context. func NewCtxGetter(c trm.СtxManager) *CtxGetter { return &CtxGetter{ctxManager: c} } +// DefaultTrOrDB returns Cmdable from context.Context or DB(redis.Cmdable) otherwise. func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db redis.Cmdable) redis.Cmdable { if tr := c.ctxManager.Default(ctx); tr != nil { return c.convert(tr) @@ -34,6 +31,7 @@ func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db redis.Cmdable) redis.C return db } +// TrOrDB returns Cmdable from context.Context by trm.CtxKey or DB(redis.Cmdable) otherwise. func (c *CtxGetter) TrOrDB(ctx context.Context, key trm.CtxKey, db redis.Cmdable) redis.Cmdable { if tr := c.ctxManager.ByKey(ctx, key); tr != nil { return c.convert(tr) diff --git a/redis/settings.go b/redis/settings.go index f90e610..f834c60 100644 --- a/redis/settings.go +++ b/redis/settings.go @@ -52,8 +52,8 @@ func MustSettings(trms trm.Settings, oo ...Opt) Settings { return s } -//revive:disable:exported -func (s Settings) EnrichBy(in trm.Settings) trm.Settings { //nolint:ireturn,nolintlint +// EnrichBy fills nil properties from external Settings. +func (s Settings) EnrichBy(in trm.Settings) trm.Settings { external, ok := in.(Settings) if ok { if s.IsMultiOrNil() == nil { @@ -78,6 +78,7 @@ func (s Settings) EnrichBy(in trm.Settings) trm.Settings { //nolint:ireturn,noli return s } +// IsMulti - true uses redis MULTI cmd. func (s Settings) IsMulti() bool { if s.isMulti == nil { return DefaultMulti @@ -86,6 +87,7 @@ func (s Settings) IsMulti() bool { return *s.isMulti } +// IsMultiOrNil returns IsMulti or nil. func (s Settings) IsMultiOrNil() *bool { return s.isMulti } diff --git a/sql/context.go b/sql/context.go index 1a929df..dac65a4 100644 --- a/sql/context.go +++ b/sql/context.go @@ -1,5 +1,3 @@ -//nolint:ireturn,nolintlint // return Tr for external usage. -//revive:disable:package-comments package sql import ( @@ -11,8 +9,6 @@ import ( ) // DefaultCtxGetter is the CtxGetter with settings.DefaultCtxKey. -// -//nolint:gochecknoglobals var DefaultCtxGetter = NewCtxGetter(trmcontext.DefaultManager) // CtxGetter gets Tr from trm.СtxManager by casting trm.Transaction to Tr. @@ -20,11 +16,12 @@ type CtxGetter struct { ctxManager trm.СtxManager } -//revive:disable:exported +// NewCtxGetter returns *CtxGetter to get Tr from context.Context. func NewCtxGetter(c trm.СtxManager) *CtxGetter { return &CtxGetter{ctxManager: c} } +// DefaultTrOrDB returns Tr from context.Context or DB(Tr) otherwise. func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db Tr) Tr { if tr := c.ctxManager.Default(ctx); tr != nil { return c.convert(tr) @@ -33,6 +30,7 @@ func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db Tr) Tr { return db } +// TrOrDB returns Tr from context.Context by trm.CtxKey or DB(Tr) otherwise. func (c *CtxGetter) TrOrDB(ctx context.Context, key trm.CtxKey, db Tr) Tr { if tr := c.ctxManager.ByKey(ctx, key); tr != nil { return c.convert(tr) diff --git a/sql/settings.go b/sql/settings.go index fb5a709..8295a82 100644 --- a/sql/settings.go +++ b/sql/settings.go @@ -47,8 +47,8 @@ func MustSettings(trms trm.Settings, oo ...Opt) Settings { return s } -//revive:disable:exported -func (s Settings) EnrichBy(in trm.Settings) trm.Settings { //nolint:ireturn,nolintlint +// EnrichBy fills nil properties from external Settings. +func (s Settings) EnrichBy(in trm.Settings) trm.Settings { external, ok := in.(Settings) if ok { if s.TxOpts() == nil { diff --git a/sql/transaction.go b/sql/transaction.go index 5367b57..e8e6d1a 100644 --- a/sql/transaction.go +++ b/sql/transaction.go @@ -55,7 +55,7 @@ func (t *Transaction) Transaction() interface{} { } // Begin nested transaction by save point. -func (t *Transaction) Begin(ctx context.Context, _ trm.Settings) (context.Context, trm.Transaction, error) { //nolint:ireturn,nolintlint +func (t *Transaction) Begin(ctx context.Context, _ trm.Settings) (context.Context, trm.Transaction, error) { _, err := t.tx.ExecContext(ctx, t.savePoint.Create(t.incrementID())) if err != nil { return ctx, nil, err diff --git a/sqlx/context.go b/sqlx/context.go index 689c80b..59c6399 100644 --- a/sqlx/context.go +++ b/sqlx/context.go @@ -1,5 +1,3 @@ -//nolint:ireturn,nolintlint // return Tr for external usage. -//revive:disable:package-comments package sqlx import ( @@ -12,8 +10,6 @@ import ( ) // DefaultCtxGetter is the CtxGetter with settings.DefaultCtxKey. -// -//nolint:gochecknoglobals var DefaultCtxGetter = NewCtxGetter(trmcontext.DefaultManager) // CtxGetter gets Tr from trm.СtxManager by casting trm.Transaction to Tr. @@ -21,11 +17,12 @@ type CtxGetter struct { ctxManager trm.СtxManager } -//revive:disable:exported +// NewCtxGetter returns *CtxGetter to get Tr from context.Context. func NewCtxGetter(c trm.СtxManager) *CtxGetter { return &CtxGetter{ctxManager: c} } +// DefaultTrOrDB returns Tr from context.Context or DB(Tr) otherwise. func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db Tr) Tr { if tr := c.ctxManager.Default(ctx); tr != nil { return c.convert(tr) @@ -34,6 +31,7 @@ func (c *CtxGetter) DefaultTrOrDB(ctx context.Context, db Tr) Tr { return db } +// TrOrDB returns Tr from context.Context by trm.CtxKey or DB(Tr) otherwise. func (c *CtxGetter) TrOrDB(ctx context.Context, key trm.CtxKey, db Tr) Tr { if tr := c.ctxManager.ByKey(ctx, key); tr != nil { return c.convert(tr) diff --git a/sqlx/transaction.go b/sqlx/transaction.go index da76bbf..c8873b2 100644 --- a/sqlx/transaction.go +++ b/sqlx/transaction.go @@ -57,7 +57,7 @@ func (t *Transaction) Transaction() interface{} { } // Begin nested transaction by save point. -func (t *Transaction) Begin(ctx context.Context, _ trm.Settings) (context.Context, trm.Transaction, error) { //nolint:ireturn,nolintlint +func (t *Transaction) Begin(ctx context.Context, _ trm.Settings) (context.Context, trm.Transaction, error) { _, err := t.tx.ExecContext(ctx, t.savePoint.Create(t.incrementID())) if err != nil { return ctx, nil, multierr.Combine(trm.ErrNestedBegin, err) diff --git a/trm/context/context.go b/trm/context/context.go index f82fd21..5fb4377 100644 --- a/trm/context/context.go +++ b/trm/context/context.go @@ -1,6 +1,4 @@ // Package context implement a setter and getter to put and get trm.Transaction from context.Context. -// -//nolint:ireturn,nolintlint package context import ( @@ -23,15 +21,17 @@ func New(ctxKey trm.CtxKey) *Manager { return &Manager{ctxKey: ctxKey} } -//revive:disable:exported +// Default returns trm.Transaction from context.Context by default key. func (c *Manager) Default(ctx context.Context) trm.Transaction { return c.ByKey(ctx, c.ctxKey) } +// SetDefault puts trm.Transaction in context.Context by default key. func (c *Manager) SetDefault(ctx context.Context, t trm.Transaction) context.Context { return c.SetByKey(ctx, c.ctxKey, t) } +// ByKey returns trm.Transaction from context.Context by key(trm.CtxKey). func (c *Manager) ByKey(ctx context.Context, key trm.CtxKey) trm.Transaction { if tr, ok := ctx.Value(key).(trm.Transaction); ok { return tr @@ -40,6 +40,7 @@ func (c *Manager) ByKey(ctx context.Context, key trm.CtxKey) trm.Transaction { return nil } +// SetByKey puts trm.Transaction in context.Context by key(trm.CtxKey). func (c *Manager) SetByKey(ctx context.Context, key trm.CtxKey, t trm.Transaction) context.Context { return context.WithValue(ctx, key, t) } diff --git a/trm/settings/settings.go b/trm/settings/settings.go index dbc583a..284ae78 100644 --- a/trm/settings/settings.go +++ b/trm/settings/settings.go @@ -56,8 +56,8 @@ func Must(oo ...Opt) Settings { return s } -//revive:disable:exported -func (s Settings) EnrichBy(external trm.Settings) (res trm.Settings) { //nolint:ireturn,nolintlint +// EnrichBy fills nil properties from external Settings. +func (s Settings) EnrichBy(external trm.Settings) (res trm.Settings) { res = s if s.CtxKeyOrNil() == nil { @@ -79,8 +79,8 @@ func (s Settings) EnrichBy(external trm.Settings) (res trm.Settings) { //nolint: return res } -// CtxKey returns trm.CtxKey for the trm.Transaction. -func (s Settings) CtxKey() trm.CtxKey { //nolint:ireturn,nolintlint +// CtxKey returns key(trm.CtxKey) to get trm.Transaction from context.Context. +func (s Settings) CtxKey() trm.CtxKey { if s.ctxKey == nil { return DefaultCtxKey } @@ -88,11 +88,13 @@ func (s Settings) CtxKey() trm.CtxKey { //nolint:ireturn,nolintlint return *s.ctxKey } +// CtxKeyOrNil returns key(*trm.CtxKey) or nil. func (s Settings) CtxKeyOrNil() *trm.CtxKey { return s.ctxKey } -func (s Settings) SetCtxKey(key *trm.CtxKey) trm.Settings { //nolint:ireturn,nolintlint +// SetCtxKey sets key(*trm.CtxKey) to store trm.Transaction in context.Context. +func (s Settings) SetCtxKey(key *trm.CtxKey) trm.Settings { return s.setCtxKey(key) } @@ -111,11 +113,13 @@ func (s Settings) Propagation() trm.Propagation { return *s.propagation } +// PropagationOrNil returns trm.Propagation or nil. func (s Settings) PropagationOrNil() *trm.Propagation { return s.propagation } -func (s Settings) SetPropagation(p *trm.Propagation) trm.Settings { //nolint:ireturn,nolintlint +// SetPropagation sets *trm.Propagation. +func (s Settings) SetPropagation(p *trm.Propagation) trm.Settings { return s.setPropagation(p) } @@ -134,11 +138,13 @@ func (s Settings) Cancelable() bool { return *s.isCancelable } +// CancelableOrNil returns Cancelable or nil. func (s Settings) CancelableOrNil() *bool { return s.isCancelable } -func (s Settings) SetCancelable(t *bool) trm.Settings { //nolint:ireturn,nolintlint +// SetCancelable sets Cancelable(*bool). +func (s Settings) SetCancelable(t *bool) trm.Settings { return s.setCancelable(t) } @@ -153,7 +159,8 @@ func (s Settings) TimeoutOrNil() *time.Duration { return s.timeout } -func (s Settings) SetTimeout(t *time.Duration) trm.Settings { //nolint:ireturn,nolintlint +// SetTimeout sets *time.Duration for trm.Transaction. +func (s Settings) SetTimeout(t *time.Duration) trm.Settings { return s.setTimeout(t) }