summaryrefslogtreecommitdiff
path: root/vendor/github.com/xlgmokha/x/pkg/context/key.go
blob: 80ca7bd2ef15d206e53dbbd757c0564081fc68df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package context

import (
	"context"

	"github.com/xlgmokha/x/pkg/x"
)

type Key[T any] string

func (self Key[T]) With(ctx context.Context, value T) context.Context {
	return context.WithValue(ctx, self, value)
}

func (self Key[T]) From(ctx context.Context) T {
	if value := ctx.Value(self); value != nil {
		return value.(T)
	}
	return x.Zero[T]()
}