2019-01-18 20:29:17 +03:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2019-01-18 20:29:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type servicesKey struct{}
|
|
|
|
|
2019-11-16 13:55:11 +03:00
|
|
|
func getServiceRecords(ctx context.Context) map[string]map[string]*record {
|
|
|
|
memServices, ok := ctx.Value(servicesKey{}).(map[string][]*registry.Service)
|
2019-01-18 20:29:17 +03:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-16 13:55:11 +03:00
|
|
|
|
|
|
|
services := make(map[string]map[string]*record)
|
|
|
|
|
|
|
|
for name, svc := range memServices {
|
|
|
|
if _, ok := services[name]; !ok {
|
|
|
|
services[name] = make(map[string]*record)
|
|
|
|
}
|
|
|
|
// go through every version of the service
|
|
|
|
for _, s := range svc {
|
|
|
|
services[s.Name][s.Version] = serviceToRecord(s, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return services
|
2019-01-18 20:29:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Services is an option that preloads service data
|
|
|
|
func Services(s map[string][]*registry.Service) registry.Option {
|
|
|
|
return func(o *registry.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, servicesKey{}, s)
|
|
|
|
}
|
|
|
|
}
|