summaryrefslogtreecommitdiffhomepage
path: root/util/ctxkey
AgeCommit message (Collapse)AuthorFilesLines
2024-02-08all: use reflect.TypeFor now available in Go 1.22 (#11078)Joe Tsai1-8/+3
Updates #cleanup Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2024-01-16all: make use of ctxkey everywhere (#10846)Joe Tsai2-28/+51
Also perform minor cleanups on the ctxkey package itself. Provide guidance on when to use ctxkey.Key[T] over ctxkey.New. Also, allow for interface kinds because the value wrapping trick also happens to fix edge cases with interfaces in Go. Updates #cleanup Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2024-01-12util/ctxkey: add package for type-safe context keys (#10841)Joe Tsai2-0/+221
The lack of type-safety in context.WithValue leads to the common pattern of defining of package-scoped type to ensure global uniqueness: type fooKey struct{} func withFoo(ctx context, v Foo) context.Context { return context.WithValue(ctx, fooKey{}, v) } func fooValue(ctx context) Foo { v, _ := ctx.Value(fooKey{}).(Foo) return v } where usage becomes: ctx = withFoo(ctx, foo) foo := fooValue(ctx) With many different context keys, this can be quite tedious. Using generics, we can simplify this as: var fooKey = ctxkey.New("mypkg.fooKey", Foo{}) where usage becomes: ctx = fooKey.WithValue(ctx, foo) foo := fooKey.Value(ctx) See https://go.dev/issue/49189 Updates #cleanup Signed-off-by: Joe Tsai <joetsai@digital-static.net>