register/noop: add noop register #306

Merged
vtolstov merged 2 commits from register-noop into v3 2024-03-01 21:40:04 +03:00
2 changed files with 88 additions and 66 deletions
Showing only changes of commit 3cd8bc33d6 - Show all commits

View File

@ -7,11 +7,15 @@ import (
"go.unistack.org/micro/v3/broker"
"go.unistack.org/micro/v3/client"
"go.unistack.org/micro/v3/codec"
"go.unistack.org/micro/v3/config"
"go.unistack.org/micro/v3/flow"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/meter"
"go.unistack.org/micro/v3/register"
"go.unistack.org/micro/v3/resolver"
"go.unistack.org/micro/v3/router"
"go.unistack.org/micro/v3/selector"
"go.unistack.org/micro/v3/server"
"go.unistack.org/micro/v3/store"
"go.unistack.org/micro/v3/tracer"
@ -72,8 +76,8 @@ func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...se
}
type service struct {
sync.RWMutex
opts Options
sync.RWMutex
}
// NewService creates and returns a new Service based on the packages within.
@ -377,69 +381,95 @@ func (s *service) Run() error {
}
func getNameIndex(n string, ifaces interface{}) int {
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
}
}
type namer interface {
Name() string
}
switch vt := ifaces.(type) {
case []broker.Broker:
for idx, iface := range values {
if iface.Name() == n {
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []client.Client:
for idx, iface := range values {
if iface.Name() == n {
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []codec.Codec:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []config.Config:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []flow.Flow:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []logger.Logger:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []meter.Meter:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []register.Register:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []resolver.Resolver:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []router.Router:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []selector.Selector:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []server.Server:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []store.Store:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
case []tracer.Tracer:
for idx, iface := range vt {
if nm, ok := iface.(namer); ok && nm.Name() == n {
return idx
}
}
/*
case []logger.Logger:
for idx, iface := range values {
if iface.Name() == n {
return idx
}
}
*/
}
return 0

View File

@ -22,13 +22,14 @@ func TestClient(t *testing.T) {
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")
t.Fatalf("invalid client %#+v", svc.Options().Clients)
}
}
@ -40,15 +41,6 @@ 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