[#607] network: Add helper function to iterate over all addresses

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-23 12:57:03 +03:00 committed by Leonard Lyubich
parent 119031c8c7
commit cbe20a2bac

View file

@ -19,9 +19,8 @@ type AddressGroup []Address
func StringifyGroup(x AddressGroup) string { func StringifyGroup(x AddressGroup) string {
var s string var s string
x.IterateAddresses(func(addr Address) bool { iterateAllAddresses(x, func(addr Address) {
s += addr.String() s += addr.String()
return false
}) })
return s return s
@ -40,6 +39,15 @@ func (x AddressGroup) IterateAddresses(f func(Address) bool) {
} }
} }
// iterateAllAddresses iterates over all network addresses of g
// and passes each of them to f.
func iterateAllAddresses(g AddressGroup, f func(Address)) {
g.IterateAddresses(func(addr Address) bool {
f(addr)
return false
})
}
// Len returns number of addresses in AddressGroup. // Len returns number of addresses in AddressGroup.
func (x AddressGroup) Len() int { func (x AddressGroup) Len() int {
return len(x) return len(x)
@ -111,9 +119,8 @@ func WriteToNodeInfo(g AddressGroup, ni *netmap.NodeInfo) {
num := g.Len() num := g.Len()
addrs := make([]string, 0, num) addrs := make([]string, 0, num)
g.IterateAddresses(func(addr Address) bool { iterateAllAddresses(g, func(addr Address) {
addrs = append(addrs, addr.String()) addrs = append(addrs, addr.String())
return false
}) })
ni.SetAddresses(addrs...) ni.SetAddresses(addrs...)