Skip to content

Commit

Permalink
[Refactor] route, remove route children, use RouteList
Browse files Browse the repository at this point in the history
  • Loading branch information
JanGalek committed Jan 3, 2025
1 parent 270f0db commit fd140ce
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 135 deletions.
36 changes: 14 additions & 22 deletions route.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
package router

type Route struct {
name string
pattern string
handler interface{}
method Method
children map[string]*Route
name string
pattern string
handler interface{}
method Method
}

func NewRoute(name string, pattern string, handler interface{}, method Method, children map[string]*Route) *Route {
// NewRoute create Route
func NewRoute(name string, pattern string, handler interface{}, method Method) *Route {
return &Route{
name: name,
pattern: pattern,
handler: handler,
method: method,
children: children,
name: name,
pattern: pattern,
handler: handler,
method: method,
}
}

// GetName get route name
func (r *Route) GetName() string {
return r.name
}

// GetMethod get route method
func (r *Route) GetMethod() Method {
return r.method
}

func (r *Route) GetChildren() map[string]*Route {
return r.children
}

// GetPattern get route pattern
func (r *Route) GetPattern() string {
return r.pattern
}

// GetHandler get route handler
func (r *Route) GetHandler() interface{} {
return r.handler
}

func (r *Route) AddChild(name string, pattern string, handler interface{}, method Method) *Route {
child := NewRoute(r.name+":"+name, pattern, handler, method, nil)
r.children[pattern] = child

return r
}
11 changes: 6 additions & 5 deletions routeList.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@ type RouteList struct {
children []*RouteList
}

// NewRouteList create RouteList with empty pattern
func NewRouteList() *RouteList {
return CreateRouteList("")
}

// CreateRouteList create RouteList
func CreateRouteList(pattern string) *RouteList {
return &RouteList{pattern: pattern}
}

// AddChild add child of RouteList
func (l *RouteList) AddChild(child *RouteList) *RouteList {
l.children = append(l.children, child)

return l
}

// Add create route for RouteList
func (l *RouteList) Add(name string, pattern string, handler interface{}, method Method) *RouteList {
return l.AddWithChildren(name, pattern, handler, method, nil)
}

func (l *RouteList) AddWithChildren(name string, pattern string, handler interface{}, method Method, children map[string]*Route) *RouteList {
route := NewRoute(name, pattern, handler, method, children)
route := NewRoute(name, pattern, handler, method)
l.AddRoute(route)

return l
}

// AddRoute add Route to RouteList
func (l *RouteList) AddRoute(r *Route) *RouteList {
l.routes = append(l.routes, r)
return l
Expand Down
Loading

0 comments on commit fd140ce

Please sign in to comment.