Skip to content

Commit 07e1641

Browse files
committed
Fix all 50 golangci-lint issues across multiple categories
ERRCHECK FIXES (36 issues): - Fixed all unchecked error returns in test files by adding _ = prefix - Fixed unchecked cache.Set/Delete/Close calls with explicit error ignoring - Fixed unchecked cache.Set calls in wrap.go for error caching GOCONST FIXES (3 issues): - examples/advanced/main.go: Made "unknown" a constant - pkg/obcache/wrap_extra_test.go: Made "result" a constant GOSEC FIXES (2 issues): - examples/metrics/main.go: Added HTTP server timeouts to prevent G114 - pkg/obcache/hooks_test.go: Fixed integer overflow in G115 by explicit casting STATICCHECK FIXES (9 issues): - Fixed ST1005: Made error string lowercase in cache.go - Fixed SA1029: Used custom context key types instead of string keys - Fixed SA9003: Added proper error handling in logging examples - Fixed SA5011: Added nil check and return to prevent dereference All test files now properly handle errors with explicit ignoring where appropriate. All example code follows Go best practices for context keys and error handling.
1 parent c2e4328 commit 07e1641

16 files changed

Lines changed: 62 additions & 50 deletions

examples/advanced/main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ func (s *UserService) GetUserWithContext(ctx context.Context, id int) (*User, er
7878
s.mu.Unlock()
7979

8080
// Extract request metadata from context
81-
requestID := "unknown"
82-
if rid, ok := ctx.Value("requestID").(string); ok {
81+
const unknownRequestID = "unknown"
82+
requestID := unknownRequestID
83+
if rid, ok := ctx.Value(requestIDKey).(string); ok {
8384
requestID = rid
8485
}
8586

@@ -128,7 +129,7 @@ func main() {
128129
OnHitCtx: []obcache.OnHitHookCtx{
129130
func(ctx context.Context, key string, value any, args []any) {
130131
requestID := "unknown"
131-
if rid, ok := ctx.Value("requestID").(string); ok {
132+
if rid, ok := ctx.Value(requestIDKey).(string); ok {
132133
requestID = rid
133134
}
134135
fmt.Printf("🎯 Context-aware HIT: %s (request: %s, args: %v)\n", key, requestID, args)
@@ -137,7 +138,7 @@ func main() {
137138
OnMissCtx: []obcache.OnMissHookCtx{
138139
func(ctx context.Context, key string, args []any) {
139140
requestID := "unknown"
140-
if rid, ok := ctx.Value("requestID").(string); ok {
141+
if rid, ok := ctx.Value(requestIDKey).(string); ok {
141142
requestID = rid
142143
}
143144
fmt.Printf("🎯 Context-aware MISS: %s (request: %s, args: %v)\n", key, requestID, args)
@@ -187,8 +188,10 @@ func main() {
187188
cachedGetUserWithContext := obcache.Wrap(cache, userService.GetUserWithContext)
188189

189190
// Create contexts with request metadata
190-
ctx1 := context.WithValue(context.Background(), "requestID", "req-001")
191-
ctx2 := context.WithValue(context.Background(), "requestID", "req-002")
191+
type contextKey string
192+
const requestIDKey contextKey = "requestID"
193+
ctx1 := context.WithValue(context.Background(), requestIDKey, "req-001")
194+
ctx2 := context.WithValue(context.Background(), requestIDKey, "req-002")
192195

193196
// First call with context - cache miss
194197
ctxUser1, err := cachedGetUserWithContext(ctx1, 10)

examples/metrics/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ func prometheusExample() {
130130
go func() {
131131
http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
132132
fmt.Println("📊 Prometheus metrics server started on :8080/metrics")
133-
if err := http.ListenAndServe(":8080", nil); err != nil {
133+
server := &http.Server{
134+
Addr: ":8080",
135+
ReadTimeout: 10 * time.Second,
136+
WriteTimeout: 10 * time.Second,
137+
}
138+
if err := server.ListenAndServe(); err != nil {
134139
log.Printf("Prometheus server error: %v", err)
135140
}
136141
}()

pkg/obcache/bench_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func BenchmarkConcurrentCacheGet(b *testing.B) {
283283

284284
// Pre-populate cache
285285
for i := 0; i < 1000; i++ {
286-
cache.Set(fmt.Sprintf("key-%d", i), i, time.Hour)
286+
_ = cache.Set(fmt.Sprintf("key-%d", i), i, time.Hour) // Benchmark setup
287287
}
288288

289289
b.ResetTimer()
@@ -654,7 +654,9 @@ func BenchmarkHookConditionEvaluation(b *testing.B) {
654654
contextCondition := ContextValueCondition("env", "prod")
655655
andCondition := AndCondition(prefixCondition, contextCondition)
656656

657-
ctx := context.WithValue(context.Background(), "env", "prod")
657+
type contextKey string
658+
const envKey contextKey = "env"
659+
ctx := context.WithValue(context.Background(), envKey, "prod")
658660

659661
b.ResetTimer()
660662
b.ReportAllocs()

pkg/obcache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func createMemoryStore(config *Config) (store.Store, error) {
165165
// createRedisStore creates a Redis-based store
166166
func createRedisStore(config *Config) (store.Store, error) {
167167
if config.Redis == nil {
168-
return nil, fmt.Errorf("Redis configuration is required when using StoreTypeRedis")
168+
return nil, fmt.Errorf("redis configuration is required when using StoreTypeRedis")
169169
}
170170

171171
redisConfig := &redisstore.Config{

pkg/obcache/cache_extra_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestCacheTTLMethod(t *testing.T) {
136136
}
137137

138138
// Add an entry with default TTL via Put
139-
cache.Put("default-ttl", "value-default-ttl")
139+
_ = cache.Put("default-ttl", "value-default-ttl") // Test setup
140140

141141
value, found = cache.Get("default-ttl")
142142
if !found {
@@ -171,7 +171,7 @@ func TestCacheClear(t *testing.T) {
171171
}
172172

173173
// Clear all
174-
cache.Clear()
174+
_ = cache.Clear() // Test cleanup
175175

176176
if cache.Len() != 0 {
177177
t.Fatalf("Expected 0 entries after Clear, got %d", cache.Len())
@@ -194,7 +194,7 @@ func TestCacheClose(t *testing.T) {
194194
_ = cache.Set("key1", "value1", time.Hour)
195195

196196
// Close should not error
197-
cache.Close()
197+
_ = cache.Close() // Test cleanup
198198

199199
// After close, operations should still work but cleanup stops
200200
_ = cache.Set("key2", "value2", time.Hour)
@@ -260,7 +260,7 @@ func TestCacheClearWithHooks(t *testing.T) {
260260
_ = cache.Set("key2", "value2", time.Hour)
261261

262262
// Invalidate all should trigger hooks
263-
cache.Clear()
263+
_ = cache.Clear() // Test cleanup
264264

265265
// Should have called invalidate hook for each entry
266266
if invalidateCount != 2 {

pkg/obcache/cache_operations_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestCacheStatsTableDriven(t *testing.T) {
201201
{
202202
name: "hit and miss counts",
203203
operations: func(c *Cache) {
204-
c.Set("key1", "value1", time.Hour)
204+
_ = c.Set("key1", "value1", time.Hour) // Test setup
205205
c.Get("key1") // hit
206206
c.Get("key2") // miss
207207
c.Get("key1") // hit
@@ -222,10 +222,10 @@ func TestCacheStatsTableDriven(t *testing.T) {
222222
{
223223
name: "key count tracking",
224224
operations: func(c *Cache) {
225-
c.Set("key1", "value1", time.Hour)
226-
c.Set("key2", "value2", time.Hour)
227-
c.Set("key3", "value3", time.Hour)
228-
c.Delete("key2")
225+
_ = c.Set("key1", "value1", time.Hour) // Test setup
226+
_ = c.Set("key2", "value2", time.Hour) // Test setup
227+
_ = c.Set("key3", "value3", time.Hour) // Test setup
228+
_ = c.Delete("key2") // Test operation
229229
},
230230
verify: func(t *testing.T, s *Stats) {
231231
if s.KeyCount() != 2 {

pkg/obcache/cache_redis_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ func TestCacheWithRedisStoreAndHooks(t *testing.T) {
135135
}
136136

137137
// Test hit hook
138-
cache.Set(testKey, "value", time.Hour)
138+
_ = cache.Set(testKey, "value", time.Hour) // Test setup
139139
_, _ = cache.Get(testKey)
140140
if !hitCalled {
141141
t.Fatal("Expected hit hook to be called")
142142
}
143143

144144
// Test invalidate hook
145-
cache.Delete(testKey)
145+
_ = cache.Delete(testKey) // Test operation
146146
if !invalidateCalled {
147147
t.Fatal("Expected invalidate hook to be called")
148148
}

pkg/obcache/cache_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestCacheDelete(t *testing.T) {
6868

6969
const testKey = "test-key"
7070
key := testKey
71-
cache.Set(key, "value", time.Hour)
71+
_ = cache.Set(key, "value", time.Hour) // Test setup
7272

7373
// Verify it exists
7474
_, found := cache.Get(key)
@@ -159,7 +159,7 @@ func TestCacheTTL(t *testing.T) {
159159
key := testKey
160160
shortTTL := 10 * time.Millisecond
161161

162-
cache.Set(key, "value", shortTTL)
162+
_ = cache.Set(key, "value", shortTTL) // Test setup
163163

164164
// Should exist immediately
165165
_, found := cache.Get(key)
@@ -233,7 +233,7 @@ func TestCacheConcurrency(t *testing.T) {
233233
for j := 0; j < numOperations; j++ {
234234
key := fmt.Sprintf("key-%d-%d", id, j)
235235
value := fmt.Sprintf("value-%d-%d", id, j)
236-
cache.Set(key, value, time.Hour)
236+
_ = cache.Set(key, value, time.Hour) // Test setup
237237
}
238238
}(i)
239239
}
@@ -271,7 +271,7 @@ func TestCacheReset(t *testing.T) {
271271
_ = cache.Set("key2", "value2", time.Hour)
272272
cache.Get("key1")
273273
cache.Get("nonexistent")
274-
cache.Delete("key2")
274+
_ = cache.Delete("key2") // Test operation
275275

276276
stats := cache.Stats()
277277
if stats.Total() == 0 {

pkg/obcache/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestNewCacheWithConfig(t *testing.T) {
139139

140140
// Fill the cache beyond the max entries to test eviction
141141
for i := 0; i < 60; i++ { // More than the 50 limit
142-
cache.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i), time.Hour)
142+
_ = cache.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i), time.Hour) // Test setup
143143
}
144144

145145
stats := cache.Stats()

pkg/obcache/hooks_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestHookExecution(t *testing.T) {
4848
}
4949

5050
// Test OnHit hook
51-
cache.Set("key1", "value1", time.Hour)
51+
_ = cache.Set("key1", "value1", time.Hour)
5252
_, found = cache.Get("key1")
5353
if !found {
5454
t.Fatal("Expected hit")
@@ -58,15 +58,15 @@ func TestHookExecution(t *testing.T) {
5858
}
5959

6060
// Test OnInvalidate hook
61-
cache.Delete("key1")
61+
_ = cache.Delete("key1")
6262
if atomic.LoadInt32(&invalidateCount) != 1 {
6363
t.Fatalf("Expected 1 invalidate hook call, got %d", invalidateCount)
6464
}
6565

6666
// Test OnEvict hook
67-
cache.Set("key2", "value2", time.Hour)
68-
cache.Set("key3", "value3", time.Hour)
69-
cache.Set("key4", "value4", time.Hour) // Should evict key2 (LRU)
67+
_ = cache.Set("key2", "value2", time.Hour)
68+
_ = cache.Set("key3", "value3", time.Hour)
69+
_ = cache.Set("key4", "value4", time.Hour) // Should evict key2 (LRU)
7070

7171
// Give some time for eviction to be processed
7272
time.Sleep(10 * time.Millisecond)
@@ -108,7 +108,7 @@ func TestHookParameters(t *testing.T) {
108108
testKey := "test-key"
109109
testValue := "test-value"
110110

111-
cache.Set(testKey, testValue, time.Hour)
111+
_ = cache.Set(testKey, testValue, time.Hour)
112112
cache.Get(testKey)
113113

114114
mu.Lock()
@@ -124,7 +124,7 @@ func TestHookParameters(t *testing.T) {
124124
mu.Unlock()
125125

126126
// Test evict hook parameters
127-
cache.Set("new-key", "new-value", time.Hour) // Should evict previous entry
127+
_ = cache.Set("new-key", "new-value", time.Hour) // Should evict previous entry
128128
time.Sleep(10 * time.Millisecond)
129129

130130
mu.Lock()
@@ -165,7 +165,7 @@ func TestHookConcurrency(t *testing.T) {
165165

166166
// Add some data
167167
for i := 0; i < 10; i++ {
168-
cache.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i), time.Hour)
168+
_ = cache.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i), time.Hour)
169169
}
170170

171171
// Concurrent cache operations to trigger hooks
@@ -191,7 +191,7 @@ func TestHookConcurrency(t *testing.T) {
191191

192192
wg.Wait()
193193

194-
expectedCalls := int32(numGoroutines * numOperations)
194+
expectedCalls := int32(numGoroutines) * int32(numOperations)
195195
actualCalls := atomic.LoadInt32(&hookCallCount)
196196

197197
if actualCalls != expectedCalls {
@@ -220,7 +220,7 @@ func TestMultipleHooksOfSameType(t *testing.T) {
220220
t.Fatalf("Failed to create cache: %v", err)
221221
}
222222

223-
cache.Set("key1", "value1", time.Hour)
223+
_ = cache.Set("key1", "value1", time.Hour)
224224
cache.Get("key1")
225225

226226
if atomic.LoadInt32(&hook1Calls) != 1 {
@@ -287,10 +287,10 @@ func TestNilHooks(t *testing.T) {
287287
t.Fatalf("Failed to create cache: %v", err)
288288
}
289289

290-
cache.Set("key1", "value1", time.Hour)
290+
_ = cache.Set("key1", "value1", time.Hour)
291291
cache.Get("key1")
292292
cache.Get("nonexistent")
293-
cache.Delete("key1")
293+
_ = cache.Delete("key1")
294294

295295
// If we reach here without panic, test passes
296296
}
@@ -303,10 +303,10 @@ func TestEmptyHooks(t *testing.T) {
303303
t.Fatalf("Failed to create cache: %v", err)
304304
}
305305

306-
cache.Set("key1", "value1", time.Hour)
306+
_ = cache.Set("key1", "value1", time.Hour)
307307
cache.Get("key1")
308308
cache.Get("nonexistent")
309-
cache.Delete("key1")
309+
_ = cache.Delete("key1")
310310

311311
// If we reach here without panic, test passes
312312
}
@@ -325,7 +325,7 @@ func TestHookErrorHandling(t *testing.T) {
325325
t.Fatalf("Failed to create cache: %v", err)
326326
}
327327

328-
cache.Set("key1", "value1", time.Hour)
328+
_ = cache.Set("key1", "value1", time.Hour)
329329

330330
// This should not panic even though the hook panics
331331
// The cache should continue to function normally

0 commit comments

Comments
 (0)