gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/registry/memory/options.go (about)

     1  package memory
     2  
     3  import (
     4  	"context"
     5  
     6  	"gitee.com/liuxuezhan/go-micro-v1.18.0/registry"
     7  )
     8  
     9  type servicesKey struct{}
    10  
    11  func getServiceRecords(ctx context.Context) map[string]map[string]*record {
    12  	memServices, ok := ctx.Value(servicesKey{}).(map[string][]*registry.Service)
    13  	if !ok {
    14  		return nil
    15  	}
    16  
    17  	services := make(map[string]map[string]*record)
    18  
    19  	for name, svc := range memServices {
    20  		if _, ok := services[name]; !ok {
    21  			services[name] = make(map[string]*record)
    22  		}
    23  		// go through every version of the service
    24  		for _, s := range svc {
    25  			services[s.Name][s.Version] = serviceToRecord(s, 0)
    26  		}
    27  	}
    28  
    29  	return services
    30  }
    31  
    32  // Services is an option that preloads service data
    33  func Services(s map[string][]*registry.Service) registry.Option {
    34  	return func(o *registry.Options) {
    35  		if o.Context == nil {
    36  			o.Context = context.Background()
    37  		}
    38  		o.Context = context.WithValue(o.Context, servicesKey{}, s)
    39  	}
    40  }