Skip to content

Commit 212197c

Browse files
committed
调整装饰器模式
1 parent 3f86b20 commit 212197c

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

http/router/entry.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ type Entry struct {
1111
}
1212

1313
// AddLabel 添加Label
14-
func (e *Entry) AddLabel(labels ...*Label) {
14+
func (e *Entry) AddLabel(labels ...*Label) EntryDecorator {
1515
for i := range labels {
1616
e.Labels[labels[i].Key()] = labels[i].Value()
1717
}
18+
19+
return e
1820
}
1921

2022
// NewEntrySet 实例

http/router/httprouter/subrouter.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ func (r *subRouter) AddProtected(method, path string, h http.HandlerFunc) router
5656

5757
r.add(e)
5858

59-
new := *r
60-
return &new
59+
return e.Entry
6160
}
6261

6362
func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) router.EntryDecorator {
@@ -74,13 +73,11 @@ func (r *subRouter) AddPublict(method, path string, h http.HandlerFunc) router.E
7473
}
7574
r.add(e)
7675

77-
new := *r
78-
return &new
76+
return e.Entry
7977
}
8078

81-
func (r *subRouter) SetLabel(labels ...*router.Label) router.EntryDecorator {
79+
func (r *subRouter) SetLabel(labels ...*router.Label) {
8280
r.labels = append(r.labels, labels...)
83-
return r
8481
}
8582

8683
func (r *subRouter) ResourceRouter(resourceName string, labels ...*router.Label) router.ResourceRouter {

http/router/httprouter/subrouter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (a *subRouterTestSuit) testWithParams() func(t *testing.T) {
9999
func (a *subRouterTestSuit) testSetLabelWithEntry() func(t *testing.T) {
100100
return func(t *testing.T) {
101101
label := router.NewLable("k1", "v1")
102-
a.sub.AddPublict("GET", "/index/entry/label", IndexHandler).SetLabel(label)
102+
a.sub.AddPublict("GET", "/index/entry/label", IndexHandler).AddLabel(label)
103103

104104
es := a.root.GetEndpoints()
105105

http/router/router.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ type ResourceRouter interface {
4444

4545
// SubRouter 子路由或者分组路由
4646
type SubRouter interface {
47-
EntryDecorator
4847
// 添加中间件
4948
Use(m Middleware)
49+
// SetLabel 设置路由标签, 作用于Entry上
50+
SetLabel(...*Label)
5051
// With独立作用于某一个Handler
5152
With(m ...Middleware) SubRouter
5253
// 添加受认证保护的路由
@@ -60,5 +61,5 @@ type SubRouter interface {
6061
// EntryDecorator 装饰
6162
type EntryDecorator interface {
6263
// SetLabel 设置子路由标签, 作用于Entry上
63-
SetLabel(...*Label) EntryDecorator
64+
AddLabel(...*Label) EntryDecorator
6465
}

0 commit comments

Comments
 (0)