This commit is contained in:
xing 2022-10-10 21:33:41 +08:00
parent 90120056d4
commit a24352d043
4 changed files with 46 additions and 13 deletions

View File

@ -58,7 +58,7 @@ func getCommentByIds(args ...any) (map[uint64]models.WpComments, error) {
if err != nil { if err != nil {
return m, err return m, err
} }
return helper.SliceToMap(r, func(t models.WpComments) uint64 { return helper.SimpleSliceToMap(r, func(t models.WpComments) uint64 {
return t.CommentId return t.CommentId
}, true), err }), err
} }

View File

@ -231,16 +231,22 @@ func SliceSelfReverse[T any](arr []T) []T {
return arr return arr
} }
func SliceToMap[K comparable, V any](arr []V, fn func(V) K, isCoverPrev bool) map[K]V { func SimpleSliceToMap[K comparable, V any](arr []V, fn func(V) K) map[K]V {
m := make(map[K]V) return SliceToMap(arr, func(v V) (K, V) {
return fn(v), v
}, true)
}
func SliceToMap[K comparable, V, T any](arr []V, fn func(V) (K, T), isCoverPrev bool) map[K]T {
m := make(map[K]T)
for _, v := range arr { for _, v := range arr {
k := fn(v) k, r := fn(v)
if !isCoverPrev { if !isCoverPrev {
if _, ok := m[k]; ok { if _, ok := m[k]; ok {
continue continue
} }
} }
m[k] = v m[k] = r
} }
return m return m
} }

View File

@ -513,7 +513,7 @@ func TestSliceToMap(t *testing.T) {
} }
type args struct { type args struct {
arr []ss arr []ss
fn func(ss) int fn func(ss) (int, ss)
isCoverPrev bool isCoverPrev bool
} }
tests := []struct { tests := []struct {
@ -525,8 +525,8 @@ func TestSliceToMap(t *testing.T) {
name: "t1", name: "t1",
args: args{ args: args{
arr: []ss{{1, "k1"}, {2, "v2"}, {2, "v3"}}, arr: []ss{{1, "k1"}, {2, "v2"}, {2, "v3"}},
fn: func(s ss) int { fn: func(s ss) (int, ss) {
return s.id return s.id, s
}, },
isCoverPrev: true, isCoverPrev: true,
}, },
@ -535,8 +535,8 @@ func TestSliceToMap(t *testing.T) {
name: "t2", name: "t2",
args: args{ args: args{
arr: []ss{{1, "k1"}, {2, "v2"}, {2, "v3"}}, arr: []ss{{1, "k1"}, {2, "v2"}, {2, "v3"}},
fn: func(s ss) int { fn: func(s ss) (int, ss) {
return s.id return s.id, s
}, },
isCoverPrev: false, isCoverPrev: false,
}, },
@ -551,3 +551,30 @@ func TestSliceToMap(t *testing.T) {
}) })
} }
} }
func TestSimpleSliceToMap(t *testing.T) {
type args struct {
arr []int
fn func(int) int
}
tests := []struct {
name string
args args
want map[int]int
}{
{
name: "t1",
args: args{arr: []int{1, 2, 3}, fn: func(i int) int {
return i
}},
want: map[int]int{1: 1, 2: 2, 3: 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SimpleSliceToMap(tt.args.arr, tt.args.fn); !reflect.DeepEqual(got, tt.want) {
t.Errorf("SimpleSliceToMap() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -9,9 +9,9 @@ import (
) )
func ValidateServerNames() func(ctx *gin.Context) { func ValidateServerNames() func(ctx *gin.Context) {
serverName := helper.SliceToMap(vars.Conf.TrustServerNames, func(v string) string { serverName := helper.SimpleSliceToMap(vars.Conf.TrustServerNames, func(v string) string {
return v return v
}, true) })
return func(c *gin.Context) { return func(c *gin.Context) {
if len(serverName) > 0 { if len(serverName) > 0 {
if _, ok := serverName[strings.Split(c.Request.Host, ":")[0]]; !ok { if _, ok := serverName[strings.Split(c.Request.Host, ":")[0]]; !ok {