From 2bc3d38b833d3dc67a72f80bb4f8c8948b616277 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Sun, 8 Dec 2024 20:21:35 +0800 Subject: [PATCH] chore: use cmp (#568) --- pkg/utils/utils.go | 37 ---------------------------- service/singleton/crontask.go | 4 +-- service/singleton/ddns.go | 4 +-- service/singleton/nat.go | 4 +-- service/singleton/notification.go | 4 +-- service/singleton/servicesentinel.go | 4 +-- 6 files changed, 10 insertions(+), 47 deletions(-) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 7300c69..1ec9985 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -145,40 +145,3 @@ func Itoa[T constraints.Integer](i T) string { return "" } } - -// From go1.23 - -// Compare returns -// -// -1 if x is less than y, -// 0 if x equals y, -// +1 if x is greater than y. -// -// For floating-point types, a NaN is considered less than any non-NaN, -// a NaN is considered equal to a NaN, and -0.0 is equal to 0.0. -func Compare[T constraints.Ordered](x, y T) int { - xNaN := isNaN(x) - yNaN := isNaN(y) - if xNaN { - if yNaN { - return 0 - } - return -1 - } - if yNaN { - return +1 - } - if x < y { - return -1 - } - if x > y { - return +1 - } - return 0 -} - -// isNaN reports whether x is a NaN without requiring the math package. -// This will always return false if T is not floating-point. -func isNaN[T constraints.Ordered](x T) bool { - return x != x -} diff --git a/service/singleton/crontask.go b/service/singleton/crontask.go index ccfb95d..0005ef7 100644 --- a/service/singleton/crontask.go +++ b/service/singleton/crontask.go @@ -1,6 +1,7 @@ package singleton import ( + "cmp" "fmt" "slices" "strings" @@ -11,7 +12,6 @@ import ( "github.com/robfig/cron/v3" "github.com/nezhahq/nezha/model" - "github.com/nezhahq/nezha/pkg/utils" pb "github.com/nezhahq/nezha/proto" ) @@ -84,7 +84,7 @@ func UpdateCronList() { CronList = append(CronList, c) } slices.SortFunc(CronList, func(a, b *model.Cron) int { - return utils.Compare(a.ID, b.ID) + return cmp.Compare(a.ID, b.ID) }) } diff --git a/service/singleton/ddns.go b/service/singleton/ddns.go index e2799fb..7f35dab 100644 --- a/service/singleton/ddns.go +++ b/service/singleton/ddns.go @@ -1,6 +1,7 @@ package singleton import ( + "cmp" "fmt" "slices" "sync" @@ -12,7 +13,6 @@ import ( ddns2 "github.com/nezhahq/nezha/pkg/ddns" "github.com/nezhahq/nezha/pkg/ddns/dummy" "github.com/nezhahq/nezha/pkg/ddns/webhook" - "github.com/nezhahq/nezha/pkg/utils" ) var ( @@ -61,7 +61,7 @@ func UpdateDDNSList() { DDNSList = append(DDNSList, p) } slices.SortFunc(DDNSList, func(a, b *model.DDNSProfile) int { - return utils.Compare(a.ID, b.ID) + return cmp.Compare(a.ID, b.ID) }) } diff --git a/service/singleton/nat.go b/service/singleton/nat.go index f03bc04..7ac2897 100644 --- a/service/singleton/nat.go +++ b/service/singleton/nat.go @@ -1,11 +1,11 @@ package singleton import ( + "cmp" "slices" "sync" "github.com/nezhahq/nezha/model" - "github.com/nezhahq/nezha/pkg/utils" ) var ( @@ -64,7 +64,7 @@ func UpdateNATList() { NATList = append(NATList, n) } slices.SortFunc(NATList, func(a, b *model.NAT) int { - return utils.Compare(a.ID, b.ID) + return cmp.Compare(a.ID, b.ID) }) } diff --git a/service/singleton/notification.go b/service/singleton/notification.go index 3b26bbc..498f000 100644 --- a/service/singleton/notification.go +++ b/service/singleton/notification.go @@ -1,6 +1,7 @@ package singleton import ( + "cmp" "fmt" "log" "slices" @@ -8,7 +9,6 @@ import ( "time" "github.com/nezhahq/nezha/model" - "github.com/nezhahq/nezha/pkg/utils" ) const ( @@ -90,7 +90,7 @@ func UpdateNotificationList() { NotificationListSorted = append(NotificationListSorted, n) } slices.SortFunc(NotificationListSorted, func(a, b *model.Notification) int { - return utils.Compare(a.ID, b.ID) + return cmp.Compare(a.ID, b.ID) }) } diff --git a/service/singleton/servicesentinel.go b/service/singleton/servicesentinel.go index 4638000..2ef5c56 100644 --- a/service/singleton/servicesentinel.go +++ b/service/singleton/servicesentinel.go @@ -1,6 +1,7 @@ package singleton import ( + "cmp" "fmt" "log" "slices" @@ -10,7 +11,6 @@ import ( "github.com/jinzhu/copier" "github.com/nezhahq/nezha/model" - "github.com/nezhahq/nezha/pkg/utils" pb "github.com/nezhahq/nezha/proto" ) @@ -180,7 +180,7 @@ func (ss *ServiceSentinel) UpdateServiceList() { } slices.SortFunc(ss.ServiceList, func(a, b *model.Service) int { - return utils.Compare(a.ID, b.ID) + return cmp.Compare(a.ID, b.ID) }) }