优化
This commit is contained in:
parent
439a49cf12
commit
796ae6f0fa
|
@ -57,3 +57,9 @@ func Delete[T any](a *[]T, index int) {
|
|||
arr := *a
|
||||
*a = append(arr[:index], arr[index+1:]...)
|
||||
}
|
||||
|
||||
func Copy[T any](a []T) []T {
|
||||
dst := make([]T, len(a))
|
||||
copy(dst, a)
|
||||
return dst
|
||||
}
|
||||
|
|
|
@ -134,3 +134,34 @@ func TestDelete(t *testing.T) {
|
|||
fmt.Println(a)
|
||||
fmt.Println(b)
|
||||
}
|
||||
|
||||
func TestCopy(t *testing.T) {
|
||||
type args[T int] struct {
|
||||
a []T
|
||||
}
|
||||
type testCase[T int] struct {
|
||||
name string
|
||||
args args[T]
|
||||
want []T
|
||||
}
|
||||
tests := []testCase[int]{
|
||||
{
|
||||
name: "t1",
|
||||
args: args[int]{
|
||||
a: number.Range(1, 10, 1),
|
||||
},
|
||||
want: number.Range(1, 10, 1),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := Copy(tt.args.a)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Copy() = %v, want %v", got, tt.want)
|
||||
}
|
||||
got[9] = 111
|
||||
fmt.Println(tt.args.a)
|
||||
fmt.Println(got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,14 @@ func PluginFn[T any](a []Plugin[T], h Handle, fn Fn[T]) Fn[T] {
|
|||
}, fn)
|
||||
}
|
||||
|
||||
var plugin = []Plugin[models.Posts]{
|
||||
PasswordProject, Digest,
|
||||
}
|
||||
|
||||
func Plugins() []Plugin[models.Posts] {
|
||||
return plugin
|
||||
}
|
||||
|
||||
func Default[T any](t T) T {
|
||||
return t
|
||||
}
|
||||
|
|
|
@ -48,14 +48,12 @@ func Hook(h2 common.Handle) {
|
|||
h.index()
|
||||
}
|
||||
|
||||
var plugin = []common.Plugin[models.Posts]{
|
||||
common.PasswordProject, common.Digest,
|
||||
}
|
||||
var plugin = slice.Copy(common.Plugins())
|
||||
|
||||
func (h handle) index() {
|
||||
if h.Stats != plugins.Empty404 {
|
||||
posts := h.GinH["posts"].([]models.Posts)
|
||||
posts = slice.Map(posts, common.PluginFn(plugin, h.Handle, common.Default[models.Posts]))
|
||||
posts = slice.Map(posts, common.PluginFn[models.Posts](plugin, h.Handle, common.Default[models.Posts]))
|
||||
p, ok := h.GinH["pagination"]
|
||||
if ok {
|
||||
pp, ok := p.(pagination.ParsePagination)
|
||||
|
|
Loading…
Reference in New Issue
Block a user