From 92474de685b6ca6feb657e0efce3dc1364dd294d Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 13 Nov 2023 08:19:44 +0300 Subject: [PATCH 1/2] fixup multiple client handling Signed-off-by: Vasiliy Tolstov --- service.go | 74 +++++++++++++++++++++++++++++++++++++++++-------- service_test.go | 15 ++++++++++ 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/service.go b/service.go index fe1d950b..3bf9118f 100644 --- a/service.go +++ b/service.go @@ -372,19 +372,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 0063727e..460fc0f5 100644 --- a/service_test.go +++ b/service_test.go @@ -17,6 +17,21 @@ import ( "go.unistack.org/micro/v4/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 From b64a93ad2d67bdecd412aed241e6ed95d2da96fc Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 13 Nov 2023 08:22:51 +0300 Subject: [PATCH 2/2] fixup multiple client handling Signed-off-by: Vasiliy Tolstov --- service_test.go | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/service_test.go b/service_test.go index 460fc0f5..582318da 100644 --- a/service_test.go +++ b/service_test.go @@ -18,8 +18,8 @@ import ( ) func TestClient(t *testing.T) { - c1 := client.NewClient(client.Name("test1")) - c2 := client.NewClient(client.Name("test2")) + c1 := client.NewClient(options.Name("test1")) + c2 := client.NewClient(options.Name("test2")) svc := NewService(Client(c1, c2)) if err := svc.Init(); err != nil { @@ -32,23 +32,6 @@ func TestClient(t *testing.T) { } } -type testItem struct { - name string -} - -func (ti *testItem) Name() string { - return ti.name -} - -func TestGetNameIndex(t *testing.T) { - item1 := &testItem{name: "first"} - item2 := &testItem{name: "second"} - items := []interface{}{item1, item2} - if idx := getNameIndex("second", items); idx != 1 { - t.Fatalf("getNameIndex func error, item not found") - } -} - func TestRegisterHandler(t *testing.T) { type args struct { s server.Server -- 2.45.2