diff --git a/helper/slice/slices.go b/helper/slice/slices.go index 8fb1af0..ab07083 100644 --- a/helper/slice/slices.go +++ b/helper/slice/slices.go @@ -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 +} diff --git a/helper/slice/slices_test.go b/helper/slice/slices_test.go index cdc150a..0d2cfaa 100644 --- a/helper/slice/slices_test.go +++ b/helper/slice/slices_test.go @@ -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) + }) + } +} diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index ba85789..9e04bf4 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -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 } diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 61b11d6..eebd496 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -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)