8000 Change `key` accessing for default `ContextOperator` · Issue #90 · kozmod/oniontx · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Change key accessing for default ContextOperator #90
Open
@kozmod

Description

@kozmod

Description

Default ContextOperator uses genetic type any(v0.2.3) (comparable #88 ) as key for storing/getting Tx.

Changing the key field to the function that return (calculate) the key, will make ContextOperator more flexible.

// ContextOperator inject and extract Tx from context.Context.
type ContextOperator[B comparable, T Tx] struct {
	key func() B
}

// NewContextOperator returns new ContextOperator.
func NewContextOperator[B comparable, T Tx](key func() B) *ContextOperator[B, T] {
	return &ContextOperator[B, T]{
		key: key,
	}
}

// Inject returns new context.Context contains Tx as value.
func (p *ContextOperator[B, T]) Inject(ctx context.Context, tx T) context.Context {
	return context.WithValue(ctx, p.key(), tx)
}

// Extract returns Tx extracted from context.Context.
func (p *ContextOperator[B, T]) Extract(ctx context.Context) (T, bool) {
	c, ok := ctx.Value(p.key()).(T)
	return c, ok
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestgood first issueGood for newcomershelp wantedExtra attention is neededholdExtra discussion is needed

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0