github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/registry/memory/options.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/registry/memory/options.go
    14  
    15  package memory
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/tickoalcantara12/micro/v3/service/registry"
    21  )
    22  
    23  type servicesKey struct{}
    24  
    25  func getServiceRecords(ctx context.Context) map[string]map[string]*record {
    26  	memServices, ok := ctx.Value(servicesKey{}).(map[string][]*registry.Service)
    27  	if !ok {
    28  		return nil
    29  	}
    30  
    31  	services := make(map[string]map[string]*record)
    32  
    33  	for name, svc := range memServices {
    34  		if _, ok := services[name]; !ok {
    35  			services[name] = make(map[string]*record)
    36  		}
    37  		// go through every version of the service
    38  		for _, s := range svc {
    39  			services[s.Name][s.Version] = serviceToRecord(s, 0)
    40  		}
    41  	}
    42  
    43  	return services
    44  }
    45  
    46  // Services is an option that preloads service data
    47  func Services(s map[string][]*registry.Service) registry.Option {
    48  	return func(o *registry.Options) {
    49  		if o.Context == nil {
    50  			o.Context = context.Background()
    51  		}
    52  		o.Context = context.WithValue(o.Context, servicesKey{}, s)
    53  	}
    54  }