Compare commits

...

2 Commits

Author SHA1 Message Date
b64a93ad2d fixup multiple client handling
Some checks failed
lint / lint (pull_request) Failing after 1m28s
pr / test (pull_request) Failing after 2m39s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-11-13 08:22:51 +03:00
92474de685 fixup multiple client handling
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-11-13 08:22:00 +03:00
2 changed files with 73 additions and 23 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 {
} if iface.Name() == n {
for idx, iface := range values { return idx
if ifc, ok := iface.(nameIface); ok && ifc.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 return 0
} }

View File

@ -17,20 +17,18 @@ import (
"go.unistack.org/micro/v4/tracer" "go.unistack.org/micro/v4/tracer"
) )
type testItem struct { func TestClient(t *testing.T) {
name string c1 := client.NewClient(options.Name("test1"))
} c2 := client.NewClient(options.Name("test2"))
func (ti *testItem) Name() string { svc := NewService(Client(c1, c2))
return ti.name if err := svc.Init(); err != nil {
} t.Fatal(err)
}
func TestGetNameIndex(t *testing.T) { x1 := svc.Client("test2")
item1 := &testItem{name: "first"} if x1.Name() != "test2" {
item2 := &testItem{name: "second"} t.Fatal("invalid client")
items := []interface{}{item1, item2}
if idx := getNameIndex("second", items); idx != 1 {
t.Fatalf("getNameIndex func error, item not found")
} }
} }