Skip to content

Commit

Permalink
chore: use cmp (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb authored Dec 8, 2024
1 parent 96b254a commit 2bc3d38
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 47 deletions.
37 changes: 0 additions & 37 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions service/singleton/crontask.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package singleton

import (
"cmp"
"fmt"
"slices"
"strings"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
})
}

Expand Down
4 changes: 2 additions & 2 deletions service/singleton/ddns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package singleton

import (
"cmp"
"fmt"
"slices"
"sync"
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
})
}

Expand Down
4 changes: 2 additions & 2 deletions service/singleton/nat.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package singleton

import (
"cmp"
"slices"
"sync"

"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/pkg/utils"
)

var (
Expand Down Expand Up @@ -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)
})
}

Expand Down
4 changes: 2 additions & 2 deletions service/singleton/notification.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package singleton

import (
"cmp"
"fmt"
"log"
"slices"
"sync"
"time"

"github.com/nezhahq/nezha/model"
"github.com/nezhahq/nezha/pkg/utils"
)

const (
Expand Down Expand Up @@ -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)
})
}

Expand Down
4 changes: 2 additions & 2 deletions service/singleton/servicesentinel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package singleton

import (
"cmp"
"fmt"
"log"
"slices"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
})
}

Expand Down

0 comments on commit 2bc3d38

Please sign in to comment.