From 74304b5b122dc91cdc0bff1fca7f338500cd9f61 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 5 Dec 2023 21:39:13 +0800 Subject: [PATCH] fix slice.range,pipe middleware bug, add slice.simpleOrder func --- app/actions/comment.go | 8 +++++++- app/theme/wp/fn.go | 4 ++-- app/theme/wp/pipe.go | 13 +++++++------ cache/reload/reload.go | 4 ++-- helper/number/number.go | 5 ++++- helper/slice/sort.go | 13 +++++++++++++ helper/slice/sort_test.go | 37 +++++++++++++++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 12 deletions(-) diff --git a/app/actions/comment.go b/app/actions/comment.go index 6e9d71e..27be4bf 100644 --- a/app/actions/comment.go +++ b/app/actions/comment.go @@ -11,6 +11,7 @@ import ( "github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/wpconfig" "github.com/fthvgb1/wp-go/helper/slice" + str "github.com/fthvgb1/wp-go/helper/strings" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "io" @@ -110,6 +111,9 @@ func PostComment(c *gin.Context) { } cc := c.Copy() go func() { + if gin.Mode() != gin.ReleaseMode { + return + } id := comment.CommentPostId if id <= 0 { logs.Error(errors.New("获取文档id错误"), "", comment.CommentPostId) @@ -131,7 +135,9 @@ func PostComment(c *gin.Context) { return } cache.NewCommentCache().Set(c, up.RawQuery, string(s)) - c.Redirect(http.StatusFound, res.Header.Get("Location")) + uu, _ := url.Parse(res.Header.Get("Location")) + uuu := str.Join(uu.Path, "?", uu.RawQuery) + c.Redirect(http.StatusFound, uuu) return } var r io.Reader diff --git a/app/theme/wp/fn.go b/app/theme/wp/fn.go index 4ced8e1..862fe99 100644 --- a/app/theme/wp/fn.go +++ b/app/theme/wp/fn.go @@ -5,8 +5,8 @@ import ( "github.com/fthvgb1/wp-go/safety" ) -var fnMap = safety.NewMap[string, map[string]any]() //map[string]map[string]any -var fnHook = safety.NewMap[string, map[string]any]() // map[string]map[string]any +var fnMap = safety.NewMap[string, map[string]any]() +var fnHook = safety.NewMap[string, map[string]any]() func GetFn[T any](fnType string, name string) []T { v, ok := fnMap.Load(fnType) diff --git a/app/theme/wp/pipe.go b/app/theme/wp/pipe.go index c8df26e..be5b0fa 100644 --- a/app/theme/wp/pipe.go +++ b/app/theme/wp/pipe.go @@ -81,8 +81,8 @@ func BuildPipe(pipeScene string, keyFn func(*Handle, string) string, fn func(*Ha } return call, ok }) - slice.Sort(calls, func(i, j HandleCall) bool { - return i.Order > j.Order + slice.SimpleSort(calls, slice.DESC, func(t HandleCall) float64 { + return t.Order }) return calls, true }) @@ -122,9 +122,10 @@ func Run(h *Handle, conf func(*Handle)) { } return pipe, pipe.Fn != nil }) - slice.Sort(pipes, func(i, j Pipe) bool { - return i.Order > j.Order + slice.SimpleSort(pipes, slice.DESC, func(t Pipe) float64 { + return t.Order }) + arr := slice.Map(pipes, func(t Pipe) HandlePipeFn[*Handle] { return t.Fn }) @@ -133,7 +134,7 @@ func Run(h *Handle, conf func(*Handle)) { } func MiddlewareKey(h *Handle, pipScene string) string { - return h.DoActionFilter("middleware", "middleware", pipScene) + return h.DoActionFilter("middleware", str.Join("pipe-middleware-", h.scene), pipScene) } func PipeMiddlewareHandle(h *Handle, middlewares map[string][]HandleCall, key string) (handlers []HandleCall) { @@ -193,7 +194,7 @@ func (h *Handle) PipeHandleHook(name string, calls []HandleCall, m map[string][] } func InitPipe(h *Handle) { - h.PushPipe(constraints.Home, NewPipe(constraints.PipeMiddleware, 300, + h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeMiddleware, 300, BuildPipe(constraints.PipeMiddleware, MiddlewareKey, PipeMiddlewareHandle))) h.PushPipe(constraints.AllScene, NewPipe(constraints.PipeData, 200, diff --git a/cache/reload/reload.go b/cache/reload/reload.go index 182c9c6..f92559e 100644 --- a/cache/reload/reload.go +++ b/cache/reload/reload.go @@ -264,8 +264,8 @@ func Reload() { callsM.Flush() flushMapFn.Flush() callll := calls.Load() - slice.Sort(callll, func(i, j queue) bool { - return i.order > j.order + slice.SimpleSort(callll, slice.DESC, func(t queue) float64 { + return t.order }) for _, call := range callll { call.fn() diff --git a/helper/number/number.go b/helper/number/number.go index 011b214..de5c379 100644 --- a/helper/number/number.go +++ b/helper/number/number.go @@ -19,7 +19,10 @@ func Range[T constraints.Integer](start, end T, steps ...T) []T { if step == 0 { l = int(end - start + 1) } else { - l = int((end-start+1)/step + 1) + l = int((end - start + 1) / step) + if step*T(l) <= end && step != 1 { + l++ + } } if l < 0 { l = -l diff --git a/helper/slice/sort.go b/helper/slice/sort.go index d9af2ca..9efbec6 100644 --- a/helper/slice/sort.go +++ b/helper/slice/sort.go @@ -56,3 +56,16 @@ func Sorts[T constraints.Ordered](a []T, order int) { } sort.Sort(slice) } + +func SimpleSort[T any, O constraints.Ordered](a []T, order int, fn func(t T) O) { + slice := anyArr[T]{ + data: a, + fn: func(i, j T) bool { + if order == DESC { + return fn(i) > fn(j) + } + return fn(i) < fn(j) + }, + } + sort.Sort(slice) +} diff --git a/helper/slice/sort_test.go b/helper/slice/sort_test.go index 5ebfcc4..a8805be 100644 --- a/helper/slice/sort_test.go +++ b/helper/slice/sort_test.go @@ -2,6 +2,7 @@ package slice import ( "fmt" + "github.com/fthvgb1/wp-go/helper/number" "golang.org/x/exp/constraints" "reflect" "testing" @@ -163,3 +164,39 @@ func TestStableSort(t *testing.T) { }) } } + +func TestSimpleSort(t *testing.T) { + + type args[T any, O constraints.Ordered] struct { + a []T + order int + fn func(t T) O + } + type testCase[T any, O constraints.Ordered] struct { + name string + args args[T, O] + } + tests := []testCase[int, int]{ + { + name: "t1", + args: args[int, int]{ + a: func() []int { + f := number.Range(1, 20) + Shuffle(&f) + fmt.Println(f) + return f + }(), + order: ASC, + fn: func(t int) int { + return t + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SimpleSort(tt.args.a, tt.args.order, tt.args.fn) + fmt.Println(tt.args.a) + }) + } +}