From d93a1d7aae30df066d5250269c0ec92f7e95ded3 Mon Sep 17 00:00:00 2001 From: kozmod Date: Sat, 9 Sep 2023 13:12:07 +0300 Subject: [PATCH] [feature] fix `operator` and `transactor` functions description; rename `CtxKeyType` -> `CtxKey` --- operator.go | 17 +++++++++-------- stdlib/transactor.go | 2 +- transactor.go | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/operator.go b/operator.go index 33edc32..809fa3c 100644 --- a/operator.go +++ b/operator.go @@ -5,34 +5,35 @@ import ( "fmt" ) -type CtxKeyType string +// CtxKey is key type for default ContextOperator. +type CtxKey string -// ContextOperator inject and extract TxCommitter rom context.Context. +// ContextOperator inject and extract TxCommitter from context.Context. type ContextOperator[B any, C TxCommitter] struct { beginner *B } -// NewContextOperator creates new ContextOperator. +// NewContextOperator returns new ContextOperator. func NewContextOperator[B any, C TxCommitter](b *B) *ContextOperator[B, C] { return &ContextOperator[B, C]{ beginner: b, } } -// Inject injects TxCommitter. +// Inject returns new context.Context contains TxCommitter as value. func (p *ContextOperator[B, C]) Inject(ctx context.Context, c C) context.Context { key := p.Key() return context.WithValue(ctx, key, c) } -// Extract injects TxCommitter. +// Extract returns TxCommitter extracted from context.Context. func (p *ContextOperator[B, C]) Extract(ctx context.Context) (C, bool) { key := p.Key() c, ok := ctx.Value(key).(C) return c, ok } -// Key get key (CtxKeyType) for injecting or extracting TxCommitter from context.Context. -func (p *ContextOperator[B, C]) Key() CtxKeyType { - return CtxKeyType(fmt.Sprintf("%p", p.beginner)) +// Key returns key (CtxKey) for injecting or extracting TxCommitter from context.Context. +func (p *ContextOperator[B, C]) Key() CtxKey { + return CtxKey(fmt.Sprintf("%p", p.beginner)) } diff --git a/stdlib/transactor.go b/stdlib/transactor.go index d50e52c..db02f28 100644 --- a/stdlib/transactor.go +++ b/stdlib/transactor.go @@ -13,7 +13,7 @@ type Transactor struct { operator *oniontx.ContextOperator[*DB, *Tx] } -// NewTransactor creates new Transactor. +// NewTransactor returns new Transactor. func NewTransactor(db *sql.DB) *Transactor { var ( base = &DB{DB: db} diff --git a/transactor.go b/transactor.go index 3e947a5..b6048a7 100644 --- a/transactor.go +++ b/transactor.go @@ -42,7 +42,7 @@ type Transactor[B TxBeginner[C, O], C TxCommitter, O any] struct { operator СtxOperator[C] } -// NewTransactor creates new Transactor. +// NewTransactor returns new Transactor. func NewTransactor[B TxBeginner[C, O], C TxCommitter, O any]( beginner B, operator СtxOperator[C]) *Transactor[B, C, O] {