Skip to content

Commit cf4612c

Browse files
committed
refactor: 采用 slices.Clone 代替自有的实现
1 parent f12fa3e commit cf4612c

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/issue9/mux/v8
22

33
require (
4-
github.com/issue9/assert/v4 v4.1.1
4+
github.com/issue9/assert/v4 v4.2.0
55
github.com/issue9/errwrap v0.3.2
66
github.com/issue9/source v0.9.1
77
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/issue9/assert/v4 v4.1.1 h1:OhPE8SB8n/qZCNGLQa+6MQtr/B3oON0JAVj68k8jJlc=
2-
github.com/issue9/assert/v4 v4.1.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
1+
github.com/issue9/assert/v4 v4.2.0 h1:XJGMFYW0xfESqFRPLWbSsr0xWdkofytvQbDfNb5n9fw=
2+
github.com/issue9/assert/v4 v4.2.0/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
33
github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw=
44
github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A=
55
github.com/issue9/source v0.9.1 h1:duANl/q03NzDtZN5sOPia+s4vBu8c1CqtHGoluY0Tto=

group/group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (g *GroupOf[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7070

7171
// New 声明新路由
7272
//
73-
// 新路由会继承 [NewOf] 中指定的参数,其中的 o 可以覆盖由 [NewOf] 中的相关参数
73+
// 新路由会继承 [NewOf] 中指定的参数,其中的 o 可以覆盖由 [NewOf] 中指定的相关参数
7474
func (g *GroupOf[T]) New(name string, matcher Matcher, o ...mux.Option) *mux.RouterOf[T] {
7575
o = g.mergeOption(o...)
7676
r := mux.NewRouterOf(name, g.call, g.notFound, g.methodNotAllowedBuilder, g.optionsBuilder, o...)

router.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package mux
66

77
import (
88
"net/http"
9+
"slices"
910
"strconv"
1011

1112
"github.com/issue9/errwrap"
@@ -271,6 +272,7 @@ func (p *PrefixOf[T]) URL(strict bool, pattern string, params map[string]string)
271272
//
272273
// m 中间件函数,按顺序调用,会继承 p 的中间件并按在 m 之前;
273274
func (p *PrefixOf[T]) Prefix(prefix string, m ...types.MiddlewareOf[T]) *PrefixOf[T] {
275+
// TODO(go1.22): slices.Concat(p.middleware, m)
274276
ms := make([]types.MiddlewareOf[T], 0, len(p.middleware)+len(m))
275277
ms = append(ms, p.middleware...)
276278
ms = append(ms, m...)
@@ -282,9 +284,7 @@ func (p *PrefixOf[T]) Prefix(prefix string, m ...types.MiddlewareOf[T]) *PrefixO
282284
// prefix 路由前缀字符串,可以为空;
283285
// m 中间件函数,按顺序调用,会继承 r 的中间件并按在 m 之前;
284286
func (r *RouterOf[T]) Prefix(prefix string, m ...types.MiddlewareOf[T]) *PrefixOf[T] {
285-
ms := make([]types.MiddlewareOf[T], 0, len(m))
286-
ms = append(ms, m...)
287-
return &PrefixOf[T]{router: r, prefix: prefix, middleware: ms}
287+
return &PrefixOf[T]{router: r, prefix: prefix, middleware: slices.Clone(m)}
288288
}
289289

290290
// Router 返回与当前关联的 *RouterOf 实例
@@ -332,16 +332,15 @@ func (r *ResourceOf[T]) URL(strict bool, params map[string]string) (string, erro
332332
// pattern 资源地址;
333333
// m 中间件函数,按顺序调用,会继承 r 的中间件并按在 m 之前;
334334
func (r *RouterOf[T]) Resource(pattern string, m ...types.MiddlewareOf[T]) *ResourceOf[T] {
335-
ms := make([]types.MiddlewareOf[T], 0, len(m))
336-
ms = append(ms, m...)
337-
return &ResourceOf[T]{router: r, pattern: pattern, middleware: ms}
335+
return &ResourceOf[T]{router: r, pattern: pattern, middleware: slices.Clone(m)}
338336
}
339337

340338
// Resource 创建一个资源路由项
341339
//
342340
// pattern 资源地址;
343341
// m 中间件函数,按顺序调用,会继承 p 的中间件并按在 m 之前;
344342
func (p *PrefixOf[T]) Resource(pattern string, m ...types.MiddlewareOf[T]) *ResourceOf[T] {
343+
// TODO(go1.22): slices.Concat(p.middleware, m)
345344
ms := make([]types.MiddlewareOf[T], 0, len(p.middleware)+len(m))
346345
ms = append(ms, p.middleware...)
347346
ms = append(ms, m...)

0 commit comments

Comments
 (0)