This commit is contained in:
xing 2023-02-09 00:07:53 +08:00
parent 439a49cf12
commit 796ae6f0fa
4 changed files with 47 additions and 4 deletions

View File

@ -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
}

View File

@ -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)
})
}
}

View File

@ -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
}

View File

@ -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)