2020-12-29 17:49:26 +02:00
|
|
|
package registry
|
2019-11-16 12:55:11 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func serviceToRecord(s *Service, ttl time.Duration) *record {
|
2020-02-24 16:15:20 +02:00
|
|
|
metadata := make(map[string]string, len(s.Metadata))
|
2019-11-16 12:55:11 +02:00
|
|
|
for k, v := range s.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:15:20 +02:00
|
|
|
nodes := make(map[string]*node, len(s.Nodes))
|
2019-11-16 12:55:11 +02:00
|
|
|
for _, n := range s.Nodes {
|
|
|
|
nodes[n.Id] = &node{
|
|
|
|
Node: n,
|
|
|
|
TTL: ttl,
|
|
|
|
LastSeen: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
endpoints := make([]*Endpoint, len(s.Endpoints))
|
2019-11-16 12:55:11 +02:00
|
|
|
for i, e := range s.Endpoints {
|
|
|
|
endpoints[i] = e
|
|
|
|
}
|
|
|
|
|
|
|
|
return &record{
|
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Metadata: metadata,
|
|
|
|
Nodes: nodes,
|
|
|
|
Endpoints: endpoints,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
func recordToService(r *record) *Service {
|
2020-02-24 16:15:20 +02:00
|
|
|
metadata := make(map[string]string, len(r.Metadata))
|
2019-11-16 12:55:11 +02:00
|
|
|
for k, v := range r.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
endpoints := make([]*Endpoint, len(r.Endpoints))
|
2019-11-16 12:55:11 +02:00
|
|
|
for i, e := range r.Endpoints {
|
2020-12-29 17:49:26 +02:00
|
|
|
request := new(Value)
|
2019-12-03 21:59:44 +02:00
|
|
|
if e.Request != nil {
|
|
|
|
*request = *e.Request
|
|
|
|
}
|
2020-12-29 17:49:26 +02:00
|
|
|
response := new(Value)
|
2019-12-03 21:59:44 +02:00
|
|
|
if e.Response != nil {
|
|
|
|
*response = *e.Response
|
|
|
|
}
|
2019-11-16 12:55:11 +02:00
|
|
|
|
2020-02-24 16:15:20 +02:00
|
|
|
metadata := make(map[string]string, len(e.Metadata))
|
2019-11-16 12:55:11 +02:00
|
|
|
for k, v := range e.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
endpoints[i] = &Endpoint{
|
2019-11-16 12:55:11 +02:00
|
|
|
Name: e.Name,
|
|
|
|
Request: request,
|
|
|
|
Response: response,
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
nodes := make([]*Node, len(r.Nodes))
|
2019-11-16 12:55:11 +02:00
|
|
|
i := 0
|
|
|
|
for _, n := range r.Nodes {
|
2020-02-24 16:15:20 +02:00
|
|
|
metadata := make(map[string]string, len(n.Metadata))
|
2019-11-16 12:55:11 +02:00
|
|
|
for k, v := range n.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
nodes[i] = &Node{
|
2019-11-16 12:55:11 +02:00
|
|
|
Id: n.Id,
|
|
|
|
Address: n.Address,
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2020-12-29 17:49:26 +02:00
|
|
|
return &Service{
|
2019-11-16 12:55:11 +02:00
|
|
|
Name: r.Name,
|
|
|
|
Version: r.Version,
|
|
|
|
Metadata: metadata,
|
|
|
|
Endpoints: endpoints,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
|
|
|
}
|