fixup multiple client handling (#281)
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				/ autoupdate (push) Failing after 1m28s
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	/ autoupdate (push) Failing after 1m28s
				
			Reviewed-on: #281
This commit was merged in pull request #281.
	This commit is contained in:
		
							
								
								
									
										70
									
								
								service.go
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								service.go
									
									
									
									
									
								
							| @@ -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 | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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")) | ||||||
|  |  | ||||||
|  | 	svc := NewService(Client(c1, c2)) | ||||||
|  | 	if err := svc.Init(); err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| func (ti *testItem) Name() string { | 	x1 := svc.Client("test2") | ||||||
| 	return ti.name | 	if x1.Name() != "test2" { | ||||||
| } | 		t.Fatal("invalid client") | ||||||
|  |  | ||||||
| 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") |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user