优化完善代码

This commit is contained in:
xing 2023-03-02 20:36:58 +08:00
parent 5ce707e427
commit 0d62ebd5a5
7 changed files with 68 additions and 89 deletions

View File

@ -27,7 +27,8 @@ func (r anyArr[T]) Less(i, j int) bool {
return r.fn(r.data[i], r.data[j]) return r.fn(r.data[i], r.data[j])
} }
func SortSelf[T any](arr []T, fn func(i, j T) bool) { // Sort fn 中i>j 为降序,反之为升序
func Sort[T any](arr []T, fn func(i, j T) bool) {
slice := anyArr[T]{ slice := anyArr[T]{
data: arr, data: arr,
fn: fn, fn: fn,
@ -36,18 +37,6 @@ func SortSelf[T any](arr []T, fn func(i, j T) bool) {
return return
} }
// Sort fn 中i>j 为降序,反之为升序
func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) {
r = make([]T, len(arr))
copy(r, arr)
slice := anyArr[T]{
data: r,
fn: fn,
}
sort.Sort(slice)
return
}
func Sorts[T constraints.Ordered](a []T, order int) { func Sorts[T constraints.Ordered](a []T, order int) {
slice := anyArr[T]{ slice := anyArr[T]{
data: a, data: a,

View File

@ -7,59 +7,6 @@ import (
"testing" "testing"
) )
func TestSortSelf(t *testing.T) {
type xy struct {
x int
y int
}
type args struct {
arr []xy
fn func(i, j xy) bool
}
tests := []struct {
name string
args args
wantR []xy
}{
{
name: "t1",
args: args{
arr: []xy{
{1, 2},
{3, 4},
{1, 3},
{2, 1},
{1, 6},
},
fn: func(i, j xy) bool {
if i.x < j.x {
return true
}
if i.x == j.x && i.y > i.y {
return true
}
return false
},
},
wantR: []xy{
{1, 2},
{1, 3},
{1, 6},
{2, 1},
{3, 4},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if SortSelf(tt.args.arr, tt.args.fn); !reflect.DeepEqual(tt.args.arr, tt.wantR) {
t.Errorf("SimpleSort() = %v, want %v", tt.args.arr, tt.wantR)
}
})
}
}
func TestSort(t *testing.T) { func TestSort(t *testing.T) {
type xy struct { type xy struct {
x int x int
@ -103,11 +50,21 @@ func TestSort(t *testing.T) {
{3, 4}, {3, 4},
}, },
}, },
{
name: "t1",
args: args[xy]{
arr: []xy{{1, 2}, {1, 3}, {1, 6}},
fn: func(i, j xy) bool {
return i.x > j.x
},
},
wantR: []xy{{1, 2}, {1, 3}, {1, 6}},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if gotR := Sort[xy](tt.args.arr, tt.args.fn); !reflect.DeepEqual(gotR, tt.wantR) { if Sort[xy](tt.args.arr, tt.args.fn); !reflect.DeepEqual(tt.args.arr, tt.wantR) {
t.Errorf("SimpleSortR() = %v, want %v", gotR, tt.wantR) t.Errorf("SimpleSortR() = %v, want %v", tt.args.arr, tt.wantR)
} }
}) })
} }

View File

@ -55,7 +55,7 @@ func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html st
if d.depth > d.maxDepth { if d.depth > d.maxDepth {
comments = d.findComments(comments) comments = d.findComments(comments)
} }
slice.SortSelf(comments, d.i.Sort) slice.Sort(comments, d.i.Sort)
for i, comment := range comments { for i, comment := range comments {
eo := "even" eo := "even"
if (i+1)%2 == 0 { if (i+1)%2 == 0 {

View File

@ -9,6 +9,7 @@ import (
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
@ -47,10 +48,12 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 15)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 15))
h.PushHeadScript( errHandle := wp.NewHandleFn(errorsHandle, 100)
wp.NewComponents(colorScheme, 10), h.PushHandleFn(constraints.Error404, errHandle)
wp.NewComponents(customHeader, 10), h.PushHandleFn(constraints.ParamError, errHandle)
) h.PushHandleFn(constraints.InternalErr, errHandle)
h.PushGroupHeadScript(10, colorScheme, customHeader)
if "dark" == wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") { if "dark" == wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") {
h.PushHeadScript(wp.NewComponents(func(h *wp.Handle) string { h.PushHeadScript(wp.NewComponents(func(h *wp.Handle) string {
return ` <link rel="stylesheet" id="twentyseventeen-colors-dark-css" href="/wp-content/themes/twentyseventeen/assets/css/colors-dark.css?ver=20191025" media="all">` return ` <link rel="stylesheet" id="twentyseventeen-colors-dark-css" href="/wp-content/themes/twentyseventeen/assets/css/colors-dark.css?ver=20191025" media="all">`
@ -76,12 +79,18 @@ var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
}) })
}() }()
func errorsHandle(h *wp.Handle) {
h.SetTempl("twentyseventeen/posts/error.gohtml")
switch h.Stats {
case constraints.Error404, constraints.InternalErr:
logs.ErrPrintln(h.Err(), "报错:")
}
}
func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) { func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) {
err := i.BuildIndexData(wp.NewIndexParams(i.C)) err := i.BuildIndexData(wp.NewIndexParams(i.C))
if err != nil { if err != nil {
i.SetTempl(str.Join(ThemeName, "/posts/error.gohtml")) i.SetErr(err)
i.Render()
return
} }
i.SetPageEle(paginate) i.SetPageEle(paginate)
i.SetPostsPlugins(listPostsPlugins) i.SetPostsPlugins(listPostsPlugins)
@ -91,9 +100,7 @@ func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) {
func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) { func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
err := d.BuildDetailData() err := d.BuildDetailData()
if err != nil { if err != nil {
d.SetTempl(str.Join(ThemeName, "/posts/error.gohtml")) d.SetErr(err)
d.Render()
return
} }
if d.Post.Thumbnail.Path != "" { if d.Post.Thumbnail.Path != "" {
img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "full", "", "thumbnail", "post-thumbnail") img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "full", "", "thumbnail", "post-thumbnail")

View File

@ -41,7 +41,6 @@ func (d *DetailHandle) CheckAndGetPost() (err error) {
if id > maxId || id <= 0 { if id > maxId || id <= 0 {
d.Stats = constraints.ParamError d.Stats = constraints.ParamError
err = errors.New("无效的文档id") err = errors.New("无效的文档id")
d.class = append(d.class, "error404")
} }
if err != nil { if err != nil {
return return

View File

@ -108,8 +108,16 @@ func NewComponents(fn func(*Handle) string, order int) Components {
return Components{Fn: fn, Order: order} return Components{Fn: fn, Order: order}
} }
func (h *Handle) PushHandleFn(stats int, fns ...HandleCall) { func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) {
h.handleFns[stats] = append(h.handleFns[stats], fns...) h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], fns...)
}
func (h *Handle) PushGroupHandleFn(statsOrScene, order int, fns ...HandleFn[*Handle]) {
var calls []HandleCall
for _, fn := range fns {
calls = append(calls, HandleCall{fn, order})
}
h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], calls...)
} }
func (h *Handle) AddComponent(name string, fn func(*Handle) string) { func (h *Handle) AddComponent(name string, fn func(*Handle) string) {
@ -122,10 +130,17 @@ func (h *Handle) AddComponent(name string, fn func(*Handle) string) {
} }
func (h *Handle) PushHeadScript(fn ...Components) { func (h *Handle) PushHeadScript(fn ...Components) {
h.components[constraints.HeadScript] = append(h.components[constraints.HeadScript], fn...) h.PushComponents(constraints.HeadScript, fn...)
}
func (h *Handle) PushGroupHeadScript(order int, fns ...func(*Handle) string) {
h.PushGroupComponents(constraints.HeadScript, order, fns...)
} }
func (h *Handle) PushFooterScript(fn ...Components) { func (h *Handle) PushFooterScript(fn ...Components) {
h.components[constraints.FooterScript] = append(h.components[constraints.FooterScript], fn...) h.PushComponents(constraints.FooterScript, fn...)
}
func (h *Handle) PushGroupFooterScript(order int, fns ...func(*Handle) string) {
h.PushGroupComponents(constraints.FooterScript, order, fns...)
} }
func (h *Handle) GetPassword() { func (h *Handle) GetPassword() {
@ -141,11 +156,15 @@ func (h *Handle) ExecHandleFns() {
if ok { if ok {
fns = append(fns, calls...) fns = append(fns, calls...)
} }
call, ok := h.handleFns[constraints.AllStats] calls, ok = h.handleFns[h.scene]
if ok { if ok {
fns = append(fns, call...) fns = append(fns, calls...)
} }
slice.SortSelf(fns, func(i, j HandleCall) bool { calls, ok = h.handleFns[constraints.AllStats]
if ok {
fns = append(fns, calls...)
}
slice.Sort(fns, func(i, j HandleCall) bool {
return i.Order > j.Order return i.Order > j.Order
}) })
for _, fn := range fns { for _, fn := range fns {
@ -200,11 +219,19 @@ func (h *Handle) PushComponents(name string, components ...Components) {
h.components[name] = append(h.components[name], components...) h.components[name] = append(h.components[name], components...)
} }
func (h *Handle) PushGroupComponents(name string, order int, fns ...func(*Handle) string) {
var calls []Components
for _, fn := range fns {
calls = append(calls, Components{fn, order})
}
h.components[name] = append(h.components[name], calls...)
}
func (h *Handle) CalMultipleComponents() { func (h *Handle) CalMultipleComponents() {
for k, ss := range h.components { for k, ss := range h.components {
v, ok := reload.GetStr(k) v, ok := reload.GetStr(k)
if !ok { if !ok {
slice.SortSelf(ss, func(i, j Components) bool { slice.Sort(ss, func(i, j Components) bool {
return i.Order > j.Order return i.Order > j.Order
}) })
v = strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) { v = strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {

View File

@ -104,7 +104,7 @@ func (r Stream[T]) Reduce(fn func(v, r T) T, init T) T {
} }
func (r Stream[T]) Sort(fn func(i, j T) bool) Stream[T] { func (r Stream[T]) Sort(fn func(i, j T) bool) Stream[T] {
slice.SortSelf(r.arr, fn) slice.Sort(r.arr, fn)
return r return r
} }