Skip to content

Commit

Permalink
[feature] fix operator and transactor functions description; rena…
Browse files Browse the repository at this point in the history
…me `CtxKeyType` -> `CtxKey`
  • Loading branch information
kozmod committed Sep 9, 2023
1 parent 2474e08 commit d93a1d7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion stdlib/transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down

0 comments on commit d93a1d7

Please sign in to comment.