Merge pull request 'fixup multiple client handling' (#280) from multiple into v3

Reviewed-on: #280
This commit is contained in:
Василий Толстов 2023-11-13 08:20:52 +03:00
commit 84552513f7
2 changed files with 79 additions and 11 deletions

View File

@ -88,6 +88,7 @@ func (s *service) Name() string {
// Init initialises options. Additionally it calls cmd.Init // Init initialises options. Additionally it calls cmd.Init
// which parses command line flags. cmd.Init is only called // which parses command line flags. cmd.Init is only called
// on first Init. // on first Init.
//
//nolint:gocyclo //nolint:gocyclo
func (s *service) Init(opts ...Option) error { func (s *service) Init(opts ...Option) error {
var err error var err error
@ -375,19 +376,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/v3/tracer" "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 { type testItem struct {
name string name string
} }