github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/router/client/table.go (about) 1 package client 2 3 import ( 4 pb "github.com/tickoalcantara12/micro/v3/proto/router" 5 "github.com/tickoalcantara12/micro/v3/service/client" 6 "github.com/tickoalcantara12/micro/v3/service/context" 7 "github.com/tickoalcantara12/micro/v3/service/router" 8 ) 9 10 type table struct { 11 table pb.TableService 12 callOpts []client.CallOption 13 } 14 15 // Create new route in the routing table 16 func (t *table) Create(r router.Route) error { 17 route := &pb.Route{ 18 Service: r.Service, 19 Address: r.Address, 20 Gateway: r.Gateway, 21 Network: r.Network, 22 Link: r.Link, 23 Metric: r.Metric, 24 } 25 26 if _, err := t.table.Create(context.DefaultContext, route, t.callOpts...); err != nil { 27 return err 28 } 29 30 return nil 31 } 32 33 // Delete deletes existing route from the routing table 34 func (t *table) Delete(r router.Route) error { 35 route := &pb.Route{ 36 Service: r.Service, 37 Address: r.Address, 38 Gateway: r.Gateway, 39 Network: r.Network, 40 Link: r.Link, 41 Metric: r.Metric, 42 } 43 44 if _, err := t.table.Delete(context.DefaultContext, route, t.callOpts...); err != nil { 45 return err 46 } 47 48 return nil 49 } 50 51 // Update updates route in the routing table 52 func (t *table) Update(r router.Route) error { 53 route := &pb.Route{ 54 Service: r.Service, 55 Address: r.Address, 56 Gateway: r.Gateway, 57 Network: r.Network, 58 Link: r.Link, 59 Metric: r.Metric, 60 } 61 62 if _, err := t.table.Update(context.DefaultContext, route, t.callOpts...); err != nil { 63 return err 64 } 65 66 return nil 67 } 68 69 // Read looks up routes in the routing table and returns them 70 func (t *table) Read(opts ...router.ReadOption) ([]router.Route, error) { 71 var options router.ReadOptions 72 for _, o := range opts { 73 o(&options) 74 } 75 // call the router 76 resp, err := t.table.Read(context.DefaultContext, &pb.ReadRequest{ 77 Service: options.Service, 78 }, t.callOpts...) 79 // errored out 80 if err != nil { 81 return nil, err 82 } 83 84 routes := make([]router.Route, len(resp.Routes)) 85 for i, route := range resp.Routes { 86 routes[i] = router.Route{ 87 Service: route.Service, 88 Address: route.Address, 89 Gateway: route.Gateway, 90 Network: route.Network, 91 Link: route.Link, 92 Metric: route.Metric, 93 } 94 } 95 96 return routes, nil 97 }