Router table.Read replaces List/Query (#1966)
* Table.REad insted of list and query * fmt
This commit is contained in:
parent
1d61a1788f
commit
900e782c4b
@ -266,7 +266,7 @@ func (r *rtr) Lookup(service string, opts ...router.LookupOption) ([]router.Rout
|
|||||||
q := router.NewLookup(opts...)
|
q := router.NewLookup(opts...)
|
||||||
|
|
||||||
// if we find the routes filter and return them
|
// if we find the routes filter and return them
|
||||||
routes, err := r.table.Query(service)
|
routes, err := r.table.Read(router.ReadService(service))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
routes = router.Filter(routes, q)
|
routes = router.Filter(routes, q)
|
||||||
if len(routes) == 0 {
|
if len(routes) == 0 {
|
||||||
|
38
table.go
38
table.go
@ -198,36 +198,36 @@ func (t *table) Update(r router.Route) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// List returns a list of all routes in the table
|
// Read entries from the table
|
||||||
func (t *table) List() ([]router.Route, error) {
|
func (t *table) Read(opts ...router.ReadOption) ([]router.Route, error) {
|
||||||
|
var options router.ReadOptions
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
t.RLock()
|
t.RLock()
|
||||||
defer t.RUnlock()
|
defer t.RUnlock()
|
||||||
|
|
||||||
var routes []router.Route
|
var routes []router.Route
|
||||||
for _, rmap := range t.routes {
|
|
||||||
for _, route := range rmap {
|
|
||||||
routes = append(routes, route.route)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return routes, nil
|
// get the routes based on options passed
|
||||||
}
|
if len(options.Service) > 0 {
|
||||||
|
routeMap, ok := t.routes[options.Service]
|
||||||
// Lookup queries routing table and returns all routes that match the lookup query
|
|
||||||
func (t *table) Query(service string) ([]router.Route, error) {
|
|
||||||
t.RLock()
|
|
||||||
defer t.RUnlock()
|
|
||||||
|
|
||||||
routeMap, ok := t.routes[service]
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, router.ErrRouteNotFound
|
return nil, router.ErrRouteNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
var routes []router.Route
|
|
||||||
|
|
||||||
for _, rt := range routeMap {
|
for _, rt := range routeMap {
|
||||||
routes = append(routes, rt.route)
|
routes = append(routes, rt.route)
|
||||||
}
|
}
|
||||||
|
return routes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise get all routes
|
||||||
|
for _, serviceRoutes := range t.routes {
|
||||||
|
for _, rt := range serviceRoutes {
|
||||||
|
routes = append(routes, rt.route)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return routes, nil
|
return routes, nil
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ func TestList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
routes, err := table.List()
|
routes, err := table.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error listing routes: %s", err)
|
t.Fatalf("error listing routes: %s", err)
|
||||||
}
|
}
|
||||||
@ -116,7 +116,7 @@ func TestQuery(t *testing.T) {
|
|||||||
t.Fatalf("error adding route: %s", err)
|
t.Fatalf("error adding route: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rt, err := table.Query(route.Service)
|
rt, err := table.Read(router.ReadService(route.Service))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Expected a route got err", err)
|
t.Fatal("Expected a route got err", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user