# 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: ```go 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: ```go 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