2019-07-04 00:15:44 +03:00
|
|
|
package registry
|
2018-12-04 16:41:40 +00:00
|
|
|
|
2019-07-04 11:36:49 +01:00
|
|
|
func addNodes(old, neu []*Node) []*Node {
|
2019-07-15 14:47:33 +01:00
|
|
|
nodes := make([]*Node, len(neu))
|
2019-07-04 11:36:49 +01:00
|
|
|
// add all new nodes
|
2019-07-15 14:47:33 +01:00
|
|
|
for i, n := range neu {
|
2019-07-04 11:36:49 +01:00
|
|
|
node := *n
|
2019-07-15 14:47:33 +01:00
|
|
|
nodes[i] = &node
|
2019-07-04 11:36:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 delNodes(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
|
|
|
|
}
|
|
|
|
|
2019-09-30 10:36:43 +08:00
|
|
|
// CopyService make a copy of service
|
|
|
|
func CopyService(service *Service) *Service {
|
|
|
|
// copy service
|
|
|
|
s := new(Service)
|
|
|
|
*s = *service
|
|
|
|
|
|
|
|
// copy nodes
|
|
|
|
nodes := make([]*Node, len(service.Nodes))
|
|
|
|
for j, node := range service.Nodes {
|
|
|
|
n := new(Node)
|
|
|
|
*n = *node
|
|
|
|
nodes[j] = n
|
|
|
|
}
|
|
|
|
s.Nodes = nodes
|
|
|
|
|
|
|
|
// copy endpoints
|
|
|
|
eps := make([]*Endpoint, len(service.Endpoints))
|
|
|
|
for j, ep := range service.Endpoints {
|
|
|
|
e := new(Endpoint)
|
|
|
|
*e = *ep
|
|
|
|
eps[j] = e
|
|
|
|
}
|
|
|
|
s.Endpoints = eps
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-07-04 11:36:49 +01:00
|
|
|
// Copy makes a copy of services
|
|
|
|
func Copy(current []*Service) []*Service {
|
2019-07-15 11:13:58 +01:00
|
|
|
services := make([]*Service, len(current))
|
|
|
|
for i, service := range current {
|
2019-09-30 10:36:43 +08:00
|
|
|
services[i] = CopyService(service)
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2019-07-04 11:36:49 +01:00
|
|
|
// Merge merges two lists of services and returns a new copy
|
|
|
|
func Merge(olist []*Service, nlist []*Service) []*Service {
|
2019-07-04 00:15:44 +03:00
|
|
|
var srv []*Service
|
2019-02-01 09:05:03 +00:00
|
|
|
|
2019-07-04 00:15:44 +03:00
|
|
|
for _, n := range nlist {
|
2018-12-04 16:41:40 +00:00
|
|
|
var seen bool
|
2019-07-04 00:15:44 +03:00
|
|
|
for _, o := range olist {
|
|
|
|
if o.Version == n.Version {
|
|
|
|
sp := new(Service)
|
2019-02-01 09:05:03 +00:00
|
|
|
// make copy
|
|
|
|
*sp = *o
|
|
|
|
// set nodes
|
2019-07-04 11:36:49 +01:00
|
|
|
sp.Nodes = addNodes(o.Nodes, n.Nodes)
|
2019-02-01 09:05:03 +00:00
|
|
|
|
|
|
|
// mark as seen
|
2018-12-04 16:41:40 +00:00
|
|
|
seen = true
|
2019-02-01 09:05:03 +00:00
|
|
|
srv = append(srv, sp)
|
2018-12-04 16:41:40 +00:00
|
|
|
break
|
2019-07-04 00:15:44 +03:00
|
|
|
} else {
|
|
|
|
sp := new(Service)
|
|
|
|
// make copy
|
|
|
|
*sp = *o
|
|
|
|
srv = append(srv, sp)
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !seen {
|
2019-07-04 11:36:49 +01:00
|
|
|
srv = append(srv, Copy([]*Service{n})...)
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-01 09:05:03 +00:00
|
|
|
return srv
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 11:36:49 +01:00
|
|
|
// Remove removes services and returns a new copy
|
|
|
|
func Remove(old, del []*Service) []*Service {
|
2019-07-04 00:15:44 +03:00
|
|
|
var services []*Service
|
2019-06-22 18:58:12 +01:00
|
|
|
|
2018-12-29 15:44:51 +00:00
|
|
|
for _, o := range old {
|
2019-07-04 00:15:44 +03:00
|
|
|
srv := new(Service)
|
2019-06-22 18:58:12 +01:00
|
|
|
*srv = *o
|
|
|
|
|
2018-12-04 16:41:40 +00:00
|
|
|
var rem bool
|
2019-06-22 18:58:12 +01:00
|
|
|
|
2018-12-04 16:41:40 +00:00
|
|
|
for _, s := range del {
|
2019-06-22 18:58:12 +01:00
|
|
|
if srv.Version == s.Version {
|
2019-07-04 11:36:49 +01:00
|
|
|
srv.Nodes = delNodes(srv.Nodes, s.Nodes)
|
2019-06-22 18:58:12 +01:00
|
|
|
|
|
|
|
if len(srv.Nodes) == 0 {
|
2018-12-04 16:41:40 +00:00
|
|
|
rem = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 18:58:12 +01:00
|
|
|
|
2018-12-04 16:41:40 +00:00
|
|
|
if !rem {
|
2019-06-22 18:58:12 +01:00
|
|
|
services = append(services, srv)
|
2018-12-04 16:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 18:58:12 +01:00
|
|
|
|
2018-12-04 16:41:40 +00:00
|
|
|
return services
|
|
|
|
}
|