mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
@@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"iter"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -71,35 +72,63 @@ func FindByUserID[S ~[]E, E CommonInterface](s S, uid uint64) []uint64 {
|
||||
}
|
||||
|
||||
func SearchByIDCtx[S ~[]E, E CommonInterface](c *gin.Context, x S) S {
|
||||
switch any(x).(type) {
|
||||
case []*Server:
|
||||
l := searchByIDCtxServer(c, any(x).([]*Server))
|
||||
return any(l).(S)
|
||||
default:
|
||||
var s S
|
||||
for idStr := range strings.SplitSeq(c.Query("id"), ",") {
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
s = appendBinarySearch(s, x, id)
|
||||
}
|
||||
return utils.IfOr(len(s) > 0, s, x)
|
||||
}
|
||||
return SearchByID(strings.SplitSeq(c.Query("id"), ","), x)
|
||||
}
|
||||
|
||||
func searchByIDCtxServer(c *gin.Context, x []*Server) []*Server {
|
||||
list1, list2 := SplitList(x)
|
||||
func SearchByID[S ~[]E, E CommonInterface](seq iter.Seq[string], x S) S {
|
||||
if hasPriorityList[E]() {
|
||||
return searchByIDPri(seq, x)
|
||||
}
|
||||
|
||||
var clist1, clist2 []*Server
|
||||
for idStr := range strings.SplitSeq(c.Query("id"), ",") {
|
||||
var s S
|
||||
for idStr := range seq {
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
clist1 = appendBinarySearch(clist1, list1, id)
|
||||
s = appendBinarySearch(s, x, id)
|
||||
}
|
||||
return utils.IfOr(len(s) > 0, s, x)
|
||||
}
|
||||
|
||||
func hasPriorityList[T CommonInterface]() bool {
|
||||
var class T
|
||||
|
||||
switch any(class).(type) {
|
||||
case *Server:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
type splitter[S ~[]E, E CommonInterface] interface {
|
||||
// SplitList should split a sorted list into two separate lists:
|
||||
// The first list contains elements with a priority set (DisplayIndex != 0).
|
||||
// The second list contains elements without a priority set (DisplayIndex == 0).
|
||||
// The original slice is not modified. If no element without a priority is found, it returns nil.
|
||||
// Should be safe to use with a nil pointer.
|
||||
SplitList(x S) (S, S)
|
||||
}
|
||||
|
||||
func searchByIDPri[S ~[]E, E CommonInterface](seq iter.Seq[string], x S) S {
|
||||
var class E
|
||||
split, ok := any(class).(splitter[S, E])
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
plist, list2 := split.SplitList(x)
|
||||
|
||||
var clist1, clist2 S
|
||||
for idStr := range seq {
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
clist1 = appendSearch(clist1, plist, id)
|
||||
clist2 = appendBinarySearch(clist2, list2, id)
|
||||
}
|
||||
|
||||
@@ -115,3 +144,13 @@ func appendBinarySearch[S ~[]E, E CommonInterface](x, y S, target uint64) S {
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func appendSearch[S ~[]E, E CommonInterface](x, y S, target uint64) S {
|
||||
if i := slices.IndexFunc(y, func(e E) bool {
|
||||
return e.GetID() == target
|
||||
}); i != -1 {
|
||||
x = append(x, y[i])
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
49
model/common_test.go
Normal file
49
model/common_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSearchByID(t *testing.T) {
|
||||
t.Run("WithoutPriorityList", func(t *testing.T) {
|
||||
list, exp := []*DDNSProfile{
|
||||
{Common: Common{ID: 1}},
|
||||
{Common: Common{ID: 2}},
|
||||
{Common: Common{ID: 3}},
|
||||
{Common: Common{ID: 4}},
|
||||
{Common: Common{ID: 5}},
|
||||
}, []*DDNSProfile{
|
||||
{Common: Common{ID: 4}},
|
||||
{Common: Common{ID: 1}},
|
||||
{Common: Common{ID: 3}},
|
||||
}
|
||||
|
||||
searchList := slices.Values([]string{"4", "1", "3"})
|
||||
filtered := SearchByID(searchList, list)
|
||||
if !reflect.DeepEqual(filtered, exp) {
|
||||
t.Fatalf("expected %v, but got %v", exp, filtered)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("WithPriorityTest", func(t *testing.T) {
|
||||
list, exp := []*Server{
|
||||
{Common: Common{ID: 5}, DisplayIndex: 2},
|
||||
{Common: Common{ID: 4}, DisplayIndex: 1},
|
||||
{Common: Common{ID: 1}},
|
||||
{Common: Common{ID: 2}},
|
||||
{Common: Common{ID: 3}},
|
||||
}, []*Server{
|
||||
{Common: Common{ID: 4}, DisplayIndex: 1},
|
||||
{Common: Common{ID: 5}, DisplayIndex: 2},
|
||||
{Common: Common{ID: 3}},
|
||||
}
|
||||
|
||||
searchList := slices.Values([]string{"3", "4", "5"})
|
||||
filtered := SearchByID(searchList, list)
|
||||
if !reflect.DeepEqual(filtered, exp) {
|
||||
t.Fatalf("expected %v, but got %v", exp, filtered)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -73,11 +73,7 @@ func (s *Server) AfterFind(tx *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Split a sorted server list into two separate lists:
|
||||
// The first list contains servers with a priority set (DisplayIndex != 0).
|
||||
// The second list contains servers without a priority set (DisplayIndex == 0).
|
||||
// The original slice is not modified. If no server without a priority is found, it returns nil.
|
||||
func SplitList(x []*Server) ([]*Server, []*Server) {
|
||||
func (s *Server) SplitList(x []*Server) ([]*Server, []*Server) {
|
||||
pri := func(s *Server) bool {
|
||||
return s.DisplayIndex == 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user