fixup multiple client handling

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-11-13 08:19:44 +03:00
parent b1fd82adf8
commit 92474de685
2 changed files with 78 additions and 11 deletions

View File

@ -372,19 +372,71 @@ func (s *service) Run() error {
return s.Stop() return s.Stop()
} }
type nameIface interface {
Name() string
}
func getNameIndex(n string, ifaces interface{}) int { func getNameIndex(n string, ifaces interface{}) int {
values, ok := ifaces.([]interface{}) switch values := ifaces.(type) {
if !ok { case []router.Router:
return 0
}
for idx, iface := range values { for idx, iface := range values {
if ifc, ok := iface.(nameIface); ok && ifc.Name() == n { if iface.Name() == n {
return idx 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 return 0
} }

View File

@ -17,6 +17,21 @@ import (
"go.unistack.org/micro/v4/tracer" "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 { type testItem struct {
name string name string
} }