github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/address.go (about)

     1  package kafka
     2  
     3  import (
     4  	"net"
     5  	"strings"
     6  )
     7  
     8  // TCP constructs an address with the network set to "tcp".
     9  func TCP(address ...string) net.Addr { return makeNetAddr("tcp", address) }
    10  
    11  func makeNetAddr(network string, addresses []string) net.Addr {
    12  	switch len(addresses) {
    13  	case 0:
    14  		return nil // maybe panic instead?
    15  	case 1:
    16  		return makeAddr(network, addresses[0])
    17  	default:
    18  		return makeMultiAddr(network, addresses)
    19  	}
    20  }
    21  
    22  func makeAddr(network, address string) net.Addr {
    23  	return &networkAddress{
    24  		network: network,
    25  		address: canonicalAddress(address),
    26  	}
    27  }
    28  
    29  func makeMultiAddr(network string, addresses []string) net.Addr {
    30  	multi := make(multiAddr, len(addresses))
    31  	for i, address := range addresses {
    32  		multi[i] = makeAddr(network, address)
    33  	}
    34  	return multi
    35  }
    36  
    37  type networkAddress struct {
    38  	network string
    39  	address string
    40  }
    41  
    42  func (a *networkAddress) Network() string { return a.network }
    43  
    44  func (a *networkAddress) String() string { return a.address }
    45  
    46  type multiAddr []net.Addr
    47  
    48  func (m multiAddr) Network() string { return m.join(net.Addr.Network) }
    49  
    50  func (m multiAddr) String() string { return m.join(net.Addr.String) }
    51  
    52  func (m multiAddr) join(f func(net.Addr) string) string {
    53  	switch len(m) {
    54  	case 0:
    55  		return ""
    56  	case 1:
    57  		return f(m[0])
    58  	}
    59  	s := make([]string, len(m))
    60  	for i, a := range m {
    61  		s[i] = f(a)
    62  	}
    63  	return strings.Join(s, ",")
    64  }