From 900e782c4beee80661930f276050652cea35af5c Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sun, 23 Aug 2020 13:10:48 +0100 Subject: [PATCH] Router table.Read replaces List/Query (#1966) * Table.REad insted of list and query * fmt --- registry.go | 2 +- table.go | 44 ++++++++++++++++++++++---------------------- table_test.go | 4 ++-- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/registry.go b/registry.go index a7b63dd..f36a870 100644 --- a/registry.go +++ b/registry.go @@ -266,7 +266,7 @@ func (r *rtr) Lookup(service string, opts ...router.LookupOption) ([]router.Rout q := router.NewLookup(opts...) // 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 { routes = router.Filter(routes, q) if len(routes) == 0 { diff --git a/table.go b/table.go index c268255..9f99a6a 100644 --- a/table.go +++ b/table.go @@ -198,35 +198,35 @@ func (t *table) Update(r router.Route) error { return nil } -// List returns a list of all routes in the table -func (t *table) List() ([]router.Route, error) { +// Read entries from the table +func (t *table) Read(opts ...router.ReadOption) ([]router.Route, error) { + var options router.ReadOptions + for _, o := range opts { + o(&options) + } + t.RLock() defer t.RUnlock() var routes []router.Route - for _, rmap := range t.routes { - for _, route := range rmap { - routes = append(routes, route.route) + + // get the routes based on options passed + if len(options.Service) > 0 { + routeMap, ok := t.routes[options.Service] + if !ok { + return nil, router.ErrRouteNotFound } + for _, rt := range routeMap { + routes = append(routes, rt.route) + } + return routes, nil } - return routes, nil -} - -// 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 { - return nil, router.ErrRouteNotFound - } - - var routes []router.Route - - for _, rt := range routeMap { - routes = append(routes, rt.route) + // otherwise get all routes + for _, serviceRoutes := range t.routes { + for _, rt := range serviceRoutes { + routes = append(routes, rt.route) + } } return routes, nil diff --git a/table_test.go b/table_test.go index 2aacd81..7d7b789 100644 --- a/table_test.go +++ b/table_test.go @@ -99,7 +99,7 @@ func TestList(t *testing.T) { } } - routes, err := table.List() + routes, err := table.Read() if err != nil { t.Fatalf("error listing routes: %s", err) } @@ -116,7 +116,7 @@ func TestQuery(t *testing.T) { t.Fatalf("error adding route: %s", err) } - rt, err := table.Query(route.Service) + rt, err := table.Read(router.ReadService(route.Service)) if err != nil { t.Fatal("Expected a route got err", err) }