-
Notifications
You must be signed in to change notification settings - Fork 792
/
address.go
64 lines (52 loc) · 1.33 KB
/
address.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
package kafka
import (
"net"
"strings"
)
// TCP constructs an address with the network set to "tcp".
func TCP(address ...string) net.Addr { return makeNetAddr("tcp", address) }
func makeNetAddr(network string, addresses []string) net.Addr {
switch len(addresses) {
case 0:
return nil // maybe panic instead?
case 1:
return makeAddr(network, addresses[0])
default:
return makeMultiAddr(network, addresses)
}
}
func makeAddr(network, address string) net.Addr {
return &networkAddress{
network: network,
address: canonicalAddress(address),
}
}
func makeMultiAddr(network string, addresses []string) net.Addr {
multi := make(multiAddr, len(addresses))
for i, address := range addresses {
multi[i] = makeAddr(network, address)
}
return multi
}
type networkAddress struct {
network string
address string
}
func (a *networkAddress) Network() string { return a.network }
func (a *networkAddress) String() string { return a.address }
type multiAddr []net.Addr
func (m multiAddr) Network() string { return m.join(net.Addr.Network) }
func (m multiAddr) String() string { return m.join(net.Addr.String) }
func (m multiAddr) join(f func(net.Addr) string) string {
switch len(m) {
case 0:
return ""
case 1:
return f(m[0])
}
s := make([]string, len(m))
for i, a := range m {
s[i] = f(a)
}
return strings.Join(s, ",")
}