Skip to content

Commit

Permalink
*: Use for-range loops over iterator functions
Browse files Browse the repository at this point in the history
Possible with Go 1.23 which is min required since f95feb3.

Closes #2918.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Mar 10, 2025
1 parent c46273d commit b470836
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 140 deletions.
4 changes: 2 additions & 2 deletions cmd/internal/cmdprinter/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func PrettyPrintNodeInfo(cmd *cobra.Command, node netmap.NodeInfo,

cmd.Printf("%sNode %d: %s %s ", indent, index+1, hex.EncodeToString(node.PublicKey()), strState)

netmap.IterateNetworkEndpoints(node, func(endpoint string) {
for endpoint := range func(f func(string) bool) { node.IterateNetworkEndpoints(func(s string) bool { return !f(s) }) } {

Check warning on line 29 in cmd/internal/cmdprinter/netmap.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/cmdprinter/netmap.go#L29

Added line #L29 was not covered by tests
cmd.Printf("%s ", endpoint)
})
}

Check warning on line 31 in cmd/internal/cmdprinter/netmap.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/cmdprinter/netmap.go#L31

Added line #L31 was not covered by tests
cmd.Println()

if !short {
Expand Down
4 changes: 2 additions & 2 deletions cmd/neofs-cli/modules/netmap/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func prettyPrintNodeInfo(cmd *cobra.Command, i netmap.NodeInfo) {

cmd.Println("state:", stateWord)

netmap.IterateNetworkEndpoints(i, func(s string) {
for s := range func(f func(string) bool) { i.IterateNetworkEndpoints(func(s string) bool { return !f(s) }) } {

Check warning on line 72 in cmd/neofs-cli/modules/netmap/nodeinfo.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/netmap/nodeinfo.go#L72

Added line #L72 was not covered by tests
cmd.Println("address:", s)
})
}

Check warning on line 74 in cmd/neofs-cli/modules/netmap/nodeinfo.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-cli/modules/netmap/nodeinfo.go#L74

Added line #L74 was not covered by tests

i.IterateAttributes(func(key, value string) {
cmd.Printf("attribute: %s=%s\n", key, value)
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-node/config/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type stringAddressGroup []string

func (x stringAddressGroup) IterateAddresses(f func(string) bool) {
for i := range x {
if f(x[i]) {
if !f(x[i]) {
break
}
}
Expand Down
8 changes: 3 additions & 5 deletions cmd/neofs-node/config/node/config_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package nodeconfig

import (
"slices"
"testing"

"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test"
"github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -68,14 +68,12 @@ func TestNodeSection(t *testing.T) {

ind := 0

addrs.IterateAddresses(func(addr network.Address) bool {
for addr := range slices.Values(addrs) {
require.Equal(t, expectedAddr[ind].str, addr.String())
require.Equal(t, expectedAddr[ind].host, addr.URIAddr())

ind++

return false
})
}

require.Equal(t, true, relay)

Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-node/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func nodeKeyFromNetmap(c *cfg) []byte {

func (c *cfg) iterateNetworkAddresses(f func(string) bool) {
ni := c.cfgNodeInfo.localInfo
ni.IterateNetworkEndpoints(f)
ni.IterateNetworkEndpoints(func(s string) bool { return !f(s) })

Check warning on line 147 in cmd/neofs-node/netmap.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-node/netmap.go#L147

Added line #L147 was not covered by tests
}

func (c *cfg) addressNum() int {
Expand Down
14 changes: 7 additions & 7 deletions pkg/core/netmap/nodes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package netmap

import "github.com/nspcc-dev/neofs-sdk-go/netmap"
import (
"slices"

"github.com/nspcc-dev/neofs-sdk-go/netmap"
)

// Node is a named type of netmap.NodeInfo which provides interface needed
// in the current repository. Node is expected to be used everywhere instead
Expand All @@ -17,12 +21,8 @@ func (x Node) PublicKey() []byte {
// IterateAddresses iterates over all announced network addresses
// and passes them into f. Handler MUST NOT be nil.
func (x Node) IterateAddresses(f func(string) bool) {
(netmap.NodeInfo)(x).IterateNetworkEndpoints(f)
for _, addr := range (netmap.NodeInfo)(x).ExternalAddresses() {
if f(addr) {
return
}
}
(netmap.NodeInfo)(x).IterateNetworkEndpoints(func(s string) bool { return !f(s) })
slices.Values((netmap.NodeInfo)(x).ExternalAddresses())(f)

Check warning on line 25 in pkg/core/netmap/nodes.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/netmap/nodes.go#L24-L25

Added lines #L24 - L25 were not covered by tests
}

// NumberOfAddresses returns number of announced network addresses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,25 @@ func (v Validator) Verify(nodeInfo netmap.NodeInfo) error {
var results []*client.ResEndpointInfo
var err error

nodeInfo.IterateNetworkEndpoints(func(s string) bool {
for s := range func(f func(string) bool) { nodeInfo.IterateNetworkEndpoints(func(s string) bool { return !f(s) }) } {

Check warning on line 26 in pkg/innerring/processors/netmap/nodevalidation/availability/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/nodevalidation/availability/validator.go#L26

Added line #L26 was not covered by tests
var res *client.ResEndpointInfo
var c *client.Client

c, err = createSDKClient(s)
if err != nil {
err = fmt.Errorf("'%s': client creation: %w", s, err)
return true
return fmt.Errorf("'%s': client creation: %w", s, err)

Check warning on line 32 in pkg/innerring/processors/netmap/nodevalidation/availability/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/nodevalidation/availability/validator.go#L32

Added line #L32 was not covered by tests
}
defer func() {
_ = c.Close()
}()

timeoutContext, cancel := context.WithTimeout(context.Background(), pingTimeout)
defer cancel()

res, err = c.EndpointInfo(timeoutContext, client.PrmEndpointInfo{})
cancel()
_ = c.Close()

Check warning on line 39 in pkg/innerring/processors/netmap/nodevalidation/availability/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/nodevalidation/availability/validator.go#L38-L39

Added lines #L38 - L39 were not covered by tests
if err != nil {
err = fmt.Errorf("'%s': could not ping node with `EndpointInfo`: %w", s, err)
return true
return fmt.Errorf("'%s': could not ping node with `EndpointInfo`: %w", s, err)

Check warning on line 41 in pkg/innerring/processors/netmap/nodevalidation/availability/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/nodevalidation/availability/validator.go#L41

Added line #L41 was not covered by tests
}

results = append(results, res)
return false
})
if err != nil {
return err
}

for _, res := range results {
Expand Down Expand Up @@ -108,21 +100,14 @@ func compareNodeInfos(niExp, niGot netmap.NodeInfo) error {
}

expAddrM := make(map[string]struct{}, niExp.NumberOfAttributes())
niExp.IterateNetworkEndpoints(func(s string) bool {
for s := range func(f func(string) bool) { niExp.IterateNetworkEndpoints(func(s string) bool { return !f(s) }) } {

Check warning on line 103 in pkg/innerring/processors/netmap/nodevalidation/availability/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/nodevalidation/availability/validator.go#L103

Added line #L103 was not covered by tests
expAddrM[s] = struct{}{}
return false
})
}

Check warning on line 105 in pkg/innerring/processors/netmap/nodevalidation/availability/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/nodevalidation/availability/validator.go#L105

Added line #L105 was not covered by tests

niGot.IterateNetworkEndpoints(func(s string) bool {
for s := range func(f func(string) bool) { niGot.IterateNetworkEndpoints(func(s string) bool { return !f(s) }) } {

Check warning on line 107 in pkg/innerring/processors/netmap/nodevalidation/availability/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/nodevalidation/availability/validator.go#L107

Added line #L107 was not covered by tests
if _, ok := expAddrM[s]; !ok {
err = fmt.Errorf("got unexpected address: %s", s)
return true
return fmt.Errorf("got unexpected address: %s", s)

Check warning on line 109 in pkg/innerring/processors/netmap/nodevalidation/availability/validator.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/processors/netmap/nodevalidation/availability/validator.go#L109

Added line #L109 was not covered by tests
}

return false
})
if err != nil {
return err
}

return nil
Expand Down
6 changes: 2 additions & 4 deletions pkg/morph/client/netmap/add_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package netmap

import (
"fmt"
"slices"

"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
netmaprpc "github.com/nspcc-dev/neofs-contract/rpc/netmap"
Expand All @@ -27,10 +28,7 @@ func (c *Client) AddPeer(ni netmap.NodeInfo, pkey *keys.PublicKey) error {
Key: pkey,
State: netmaprpc.NodeStateOnline,
}
ni.IterateNetworkEndpoints(func(addr string) bool {
node.Addresses = append(node.Addresses, addr)
return false
})
node.Addresses = slices.Collect(func(f func(s string) bool) { ni.IterateNetworkEndpoints(func(s string) bool { return !f(s) }) })

Check warning on line 31 in pkg/morph/client/netmap/add_peer.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/client/netmap/add_peer.go#L31

Added line #L31 was not covered by tests
ni.IterateAttributes(func(k, v string) {
node.Attributes[k] = v
})
Expand Down
17 changes: 10 additions & 7 deletions pkg/network/cache/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -100,10 +101,9 @@ func (x *multiClient) createForAddress(addr network.Address) (clientcore.Client,
func (x *multiClient) updateGroup(group network.AddressGroup) {
// Firstly, remove old clients.
cache := make([]string, 0, group.Len())
group.IterateAddresses(func(a network.Address) bool {
for a := range slices.Values(group) {

Check warning on line 104 in pkg/network/cache/multi.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/cache/multi.go#L104

Added line #L104 was not covered by tests
cache = append(cache, a.String())
return false
})
}

Check warning on line 106 in pkg/network/cache/multi.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/cache/multi.go#L106

Added line #L106 was not covered by tests

x.addrMtx.RLock()
oldGroup := x.addr
Expand Down Expand Up @@ -148,11 +148,12 @@ func (x *multiClient) iterateClients(ctx context.Context, f func(clientcore.Clie
group := x.addr
x.addrMtx.RUnlock()

group.IterateAddresses(func(addr network.Address) bool {
loop:
for addr := range slices.Values(group) {

Check warning on line 152 in pkg/network/cache/multi.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/cache/multi.go#L151-L152

Added lines #L151 - L152 were not covered by tests
select {
case <-ctx.Done():
firstErr = context.Canceled
return true
break loop

Check warning on line 156 in pkg/network/cache/multi.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/cache/multi.go#L156

Added line #L156 was not covered by tests
default:
}

Expand All @@ -178,8 +179,10 @@ func (x *multiClient) iterateClients(ctx context.Context, f func(clientcore.Clie
x.ReportError(err)
}

return success
})
if success {
break

Check warning on line 183 in pkg/network/cache/multi.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/cache/multi.go#L182-L183

Added lines #L182 - L183 were not covered by tests
}
}

return firstErr
}
Expand Down
52 changes: 14 additions & 38 deletions pkg/network/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,11 @@ type AddressGroup []Address
func StringifyGroup(x AddressGroup) string {
var s string

iterateAllAddresses(x, func(addr Address) {
for addr := range slices.Values(x) {

Check warning on line 24 in pkg/network/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/group.go#L24

Added line #L24 was not covered by tests
s += addr.String()
})

return s
}

// IterateAddresses iterates over all network addresses of the node.
//
// Breaks iterating on handler's true return.
//
// Handler should not be nil.
func (x AddressGroup) IterateAddresses(f func(Address) bool) {
for i := range x {
if f(x[i]) {
break
}
}
}

// 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
})
return s

Check warning on line 28 in pkg/network/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/group.go#L28

Added line #L28 was not covered by tests
}

// Len returns number of addresses in AddressGroup.
Expand All @@ -69,7 +47,7 @@ func (x AddressGroup) Swap(i, j int) {
// MultiAddressIterator is an interface of network address group.
type MultiAddressIterator interface {
// IterateAddresses must iterate over network addresses and pass each one
// to the handler until it returns true.
// to the handler until it returns false.
IterateAddresses(func(string) bool)

// NumberOfAddresses must return number of addresses in group.
Expand Down Expand Up @@ -125,33 +103,31 @@ func (x *AddressGroup) FromIterator(iter MultiAddressIterator) error {

// iterateParsedAddresses parses each address from MultiAddressIterator and passes it to f
// until 1st parsing failure or f's error.
func iterateParsedAddresses(iter MultiAddressIterator, f func(s Address) error) (err error) {
iter.IterateAddresses(func(s string) bool {
func iterateParsedAddresses(iter MultiAddressIterator, f func(s Address) error) error {
for s := range iter.IterateAddresses {
var a Address

err = a.FromString(s)
err := a.FromString(s)
if err != nil {
err = fmt.Errorf("could not parse address from string: %w", err)
return true
return fmt.Errorf("could not parse address from string: %w", err)
}

err = f(a)

return err != nil
})

return
if err = f(a); err != nil {
return err
}
}
return nil
}

// WriteToNodeInfo writes AddressGroup to netmap.NodeInfo structure.
func WriteToNodeInfo(g AddressGroup, ni *netmap.NodeInfo) {
num := g.Len()
addrs := make([]string, 0, num)

iterateAllAddresses(g, func(addr Address) {
for addr := range slices.Values(g) {

Check warning on line 127 in pkg/network/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/group.go#L127

Added line #L127 was not covered by tests
ni.SetNetworkEndpoints()
addrs = append(addrs, addr.String())
})
}

Check warning on line 130 in pkg/network/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/network/group.go#L130

Added line #L130 was not covered by tests

ni.SetNetworkEndpoints(addrs...)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
type NodeEndpointsIterator netmap.NodeInfo

func (x NodeEndpointsIterator) IterateAddresses(f func(string) bool) {
(netmap.NodeInfo)(x).IterateNetworkEndpoints(f)
(netmap.NodeInfo)(x).IterateNetworkEndpoints(func(s string) bool { return !f(s) })
}

func (x NodeEndpointsIterator) NumberOfAddresses() int {
Expand Down
Loading

0 comments on commit b470836

Please sign in to comment.