-
Notifications
You must be signed in to change notification settings - Fork 592
/
relations.go
82 lines (75 loc) · 2.3 KB
/
relations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package util
type ForeignRelations struct {
Consumer, ConsumerGroup, Route, Service []string
}
type Rel struct {
Consumer, ConsumerGroup, Route, Service string
}
func (relations *ForeignRelations) GetCombinations() []Rel {
var (
lConsumer = len(relations.Consumer)
lConsumerGroup = len(relations.ConsumerGroup)
lRoutes = len(relations.Route)
lServices = len(relations.Service)
l = lRoutes + lServices
)
var cartesianProduct []Rel
// gocritic I don't care that you think switch statements are the one true god of readability, the language offers
// multiple options for a reason. go away, gocritic.
if lConsumer > 0 { //nolint:gocritic
if l > 0 {
cartesianProduct = make([]Rel, 0, l*lConsumer)
for _, consumer := range relations.Consumer {
for _, service := range relations.Service {
cartesianProduct = append(cartesianProduct, Rel{
Service: service,
Consumer: consumer,
})
}
for _, route := range relations.Route {
cartesianProduct = append(cartesianProduct, Rel{
Route: route,
Consumer: consumer,
})
}
}
} else {
cartesianProduct = make([]Rel, 0, len(relations.Consumer))
for _, consumer := range relations.Consumer {
cartesianProduct = append(cartesianProduct, Rel{Consumer: consumer})
}
}
} else if lConsumerGroup > 0 {
if l > 0 {
cartesianProduct = make([]Rel, 0, l*lConsumerGroup)
for _, group := range relations.ConsumerGroup {
for _, service := range relations.Service {
cartesianProduct = append(cartesianProduct, Rel{
Service: service,
ConsumerGroup: group,
})
}
for _, route := range relations.Route {
cartesianProduct = append(cartesianProduct, Rel{
Route: route,
ConsumerGroup: group,
})
}
}
} else {
cartesianProduct = make([]Rel, 0, lConsumerGroup)
for _, group := range relations.ConsumerGroup {
cartesianProduct = append(cartesianProduct, Rel{ConsumerGroup: group})
}
}
} else if l > 0 {
cartesianProduct = make([]Rel, 0, l)
for _, service := range relations.Service {
cartesianProduct = append(cartesianProduct, Rel{Service: service})
}
for _, route := range relations.Route {
cartesianProduct = append(cartesianProduct, Rel{Route: route})
}
}
return cartesianProduct
}