From 80a2db264eac958b3c90adeb06c21a37d9bab970 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 13 Nov 2023 08:19:44 +0300 Subject: [PATCH] fixup multiple client handling Signed-off-by: Vasiliy Tolstov --- service.go | 75 +++++++++++++++++++++++++++++++++++++++++-------- service_test.go | 15 ++++++++++ 2 files changed, 79 insertions(+), 11 deletions(-) diff --git a/service.go b/service.go index 60165ced..693f8d40 100644 --- a/service.go +++ b/service.go @@ -88,6 +88,7 @@ func (s *service) Name() string { // Init initialises options. Additionally it calls cmd.Init // which parses command line flags. cmd.Init is only called // on first Init. +// //nolint:gocyclo func (s *service) Init(opts ...Option) error { var err error @@ -375,19 +376,71 @@ func (s *service) Run() error { return s.Stop() } -type nameIface interface { - Name() string -} - func getNameIndex(n string, ifaces interface{}) int { - values, ok := ifaces.([]interface{}) - if !ok { - return 0 - } - for idx, iface := range values { - if ifc, ok := iface.(nameIface); ok && ifc.Name() == n { - return idx + switch values := ifaces.(type) { + case []router.Router: + for idx, iface := range values { + if iface.Name() == n { + return idx + } } + case []register.Register: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + case []store.Store: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + case []tracer.Tracer: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + case []server.Server: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + case []config.Config: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + case []meter.Meter: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + case []broker.Broker: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + case []client.Client: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + /* + case []logger.Logger: + for idx, iface := range values { + if iface.Name() == n { + return idx + } + } + */ } + return 0 } diff --git a/service_test.go b/service_test.go index 1b96cb0d..1a23de60 100644 --- a/service_test.go +++ b/service_test.go @@ -17,6 +17,21 @@ import ( "go.unistack.org/micro/v3/tracer" ) +func TestClient(t *testing.T) { + c1 := client.NewClient(client.Name("test1")) + c2 := client.NewClient(client.Name("test2")) + + svc := NewService(Client(c1, c2)) + if err := svc.Init(); err != nil { + t.Fatal(err) + } + + x1 := svc.Client("test2") + if x1.Name() != "test2" { + t.Fatal("invalid client") + } +} + type testItem struct { name string } -- 2.45.2