micro/registry/service/util.go

175 lines
3.6 KiB
Go
Raw Normal View History

2019-09-09 18:57:57 +03:00
package service
import (
"strings"
"github.com/micro/go-micro/v2/registry"
pb "github.com/micro/go-micro/v2/registry/service/proto"
2019-09-09 18:57:57 +03:00
)
func values(v []*registry.Value) []*pb.Value {
if len(v) == 0 {
return []*pb.Value{}
}
2019-11-11 03:03:51 +03:00
vs := make([]*pb.Value, 0, len(v))
2019-09-09 18:57:57 +03: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 03:03:51 +03:00
vs := make([]*registry.Value, 0, len(v))
2019-09-09 18:57:57 +03:00
for _, vi := range v {
vs = append(vs, &registry.Value{
Name: vi.Name,
Type: vi.Type,
Values: toValues(vi.Values),
})
}
return vs
}
// NameSeperator is the string which is used as a seperator when joinin
// namespace to the service name
const NameSeperator = "/"
2019-09-09 19:20:17 +03:00
func ToProto(s *registry.Service) *pb.Service {
2019-11-05 20:43:12 +03:00
endpoints := make([]*pb.Endpoint, 0, len(s.Endpoints))
2019-09-09 18:57:57 +03: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 03:03:51 +03:00
nodes := make([]*pb.Node, 0, len(s.Nodes))
2019-09-09 18:57:57 +03:00
for _, node := range s.Nodes {
nodes = append(nodes, &pb.Node{
Id: node.Id,
Address: node.Address,
Metadata: node.Metadata,
})
}
// the service name will contain the namespace, e.g.
// 'default/go.micro.service.foo'. Remove the namespace
// using the following:
comps := strings.Split(s.Name, NameSeperator)
name := comps[len(comps)-1]
2019-09-09 18:57:57 +03:00
return &pb.Service{
Name: name,
2019-09-09 18:57:57 +03:00
Version: s.Version,
Metadata: s.Metadata,
Endpoints: endpoints,
Nodes: nodes,
Options: new(pb.Options),
2019-09-09 18:57:57 +03:00
}
}
func ToService(s *pb.Service, opts ...Option) *registry.Service {
var options Options
for _, o := range opts {
o(&options)
}
2019-11-11 03:03:51 +03:00
endpoints := make([]*registry.Endpoint, 0, len(s.Endpoints))
2019-09-09 18:57:57 +03:00
for _, ep := range s.Endpoints {
var request, response *registry.Value
if ep.Request != nil {
request = &registry.Value{
Name: ep.Request.Name,
Type: ep.Request.Type,
Values: toValues(ep.Request.Values),
}
}
if ep.Response != nil {
response = &registry.Value{
Name: ep.Response.Name,
Type: ep.Response.Type,
Values: toValues(ep.Response.Values),
}
}
endpoints = append(endpoints, &registry.Endpoint{
Name: ep.Name,
Request: request,
Response: response,
Metadata: ep.Metadata,
})
}
2019-11-11 03:03:51 +03:00
nodes := make([]*registry.Node, 0, len(s.Nodes))
2019-09-09 18:57:57 +03:00
for _, node := range s.Nodes {
nodes = append(nodes, &registry.Node{
Id: node.Id,
Address: node.Address,
Metadata: node.Metadata,
})
}
// add the namespace to the name
var name string
if len(options.Namespace) > 0 {
name = strings.Join([]string{options.Namespace, s.Name}, NameSeperator)
} else {
name = s.Name
}
2019-09-09 18:57:57 +03:00
return &registry.Service{
Name: name,
2019-09-09 18:57:57 +03:00
Version: s.Version,
Metadata: s.Metadata,
Endpoints: endpoints,
Nodes: nodes,
}
}
// Options for marshaling / unmarshaling services
type Options struct {
Namespace string
}
// Option is a function which sets options
type Option func(o *Options)
// WithNamespace sets the namespace option
func WithNamespace(ns string) Option {
return func(o *Options) {
o.Namespace = ns
}
}