Skip to content

v3.1.1 - Enhanced Type Safety for Context Keys

Latest

Choose a tag to compare

@rendis rendis released this 29 Sep 10:05

🔧 Improvements

Enhanced Type Safety for Context Keys

  • Introduced ContextKeyType: Added a custom type for context keys to improve type safety
  • Updated GetCtxKey() return type: Now returns ContextKeyType instead of string
  • Automatic type conversion in SetCtxKey(): String parameters are automatically converted to ContextKeyType

Why This Matters

This change addresses Go's static analysis warning SA1029: "should not use built-in type string as key for value; define your own type to avoid collisions". By using a custom type for context keys, we ensure:

  • ✅ Compliance with Go's context package best practices
  • ✅ Prevention of key collisions between packages
  • ✅ Better static analysis and code quality
  • ✅ Seamless usage with context.WithValue() without warnings

Breaking Changes

⚠️ Minor breaking change: GetCtxKey() now returns ContextKeyType instead of string. If you were directly using the return value as a string, you may need to cast it or adjust your code.

Migration

Most users won't need to change anything. If you encounter type issues:

// Before (if you were doing direct string comparison)
if someKey == abslog.GetCtxKey() { ... }

// After
if someKey == string(abslog.GetCtxKey()) { ... }

Full Changelog: v3.1.0...v3.1.1