github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/registry/memory/util.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/util.go
    14  
    15  package memory
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/tickoalcantara12/micro/v3/service/registry"
    21  )
    22  
    23  func serviceToRecord(s *registry.Service, ttl time.Duration) *record {
    24  	metadata := make(map[string]string, len(s.Metadata))
    25  	for k, v := range s.Metadata {
    26  		metadata[k] = v
    27  	}
    28  
    29  	nodes := make(map[string]*node, len(s.Nodes))
    30  	for _, n := range s.Nodes {
    31  		nodes[n.Id] = &node{
    32  			Node:     n,
    33  			TTL:      ttl,
    34  			LastSeen: time.Now(),
    35  		}
    36  	}
    37  
    38  	endpoints := make([]*registry.Endpoint, len(s.Endpoints))
    39  	for i, e := range s.Endpoints {
    40  		endpoints[i] = e
    41  	}
    42  
    43  	return &record{
    44  		Name:      s.Name,
    45  		Version:   s.Version,
    46  		Metadata:  metadata,
    47  		Nodes:     nodes,
    48  		Endpoints: endpoints,
    49  	}
    50  }
    51  
    52  func recordToService(r *record, domain string) *registry.Service {
    53  	metadata := make(map[string]string, len(r.Metadata))
    54  	for k, v := range r.Metadata {
    55  		metadata[k] = v
    56  	}
    57  
    58  	// set the domain in metadata so it can be determined when a wildcard query is performed
    59  	metadata["domain"] = domain
    60  
    61  	endpoints := make([]*registry.Endpoint, len(r.Endpoints))
    62  	for i, e := range r.Endpoints {
    63  		request := new(registry.Value)
    64  		if e.Request != nil {
    65  			*request = *e.Request
    66  		}
    67  		response := new(registry.Value)
    68  		if e.Response != nil {
    69  			*response = *e.Response
    70  		}
    71  
    72  		metadata := make(map[string]string, len(e.Metadata))
    73  		for k, v := range e.Metadata {
    74  			metadata[k] = v
    75  		}
    76  
    77  		endpoints[i] = &registry.Endpoint{
    78  			Name:     e.Name,
    79  			Request:  request,
    80  			Response: response,
    81  			Metadata: metadata,
    82  		}
    83  	}
    84  
    85  	nodes := make([]*registry.Node, len(r.Nodes))
    86  	i := 0
    87  	for _, n := range r.Nodes {
    88  		metadata := make(map[string]string, len(n.Metadata))
    89  		for k, v := range n.Metadata {
    90  			metadata[k] = v
    91  		}
    92  
    93  		nodes[i] = &registry.Node{
    94  			Id:       n.Id,
    95  			Address:  n.Address,
    96  			Metadata: metadata,
    97  		}
    98  		i++
    99  	}
   100  
   101  	return &registry.Service{
   102  		Name:      r.Name,
   103  		Version:   r.Version,
   104  		Metadata:  metadata,
   105  		Endpoints: endpoints,
   106  		Nodes:     nodes,
   107  	}
   108  }