Repository navigation
all: make use of ctxkey everywhere - #10846
Conversation
d225318 to
3babb83
Compare
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 <[email protected]>
3babb83 to
5b6c38d
Compare
|
|
||
| // FromContext extracts a log function from ctx. | ||
| // | ||
| // Deprecated: Use [LogfKey.Value] instead. |
There was a problem hiding this comment.
Is there some reason not to have a semantically-relevant helper? It's not clear to me we get much benefit from moving the thing the caller depends on from a simple helper function to a compound call on a value in the package. (I get that the helper is no longer necessary, but I think it reads better, esp. given that loggers are going to be used ~everywhere. But YMMV).
There was a problem hiding this comment.
One detriment of helpers is pollution of the namespace.
With ctxkey, we end up with single declaration like:
var ThingKey ctxkey[T] = ...In contrast, the helpers come as a pair of declarations typically named as:
func WithThing(context.Context, T) context.Context
func ThingValue(context.Context) (T, bool)From godoc, these pair of helpers don't sort next to each other, making detection more difficult. Trying to find helpers that deal with context values is somewhat difficult as searching for "context.Context" or "T" usually produces many false positives. On the other hand, we can just search for "ctxkey".
The other detriment is the need to write extra code. One of the value propositions of ctxkey.Key is that you don't have manually declare these two helpers to gain type safety. If we're always going to declare the helper functions in addition to using ctxkey.Key, then I'm not sure if ctxkey.Key is providing any value.
There was a problem hiding this comment.
Not a blocker, but I disagree with the weighting of that reasoning: Not that layout doesn't matter, but cognitive burden to the reader at the call site is IMO more important than, say, where the functions sort in a listing.
And we're still "polluting" the namespace, only now it's with a mutable variable (morally immutable, but as a reader when I see a variable reference I know it's a variable, and that makes me attend to whether that affects its use).
There was a problem hiding this comment.
For the callsite, we have:
| construction | extraction | |
|---|---|---|
with ctxkey |
ctx := mypkg.ThingKey.WithValue(ctx, v) |
v := mypkg.ThingKey.Value(ctx) |
| user defined | ctx := mypkg.WithThing(ctx, v) |
v := mypkg.ThingValue(ctx) |
Thoughts:
- The existence of the
ThingKeynamespace could be seen as additional cognitive burden. - However, with
ctxkey, we will always have aWithValue(ctx, ...)orValue(ctx)suffix, while with user-defined helpers, we're left with whether people follow a consistent convention (and we don't). I'd argue that consistency helps with cognitive burden. - Using
ctxkeyis not always more typing as some helpers are named asContextWithThingorThingFromContext, which can be longer.
it's a variable, and that makes me attend to whether that affects its use
Fair criticism. It'd be nice if I could make it a constant, but I don't know how. The alternative is to make it a argument-less function that just returns the ctxkey.Key, but that seems to buy us little.
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 <[email protected]>
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 <[email protected]>
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