github.com/octohelm/wagon@v0.0.0-20240308040401-88662650dc0b/pkg/ctxutil/ctx.go (about)

     1  package ctxutil
     2  
     3  import (
     4  	contextx "github.com/octohelm/x/context"
     5  	"golang.org/x/net/context"
     6  )
     7  
     8  func New[T any]() Context[T] {
     9  	return ctx[T]{}
    10  }
    11  
    12  type Context[T any] interface {
    13  	Inject(ctx context.Context, value T) context.Context
    14  	From(ctx context.Context) T
    15  }
    16  
    17  type ctx[T any] struct {
    18  }
    19  
    20  func (c ctx[T]) Inject(ctx context.Context, value T) context.Context {
    21  	return contextx.WithValue(ctx, c, value)
    22  }
    23  
    24  func (c ctx[T]) From(ctx context.Context) T {
    25  	return ctx.Value(c).(T)
    26  }