2019-09-09 08:57:57 -07:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/micro/go-micro/registry"
|
2019-10-13 12:23:13 +01:00
|
|
|
pb "github.com/micro/go-micro/registry/service/proto"
|
2019-09-09 08:57:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func values(v []*registry.Value) []*pb.Value {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return []*pb.Value{}
|
|
|
|
}
|
|
|
|
|
2019-11-11 00:03:51 +00:00
|
|
|
vs := make([]*pb.Value, 0, len(v))
|
2019-09-09 08:57:57 -07:00
|
|
|
for _, vi := range v {
|
|
|
|
vs = append(vs, &pb.Value{
|
|
|
|
Name: vi.Name,
|
|
|
|
Type: vi.Type,
|
|
|
|
Values: values(vi.Values),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
|
|
|
|
|
|
|
func toValues(v []*pb.Value) []*registry.Value {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return []*registry.Value{}
|
|
|
|
}
|
|
|
|
|
2019-11-11 00:03:51 +00:00
|
|
|
vs := make([]*registry.Value, 0, len(v))
|
2019-09-09 08:57:57 -07:00
|
|
|
for _, vi := range v {
|
|
|
|
vs = append(vs, ®istry.Value{
|
|
|
|
Name: vi.Name,
|
|
|
|
Type: vi.Type,
|
|
|
|
Values: toValues(vi.Values),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
|
|
|
|
2019-09-09 09:20:17 -07:00
|
|
|
func ToProto(s *registry.Service) *pb.Service {
|
2019-11-05 17:43:12 +00:00
|
|
|
endpoints := make([]*pb.Endpoint, 0, len(s.Endpoints))
|
2019-09-09 08:57:57 -07:00
|
|
|
for _, ep := range s.Endpoints {
|
|
|
|
var request, response *pb.Value
|
|
|
|
|
|
|
|
if ep.Request != nil {
|
|
|
|
request = &pb.Value{
|
|
|
|
Name: ep.Request.Name,
|
|
|
|
Type: ep.Request.Type,
|
|
|
|
Values: values(ep.Request.Values),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ep.Response != nil {
|
|
|
|
response = &pb.Value{
|
|
|
|
Name: ep.Response.Name,
|
|
|
|
Type: ep.Response.Type,
|
|
|
|
Values: values(ep.Response.Values),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints = append(endpoints, &pb.Endpoint{
|
|
|
|
Name: ep.Name,
|
|
|
|
Request: request,
|
|
|
|
Response: response,
|
|
|
|
Metadata: ep.Metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-11 00:03:51 +00:00
|
|
|
nodes := make([]*pb.Node, 0, len(s.Nodes))
|
2019-09-09 08:57:57 -07:00
|
|
|
|
|
|
|
for _, node := range s.Nodes {
|
|
|
|
nodes = append(nodes, &pb.Node{
|
|
|
|
Id: node.Id,
|
|
|
|
Address: node.Address,
|
|
|
|
Metadata: node.Metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.Service{
|
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Metadata: s.Metadata,
|
|
|
|
Endpoints: endpoints,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 09:20:17 -07:00
|
|
|
func ToService(s *pb.Service) *registry.Service {
|
2019-11-11 00:03:51 +00:00
|
|
|
endpoints := make([]*registry.Endpoint, 0, len(s.Endpoints))
|
2019-09-09 08:57:57 -07:00
|
|
|
for _, ep := range s.Endpoints {
|
|
|
|
var request, response *registry.Value
|
|
|
|
|
|
|
|
if ep.Request != nil {
|
|
|
|
request = ®istry.Value{
|
|
|
|
Name: ep.Request.Name,
|
|
|
|
Type: ep.Request.Type,
|
|
|
|
Values: toValues(ep.Request.Values),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ep.Response != nil {
|
|
|
|
response = ®istry.Value{
|
|
|
|
Name: ep.Response.Name,
|
|
|
|
Type: ep.Response.Type,
|
|
|
|
Values: toValues(ep.Response.Values),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints = append(endpoints, ®istry.Endpoint{
|
|
|
|
Name: ep.Name,
|
|
|
|
Request: request,
|
|
|
|
Response: response,
|
|
|
|
Metadata: ep.Metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-11 00:03:51 +00:00
|
|
|
nodes := make([]*registry.Node, 0, len(s.Nodes))
|
2019-09-09 08:57:57 -07:00
|
|
|
for _, node := range s.Nodes {
|
|
|
|
nodes = append(nodes, ®istry.Node{
|
|
|
|
Id: node.Id,
|
|
|
|
Address: node.Address,
|
|
|
|
Metadata: node.Metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®istry.Service{
|
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Metadata: s.Metadata,
|
|
|
|
Endpoints: endpoints,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
|
|
|
}
|