Skip to content

Commit

Permalink
调整装饰器模式
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaojun03 committed Mar 11, 2020
1 parent 3f86b20 commit 212197c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 3 additions & 1 deletion http/router/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ type Entry struct {
}

// AddLabel 添加Label
func (e *Entry) AddLabel(labels ...*Label) {
func (e *Entry) AddLabel(labels ...*Label) EntryDecorator {
for i := range labels {
e.Labels[labels[i].Key()] = labels[i].Value()
}

return e
}

// NewEntrySet 实例
Expand Down
9 changes: 3 additions & 6 deletions http/router/httprouter/subrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (r *subRouter) AddProtected(method, path string, h http.HandlerFunc) router

r.add(e)

new := *r
return &new
return e.Entry
}

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

new := *r
return &new
return e.Entry
}

func (r *subRouter) SetLabel(labels ...*router.Label) router.EntryDecorator {
func (r *subRouter) SetLabel(labels ...*router.Label) {
r.labels = append(r.labels, labels...)
return r
}

func (r *subRouter) ResourceRouter(resourceName string, labels ...*router.Label) router.ResourceRouter {
Expand Down
2 changes: 1 addition & 1 deletion http/router/httprouter/subrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (a *subRouterTestSuit) testWithParams() func(t *testing.T) {
func (a *subRouterTestSuit) testSetLabelWithEntry() func(t *testing.T) {
return func(t *testing.T) {
label := router.NewLable("k1", "v1")
a.sub.AddPublict("GET", "/index/entry/label", IndexHandler).SetLabel(label)
a.sub.AddPublict("GET", "/index/entry/label", IndexHandler).AddLabel(label)

es := a.root.GetEndpoints()

Expand Down
5 changes: 3 additions & 2 deletions http/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ type ResourceRouter interface {

// SubRouter 子路由或者分组路由
type SubRouter interface {
EntryDecorator
// 添加中间件
Use(m Middleware)
// SetLabel 设置路由标签, 作用于Entry上
SetLabel(...*Label)
// With独立作用于某一个Handler
With(m ...Middleware) SubRouter
// 添加受认证保护的路由
Expand All @@ -60,5 +61,5 @@ type SubRouter interface {
// EntryDecorator 装饰
type EntryDecorator interface {
// SetLabel 设置子路由标签, 作用于Entry上
SetLabel(...*Label) EntryDecorator
AddLabel(...*Label) EntryDecorator
}

0 comments on commit 212197c

Please sign in to comment.