From 47ef35b32c2e0f4a8e85b89f64bb7804aafbda8c Mon Sep 17 00:00:00 2001 From: MuZhou233 Date: Sat, 14 Dec 2024 19:00:28 +0800 Subject: [PATCH] fix: update golangci-lint to v1.62.2 --- .golangci.yml | 13 ++++++++++-- internal/lib/libcache/store_redis.go | 19 +++++++++++++----- internal/lib/libtype/sync_map.go | 30 +++++++++++++++++++++++----- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index fde147de..21189208 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,7 +2,7 @@ # This code is licensed under the terms of the MIT license https://opensource.org/license/mit # Copyright (c) 2021 Marat Reymers -## Golden config for golangci-lint v1.60.3 +## Golden config for golangci-lint v1.62.2 # # This is the best config for golangci-lint based on my experience and opinion. # It is very strict, but not extremely strict. @@ -186,6 +186,13 @@ linters-settings: # Default: true strconcat: false + reassign: + # Patterns for global variable names that are checked for reassignment. + # See https://github.com/curioswitch/go-reassign#usage + # Default: ["EOF", "Err.*"] + patterns: + - ".*" + rowserrcheck: # database/sql is always checked # Default: [] @@ -295,6 +302,7 @@ linters: - gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations - goprintffuncname # checks that printf-like functions are named with f at the end - gosec # inspects source code for security problems + - iface # checks the incorrect use of interfaces, helping developers avoid interface pollution - intrange # finds places where for loops could make use of an integer range #- lll # reports long lines. disable until https://github.com/golangci/golangci-lint/issues/1981 - loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap) @@ -315,6 +323,7 @@ linters: - promlinter # checks Prometheus metrics naming via promlint - protogetter # reports direct reads from proto message fields when getters should be used - reassign # checks that package variables are not reassigned + - recvcheck # checks for receiver type consistency - revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint - rowserrcheck # checks whether Err of rows is checked successfully - sloglint # ensure consistent code style when using log/slog @@ -356,7 +365,6 @@ linters: #- dupword # [useless without config] checks for duplicate words in the source code #- err113 # [too strict] checks the errors handling expressions #- errchkjson # [don't see profit + I'm against of omitting errors like in the first example https://github.com/breml/errchkjson] checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted - #- execinquery # [deprecated] checks query string in Query function which reads your Go src files and warning it finds #- exportloopref # [not necessary from Go 1.22] checks for pointers to enclosing loop variables #- forcetypeassert # [replaced by errcheck] finds forced type assertions #- gofmt # [replaced by goimports] checks whether code was gofmt-ed @@ -388,6 +396,7 @@ issues: linters: - bodyclose - dupl + - errcheck - funlen - goconst - gosec diff --git a/internal/lib/libcache/store_redis.go b/internal/lib/libcache/store_redis.go index e4e60703..3c34b425 100644 --- a/internal/lib/libcache/store_redis.go +++ b/internal/lib/libcache/store_redis.go @@ -3,6 +3,7 @@ package libcache import ( "context" "errors" + "fmt" "time" "github.com/redis/go-redis/v9" @@ -22,9 +23,17 @@ func newRedis(client *redis.Client, options ...Option) *redisStore { } } +func strKey(key any) string { + str, ok := key.(string) + if ok { + return str + } + return fmt.Sprintf("%v", key) +} + // Get returns data stored from a given key. func (s *redisStore) Get(ctx context.Context, key any) (any, error) { - object, err := s.client.Get(ctx, key.(string)).Result() + object, err := s.client.Get(ctx, strKey(key)).Result() if errors.Is(err, redis.Nil) { return nil, newNotFound(err) } @@ -36,7 +45,7 @@ func (s *redisStore) Get(ctx context.Context, key any) (any, error) { // GetWithTTL returns data stored from a given key and its corresponding TTL. func (s *redisStore) GetWithTTL(ctx context.Context, key any) (any, time.Duration, error) { - object, err := s.client.Get(ctx, key.(string)).Result() + object, err := s.client.Get(ctx, strKey(key)).Result() if errors.Is(err, redis.Nil) { return nil, 0, newNotFound(err) } @@ -44,7 +53,7 @@ func (s *redisStore) GetWithTTL(ctx context.Context, key any) (any, time.Duratio return nil, 0, err } - ttl, err := s.client.TTL(ctx, key.(string)).Result() + ttl, err := s.client.TTL(ctx, strKey(key)).Result() if err != nil { return nil, 0, err } @@ -56,7 +65,7 @@ func (s *redisStore) GetWithTTL(ctx context.Context, key any) (any, time.Duratio func (s *redisStore) Set(ctx context.Context, key any, value any, options ...Option) error { opts := applyOptionsWithDefault(s.options, options...) - err := s.client.Set(ctx, key.(string), value, opts.Expiration).Err() + err := s.client.Set(ctx, strKey(key), value, opts.Expiration).Err() if err != nil { return err } @@ -66,7 +75,7 @@ func (s *redisStore) Set(ctx context.Context, key any, value any, options ...Opt // Delete removes data from Redis for given key identifier. func (s *redisStore) Delete(ctx context.Context, key any) error { - _, err := s.client.Del(ctx, key.(string)).Result() + _, err := s.client.Del(ctx, strKey(key)).Result() return err } diff --git a/internal/lib/libtype/sync_map.go b/internal/lib/libtype/sync_map.go index 85db04af..cf8d4af1 100644 --- a/internal/lib/libtype/sync_map.go +++ b/internal/lib/libtype/sync_map.go @@ -18,12 +18,17 @@ func (m *SyncMap[K, V]) Load(key K) (V, bool) { if !ok { return value, ok } - return v.(V), ok + value, ok = v.(V) + return value, ok } func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool) { a, loaded := m.m.LoadOrStore(key, value) - return a.(V), loaded + existing, ok := a.(V) + if !ok { + return existing, false + } + return existing, loaded } func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool) { @@ -32,7 +37,11 @@ func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool) { if !loaded { return value, loaded } - return v.(V), loaded + value, ok := v.(V) + if !ok { + return value, false + } + return value, loaded } func (m *SyncMap[K, V]) Store(key K, value V) { @@ -40,12 +49,23 @@ func (m *SyncMap[K, V]) Store(key K, value V) { } func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) { - m.m.Range(func(key, value any) bool { return f(key.(K), value.(V)) }) + m.m.Range(func(key, value any) bool { + k, ok := key.(K) + v, ok2 := value.(V) + if !ok || !ok2 { + return true + } + return f(k, v) + }) } func (m *SyncMap[K, V]) Swap(key K, value V) (V, bool) { p, loaded := m.m.Swap(key, value) - return p.(V), loaded + value, ok := p.(V) + if !ok { + return value, false + } + return value, loaded } func (m *SyncMap[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool {