fix slice.range,pipe middleware bug, add slice.simpleOrder func

This commit is contained in:
xing 2023-12-05 21:39:13 +08:00
parent ac29cf2448
commit 74304b5b12
7 changed files with 72 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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