From 550a3b7f5d92254e8fbdf55f853ed7c3bc3da6f7 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Sat, 26 Aug 2023 21:19:53 +0800 Subject: [PATCH] chore: cleanup codes --- common/cache/lrucache.go | 11 ++++------- common/picker/picker_test.go | 10 +++------- common/queue/queue.go | 11 ++++------- component/resource/fetcher.go | 25 +++++++++++-------------- 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/common/cache/lrucache.go b/common/cache/lrucache.go index 1373b0be..2f9d3e79 100644 --- a/common/cache/lrucache.go +++ b/common/cache/lrucache.go @@ -7,6 +7,8 @@ import ( "time" "github.com/Dreamacro/clash/common/generics/list" + + "github.com/samber/lo" ) // Option is part of Functional Options Pattern @@ -87,7 +89,7 @@ func (c *LruCache[K, V]) Get(key K) (V, bool) { el := c.get(key) if el == nil { - return getZero[V](), false + return lo.Empty[V](), false } value := el.value @@ -119,7 +121,7 @@ func (c *LruCache[K, V]) GetWithExpire(key K) (V, time.Time, bool) { el := c.get(key) if el == nil { - return getZero[V](), time.Time{}, false + return lo.Empty[V](), time.Time{}, false } return el.value, time.Unix(el.expires, 0), true @@ -259,8 +261,3 @@ type entry[K comparable, V any] struct { value V expires int64 } - -func getZero[T any]() T { - var result T - return result -} diff --git a/common/picker/picker_test.go b/common/picker/picker_test.go index 17b823cb..4c1c9ebe 100644 --- a/common/picker/picker_test.go +++ b/common/picker/picker_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/samber/lo" "github.com/stretchr/testify/assert" ) @@ -15,7 +16,7 @@ func sleepAndSend[T any](ctx context.Context, delay int, input T) func() (T, err case <-timer.C: return input, nil case <-ctx.Done(): - return getZero[T](), ctx.Err() + return lo.Empty[T](), ctx.Err() } } } @@ -35,11 +36,6 @@ func TestPicker_Timeout(t *testing.T) { picker.Go(sleepAndSend(ctx, 20, 1)) number := picker.Wait() - assert.Equal(t, number, getZero[int]()) + assert.Equal(t, number, lo.Empty[int]()) assert.NotNil(t, picker.Error()) } - -func getZero[T any]() T { - var result T - return result -} diff --git a/common/queue/queue.go b/common/queue/queue.go index 4755cb35..cb58e2f5 100644 --- a/common/queue/queue.go +++ b/common/queue/queue.go @@ -2,6 +2,8 @@ package queue import ( "sync" + + "github.com/samber/lo" ) // Queue is a simple concurrent safe queue @@ -24,7 +26,7 @@ func (q *Queue[T]) Put(items ...T) { // Pop returns the head of items. func (q *Queue[T]) Pop() T { if len(q.items) == 0 { - return GetZero[T]() + return lo.Empty[T]() } q.lock.Lock() @@ -37,7 +39,7 @@ func (q *Queue[T]) Pop() T { // Last returns the last of item. func (q *Queue[T]) Last() T { if len(q.items) == 0 { - return GetZero[T]() + return lo.Empty[T]() } q.lock.RLock() @@ -69,8 +71,3 @@ func New[T any](hint int64) *Queue[T] { items: make([]T, 0, hint), } } - -func GetZero[T any]() T { - var result T - return result -} diff --git a/component/resource/fetcher.go b/component/resource/fetcher.go index 4b905c7f..c92687b1 100644 --- a/component/resource/fetcher.go +++ b/component/resource/fetcher.go @@ -9,6 +9,8 @@ import ( types "github.com/Dreamacro/clash/constant/provider" "github.com/Dreamacro/clash/log" + + "github.com/samber/lo" ) var ( @@ -65,7 +67,7 @@ func (f *Fetcher[V]) Initial() (V, error) { } if err != nil { - return getZero[V](), err + return lo.Empty[V](), err } var contents V @@ -85,18 +87,18 @@ func (f *Fetcher[V]) Initial() (V, error) { if err != nil { if !isLocal { - return getZero[V](), err + return lo.Empty[V](), err } // parse local file error, fallback to remote buf, err = f.vehicle.Read() if err != nil { - return getZero[V](), err + return lo.Empty[V](), err } contents, err = f.parser(buf) if err != nil { - return getZero[V](), err + return lo.Empty[V](), err } isLocal = false @@ -104,7 +106,7 @@ func (f *Fetcher[V]) Initial() (V, error) { if f.vehicle.Type() != types.File && !isLocal { if err := safeWrite(f.vehicle.Path(), buf); err != nil { - return getZero[V](), err + return lo.Empty[V](), err } } @@ -121,7 +123,7 @@ func (f *Fetcher[V]) Initial() (V, error) { func (f *Fetcher[V]) Update() (V, bool, error) { buf, err := f.vehicle.Read() if err != nil { - return getZero[V](), false, err + return lo.Empty[V](), false, err } now := time.Now() @@ -129,17 +131,17 @@ func (f *Fetcher[V]) Update() (V, bool, error) { if bytes.Equal(f.hash[:], hash[:]) { f.UpdatedAt = &now _ = os.Chtimes(f.vehicle.Path(), now, now) - return getZero[V](), true, nil + return lo.Empty[V](), true, nil } contents, err := f.parser(buf) if err != nil { - return getZero[V](), false, err + return lo.Empty[V](), false, err } if f.vehicle.Type() != types.File { if err := safeWrite(f.vehicle.Path(), buf); err != nil { - return getZero[V](), false, err + return lo.Empty[V](), false, err } } @@ -210,8 +212,3 @@ func NewFetcher[V any](name string, interval time.Duration, vehicle types.Vehicl interval: interval, } } - -func getZero[V any]() V { - var result V - return result -}