Skip to content

Commit

Permalink
chore(lint): Optimize a large number of lint detection issues (#57)
Browse files Browse the repository at this point in the history
* build(lint): Added lint detection

* chore(lint): Optimize a large number of lint detection issues

* chore(lint): Optimize a large number of lint detection issues

* chore(lint): Optimize a large number of lint detection issues
  • Loading branch information
flc1125 authored Jan 5, 2024
1 parent c5f0e53 commit 5c8aa65
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 105 deletions.
28 changes: 28 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
linters:
disable-all: true

enable:
- bodyclose
- dogsled
- durationcheck
- errcheck
- exportloopref
- govet
- gosimple
- gofmt
- gofumpt
- goconst
- goimports
- gomnd
- gocyclo
- ineffassign
- lll
- prealloc
- revive
- staticcheck
- typecheck
- unused
- whitespace
- wastedassign
- unconvert
- misspell
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.PHONY: lint
lint:
golangci-lint run
@echo "Linting complete"

.PHONY: fix
fix:
golangci-lint run --fix
@echo "Lint fixing complete"
6 changes: 3 additions & 3 deletions bytes/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Bytes int64

const (
KB Bytes = 1 << (10 * (iota + 1))
KB Bytes = 1 << (10 * (iota + 1)) //nolint:gomnd
MB
GB
TB
Expand Down Expand Up @@ -83,7 +83,7 @@ func (b Bytes) EB() float64 {
}

func (b Bytes) HumanizeValue(lens ...int) string {
var l = 2
l := 2
if len(lens) > 0 {
l = lens[0]
}
Expand All @@ -92,5 +92,5 @@ func (b Bytes) HumanizeValue(lens ...int) string {
}

func (b Bytes) Humanize(lens ...int) string {
return fmt.Sprintf("%s", b.HumanizeValue(lens...)) + b.Unit()
return b.HumanizeValue(lens...) + b.Unit()
}
70 changes: 41 additions & 29 deletions cache/redis/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ func Codec(codec codec.Codec) Option {
}
}

var (
_ cache.Store = (*Store)(nil)
)
var _ cache.Store = (*Store)(nil)

func New(redis redis.Cmdable, opts ...Option) *Store {
opt := &options{
Expand All @@ -58,71 +56,85 @@ func New(redis redis.Cmdable, opts ...Option) *Store {
}

func (s *Store) Has(ctx context.Context, key string) (bool, error) {
if r := s.redis.Exists(ctx, s.opts.prefix+key); r.Err() != nil {
r := s.redis.Exists(ctx, s.opts.prefix+key)
if r.Err() != nil {
return false, r.Err()
} else {
return r.Val() > 0, nil
}

return r.Val() > 0, nil
}

func (s *Store) Get(ctx context.Context, key string, dest interface{}) error {
if r := s.redis.Get(ctx, s.opts.prefix+key); r.Err() != nil {
r := s.redis.Get(ctx, s.opts.prefix+key)
if r.Err() != nil {
return r.Err()
} else {
return s.opts.codec.Unmarshal([]byte(r.Val()), dest)
}

return s.opts.codec.Unmarshal([]byte(r.Val()), dest)
}

func (s *Store) Put(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error) {
if valued, err := s.opts.codec.Marshal(value); err != nil {
valued, err := s.opts.codec.Marshal(value)
if err != nil {
return false, err
} else if r := s.redis.Set(ctx, s.opts.prefix+key, valued, ttl); r.Err() != nil {
}

r := s.redis.Set(ctx, s.opts.prefix+key, valued, ttl)
if r.Err() != nil {
return false, r.Err()
} else {
return r.Val() == "OK", nil
}

return r.Val() == "OK", nil
}

func (s *Store) Increment(ctx context.Context, key string, value int) (int, error) {
if r := s.redis.IncrBy(ctx, s.opts.prefix+key, int64(value)); r.Err() != nil {
r := s.redis.IncrBy(ctx, s.opts.prefix+key, int64(value))
if r.Err() != nil {
return 0, r.Err()
} else {
return int(r.Val()), nil
}

return int(r.Val()), nil
}

func (s *Store) Decrement(ctx context.Context, key string, value int) (int, error) {
if r := s.redis.DecrBy(ctx, s.opts.prefix+key, int64(value)); r.Err() != nil {
r := s.redis.DecrBy(ctx, s.opts.prefix+key, int64(value))
if r.Err() != nil {
return 0, r.Err()
} else {
return int(r.Val()), nil
}

return int(r.Val()), nil
}

func (s *Store) Forever(ctx context.Context, key string, value interface{}) (bool, error) {
if valued, err := s.opts.codec.Marshal(value); err != nil {
valued, err := s.opts.codec.Marshal(value)
if err != nil {
return false, err
} else if r := s.redis.Set(ctx, s.opts.prefix+key, valued, redis.KeepTTL); r.Err() != nil {
}

r := s.redis.Set(ctx, s.opts.prefix+key, valued, redis.KeepTTL)
if r.Err() != nil {
return false, r.Err()
} else {
return r.Val() == "OK", nil
}

return r.Val() == "OK", nil
}

func (s *Store) Forget(ctx context.Context, key string) (bool, error) {
if r := s.redis.Del(ctx, s.opts.prefix+key); r.Err() != nil {
r := s.redis.Del(ctx, s.opts.prefix+key)
if r.Err() != nil {
return false, r.Err()
} else {
return r.Val() > 0, nil
}

return r.Val() > 0, nil
}

func (s *Store) Flush(ctx context.Context) (bool, error) {
if r := s.redis.FlushAll(ctx); r.Err() != nil {
r := s.redis.FlushAll(ctx)
if r.Err() != nil {
return false, r.Err()
} else {
return r.Val() == "OK", nil
}

return r.Val() == "OK", nil
}

func (s *Store) GetPrefix() string {
Expand Down
29 changes: 18 additions & 11 deletions cache/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ func NewRepository(store Store) Repository {
}

func (r *repository) Missing(ctx context.Context, key string) (bool, error) {
if had, err := r.Store.Has(ctx, key); err != nil {
had, err := r.Store.Has(ctx, key)
if err != nil {
return false, err
} else {
return !had, nil
}

return !had, nil
}

func (r *repository) Add(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error) {
// if the store is addable, use it
if store, ok := r.Store.(Addable); ok {
Expand All @@ -44,14 +46,14 @@ func (r *repository) Add(ctx context.Context, key string, value interface{}, ttl
if missing, err := r.Missing(ctx, key); err != nil {
return false, err
} else if missing {
if status, err := r.Set(ctx, key, value, ttl); err != nil {
status, err := r.Set(ctx, key, value, ttl)
if err != nil {
return false, err
} else {
return status, nil
}
} else {
return false, nil
return status, nil
}

return false, nil
}

func (r *repository) Delete(ctx context.Context, key string) (bool, error) {
Expand All @@ -62,7 +64,12 @@ func (r *repository) Set(ctx context.Context, key string, value interface{}, ttl
return r.Put(ctx, key, value, ttl)
}

func (r *repository) Remember(ctx context.Context, key string, dest interface{}, value func() interface{}, ttl time.Duration) error {
func (r *repository) Remember(
ctx context.Context,
key string, dest interface{},
value func() interface{},
ttl time.Duration,
) error {
if missing, err := r.Missing(ctx, key); err != nil {
return err
} else if missing {
Expand All @@ -73,7 +80,7 @@ func (r *repository) Remember(ctx context.Context, key string, dest interface{},
}

return helper.ValueOf(v, dest)
} else {
return r.Get(ctx, key, dest)
}

return r.Get(ctx, key, dest)
}
3 changes: 2 additions & 1 deletion codec/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package codec_test

import (
"log"
"testing"

"github.com/go-kratos-ecosystem/components/v2/codec/json"
)

var j = json.Codec

func ExampleJSON() {
func TestJson(_ *testing.T) {
bytes, err := j.Marshal(map[string]string{
"key": "value",
})
Expand Down
7 changes: 3 additions & 4 deletions codec/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

func TestJSON(t *testing.T) {
var j1, j2 = Codec, Codec
j1, j2 := Codec, Codec

assert.Same(t, j1, j2)

var data = map[string]interface{}{
data := map[string]interface{}{
"foo": "bar",
}

Expand All @@ -26,10 +26,9 @@ func TestJSON(t *testing.T) {
assert.Equal(t, bytes1, bytes2)

// unmarshal
var dest1, dest2 = make(map[string]interface{}), make(map[string]interface{})
dest1, dest2 := make(map[string]interface{}), make(map[string]interface{})
assert.NoError(t, json.Unmarshal(bytes1, &dest1))
assert.NoError(t, j1.Unmarshal(bytes1, &dest2))

assert.Equal(t, dest1, dest2)

}
4 changes: 1 addition & 3 deletions crontab/mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"errors"
)

var (
ErrAnotherServerRunning = errors.New("crontab: another server running")
)
var ErrAnotherServerRunning = errors.New("crontab: another server running")

type Mutex interface {
Lock(ctx context.Context, name string) error
Expand Down
2 changes: 1 addition & 1 deletion crontab/mutex/redis/mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func WithExpired(expired time.Duration) Option {
func New(redis redis.Cmdable, opts ...Option) *Mutex {
m := &Mutex{
Cmdable: redis,
expired: time.Second * 60,
expired: time.Second * 60, //nolint:gomnd
serverid: uuid.New().String(),
}

Expand Down
4 changes: 2 additions & 2 deletions crontab/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *Server) Start(ctx context.Context) error {
s.cron.Stop()
}

s.mutex.Unlock(ctx, s.name)
s.mutex.Unlock(ctx, s.name) //nolint:errcheck
timer.Stop()
}()

Expand Down Expand Up @@ -106,7 +106,7 @@ func (s *Server) start() {
s.log("crontab: server started")
}

func (s *Server) Stop(ctx context.Context) error {
func (s *Server) Stop(_ context.Context) error {
s.log("crontab: server stopping")

close(s.stoped)
Expand Down
2 changes: 1 addition & 1 deletion debug/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func TestDump(t *testing.T) {
func TestDump(_ *testing.T) {
Dump("foo", []byte("1234567890"), &struct {
Name string
}{
Expand Down
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Conflict(message string) *errors.Error {
func InternalServer(message string) *errors.Error {
return errors.InternalServer(http.StatusText(http.StatusInternalServerError), message)
}

func ServiceUnavailable(message string) *errors.Error {
return errors.ServiceUnavailable(http.StatusText(http.StatusServiceUnavailable), message)
}
Expand Down
Loading

0 comments on commit 5c8aa65

Please sign in to comment.