Files
goplt/docs/adr/0009-context-key-types.md
0x1d 6a17236474 docs: add implementation plan, ADRs, and task tracking system
- Add comprehensive 8-phase implementation plan (docs/plan.md)
- Add 28 Architecture Decision Records (docs/adr/) covering all phases
- Add task tracking system with 283+ task files (docs/stories/)
- Add task generator script for automated task file creation
- Add reference playbooks and requirements documentation

This commit establishes the complete planning foundation for the Go
Platform implementation, documenting all architectural decisions and
providing detailed task breakdown for Phases 0-8.
2025-11-04 22:05:37 +01:00

1.4 KiB

ADR-0009: Context Key Types

Status

Accepted

Context

The platform will use context.Context to propagate request-scoped values such as:

  • User ID (from authentication)
  • Request ID (for tracing)
  • Tenant ID (for multi-tenancy)
  • Logger instance (with request-scoped fields)

Go best practices recommend using typed keys instead of string keys to avoid collisions.

Decision

Use typed context keys for all context values:

type contextKey string

const (
    userIDKey contextKey = "user_id"
    requestIDKey contextKey = "request_id"
    tenantIDKey contextKey = "tenant_id"
    loggerKey contextKey = "logger"
)

Rationale:

  • Prevents key collisions between packages
  • Type-safe access to context values
  • Aligns with Go best practices (see context.WithValue documentation)
  • Makes context usage explicit and discoverable

Consequences

Positive

  • Type-safe context access
  • Prevents accidental key collisions
  • Clear intent in code
  • Better IDE support

Negative

  • Slightly more verbose than string keys
  • Requires defining keys upfront

Implementation Notes

  • Create pkg/context/keys.go with all context key definitions
  • Provide helper functions for setting/getting values:
    func WithUserID(ctx context.Context, userID string) context.Context
    func UserIDFromContext(ctx context.Context) (string, bool)
    
  • Use in middleware and services
  • Document all context keys and their usage