micro/registry/util.go
Vasiliy Tolstov 0e34c572b4 export registry util function to safe copy registry data
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2019-07-04 11:39:43 +03:00

142 lines
2.2 KiB
Go

package registry
func CopyServices(current []*Service) []*Service {
var services []*Service
for _, service := range current {
// copy service
s := new(Service)
*s = *service
// copy nodes
var nodes []*Node
for _, node := range service.Nodes {
n := new(Node)
*n = *node
nodes = append(nodes, n)
}
s.Nodes = nodes
// copy endpoints
var eps []*Endpoint
for _, ep := range service.Endpoints {
e := new(Endpoint)
*e = *ep
eps = append(eps, e)
}
s.Endpoints = eps
// append service
services = append(services, s)
}
return services
}
func addServiceNodes(old, neu []*Node) []*Node {
var nodes []*Node
// add all new nodes
for _, n := range neu {
node := *n
nodes = append(nodes, &node)
}
// look at old nodes
for _, o := range old {
var exists bool
// check against new nodes
for _, n := range nodes {
// ids match then skip
if o.Id == n.Id {
exists = true
break
}
}
// keep old node
if !exists {
node := *o
nodes = append(nodes, &node)
}
}
return nodes
}
func AddServices(olist []*Service, nlist []*Service) []*Service {
var srv []*Service
for _, n := range nlist {
var seen bool
for _, o := range olist {
if o.Version == n.Version {
sp := new(Service)
// make copy
*sp = *o
// set nodes
sp.Nodes = addServiceNodes(o.Nodes, n.Nodes)
// mark as seen
seen = true
srv = append(srv, sp)
break
} else {
sp := new(Service)
// make copy
*sp = *o
srv = append(srv, sp)
}
}
if !seen {
srv = append(srv, CopyServices([]*Service{n})...)
}
}
return srv
}
func delServiceNodes(old, del []*Node) []*Node {
var nodes []*Node
for _, o := range old {
var rem bool
for _, n := range del {
if o.Id == n.Id {
rem = true
break
}
}
if !rem {
nodes = append(nodes, o)
}
}
return nodes
}
func DelServices(old, del []*Service) []*Service {
var services []*Service
for _, o := range old {
srv := new(Service)
*srv = *o
var rem bool
for _, s := range del {
if srv.Version == s.Version {
srv.Nodes = delServiceNodes(srv.Nodes, s.Nodes)
if len(srv.Nodes) == 0 {
rem = true
}
}
}
if !rem {
services = append(services, srv)
}
}
return services
}