Skip to content

Commit

Permalink
fix: update golangci-lint to v1.62.2
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Dec 14, 2024
1 parent 1fddc98 commit 47ef35b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
13 changes: 11 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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: []
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -388,6 +396,7 @@ issues:
linters:
- bodyclose
- dupl
- errcheck
- funlen
- goconst
- gosec
Expand Down
19 changes: 14 additions & 5 deletions internal/lib/libcache/store_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libcache
import (
"context"
"errors"
"fmt"
"time"

"github.com/redis/go-redis/v9"
Expand All @@ -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)
}
Expand All @@ -36,15 +45,15 @@ 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)
}
if err != nil {
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
}
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
30 changes: 25 additions & 5 deletions internal/lib/libtype/sync_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -32,20 +37,35 @@ 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) {
m.m.Store(key, value)
}

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 {
Expand Down

0 comments on commit 47ef35b

Please sign in to comment.