Skip to content

chore: introduce typed context keys - #2677

Open
xlgmokha wants to merge 1 commit into
masterfrom
xlgmokha/context-key-refactor
Open

chore: introduce typed context keys#2677
xlgmokha wants to merge 1 commit into
masterfrom
xlgmokha/context-key-refactor

Conversation

@xlgmokha

@xlgmokha xlgmokha commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Refactoring. This change adds a typed context key that can be used to add an item to context and fetch it safely.

What is the current behavior?

Each read from context.Context with an unchecked type assertion can panics if the value is the wrong type.

Before:

var tokenKey = contextKey("jwt")

func withToken(ctx context.Context, token *jwt.Token) context.Context {
  return context.WithValue(ctx, tokenKey, token)
}

func getToken(ctx context.Context) *jwt.Token {
  obj := ctx.Value(tokenKey)
  if obj == nil {
    return nil
  }
  return obj.(*jwt.Token)
}

What is the new behavior?

After:

var tokenKey = ctxkey.New[*jwt.Token]("jwt")

func withToken(ctx context.Context, token *jwt.Token) context.Context {
  return tokenKey.WithValue(ctx, token)
}

func getToken(ctx context.Context) *jwt.Token {
  return tokenKey.Value(ctx)
}

Additional context

@xlgmokha xlgmokha self-assigned this Aug 4, 2026
@xlgmokha
xlgmokha force-pushed the xlgmokha/context-key-refactor branch from c0394f3 to 3efb7ce Compare August 4, 2026 22:11
@blacksmith-sh

This comment has been minimized.

@xlgmokha
xlgmokha force-pushed the xlgmokha/context-key-refactor branch from bd0276f to d270ecd Compare August 5, 2026 15:46
@xlgmokha xlgmokha changed the title feat(api): delegate context helpers to generic typed key feat(context): delegate context helpers to generic typed key Aug 5, 2026
@xlgmokha xlgmokha changed the title feat(context): delegate context helpers to generic typed key chore: delegate context helpers to generic typed key Aug 5, 2026
@xlgmokha
xlgmokha force-pushed the xlgmokha/context-key-refactor branch 6 times, most recently from 079a840 to ed3e119 Compare August 5, 2026 18:45
@xlgmokha xlgmokha changed the title chore: delegate context helpers to generic typed key chore: introduce typed context keys Aug 5, 2026
@xlgmokha
xlgmokha force-pushed the xlgmokha/context-key-refactor branch from ed3e119 to 47d4baa Compare August 5, 2026 19:37
* Add `xcontext.Key[T]`, a generic context key that carries its own With/From accessors.
* Drops functionHooksKey, which had no readers or writers.
* Where a wrongly typed value once panicked it now returns the zero value.
@xlgmokha
xlgmokha force-pushed the xlgmokha/context-key-refactor branch from 47d4baa to 3cf9670 Compare August 5, 2026 19:43
@xlgmokha
xlgmokha marked this pull request as ready for review August 5, 2026 19:52
@xlgmokha
xlgmokha requested a review from a team as a code owner August 5, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant