forked from calibrae-project/spawn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize.go
44 lines (39 loc) · 1.14 KB
/
normalize.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
package normalize
import (
"net"
)
// address returns addr with the passed default port appended if there is not
// already a port specified.
func address(addr, defaultPort string) string {
var e error
if _, _, e = net.SplitHostPort(addr); E.Chk(e) {
return net.JoinHostPort(addr, defaultPort)
}
return addr
}
// Addresses returns a new slice with all the passed peer addresses normalized
// with the given default port, and all duplicates removed.
func Addresses(addrs []string, defaultPort string) []string {
for i := range addrs {
addrs[i] = address(addrs[i], defaultPort)
}
return removeDuplicateAddresses(addrs)
}
// removeDuplicateAddresses returns a new slice with all duplicate entries in
// addrs removed.
func removeDuplicateAddresses(addrs []string) (result []string) {
result = make([]string, 0, len(addrs))
seen := map[string]struct{}{}
for _, val := range addrs {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = struct{}{}
}
}
return result
}
// StringSliceAddresses normalizes a slice of addresses
func StringSliceAddresses(a []string, port string) {
variable := a
a = Addresses(variable, port)
}