Skip to content

Commit eea11e9

Browse files
quantpoetCoderZhi
andauthored
refactor: use slices.Contains to simplify code (#4719)
Signed-off-by: quantpoet <[email protected]> Co-authored-by: CoderZhi <[email protected]>
1 parent 3ee3f92 commit eea11e9

File tree

7 files changed

+18
-46
lines changed

7 files changed

+18
-46
lines changed

api/logfilter/logfilter.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package logfilter
22

33
import (
44
"bytes"
5+
"slices"
56

67
"github.com/iotexproject/go-pkgs/bloom"
78
"github.com/iotexproject/iotex-proto/golang/iotexapi"
@@ -57,11 +58,8 @@ func (l *LogFilter) MatchLogs(receipts []*action.Receipt) []*action.Log {
5758
func (l *LogFilter) match(log *iotextypes.Log) bool {
5859
addrMatch := len(l.pbFilter.Address) == 0
5960
if !addrMatch {
60-
for _, e := range l.pbFilter.Address {
61-
if e == log.ContractAddress {
62-
addrMatch = true
63-
break
64-
}
61+
if slices.Contains(l.pbFilter.Address, log.ContractAddress) {
62+
addrMatch = true
6563
}
6664
}
6765
if !addrMatch {
@@ -105,10 +103,8 @@ func (l *LogFilter) ExistInBloomFilter(bf bloom.BloomFilter) bool {
105103
continue
106104
}
107105

108-
for _, v := range e.Topic {
109-
if bf.Exist(v) {
110-
return true
111-
}
106+
if slices.ContainsFunc(e.Topic, bf.Exist) {
107+
return true
112108
}
113109
}
114110
// {} or nil matches any address or topic list

blockchain/config.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package blockchain
88
import (
99
"crypto/ecdsa"
1010
"os"
11+
"slices"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -263,11 +264,5 @@ func (cfg *Config) whitelistSignatureScheme(sk crypto.PrivateKey) bool {
263264
if sigScheme == "" {
264265
return false
265266
}
266-
for _, e := range cfg.SignatureScheme {
267-
if sigScheme == e {
268-
// signature scheme is whitelisted
269-
return true
270-
}
271-
}
272-
return false
267+
return slices.Contains(cfg.SignatureScheme, sigScheme)
273268
}

consensus/scheme/rolldpos/roundcalculator.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package rolldpos
77

88
import (
9+
"slices"
910
"time"
1011

1112
"github.com/pkg/errors"
@@ -118,13 +119,7 @@ func (c *roundCalculator) IsDelegate(addr string, height uint64) bool {
118119
log.L().Warn("Failed to get delegates", zap.Error(err))
119120
return false
120121
}
121-
for _, d := range delegates {
122-
if addr == d {
123-
return true
124-
}
125-
}
126-
127-
return false
122+
return slices.Contains(delegates, addr)
128123
}
129124

130125
// RoundInfo returns information of round by the given height and current time

consensus/scheme/rolldpos/roundctx.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package rolldpos
77

88
import (
9+
"slices"
910
"time"
1011

1112
"github.com/pkg/errors"
@@ -110,13 +111,7 @@ func (ctx *roundCtx) Proposers() []string {
110111
}
111112

112113
func (ctx *roundCtx) IsDelegate(addr string) bool {
113-
for _, d := range ctx.delegates {
114-
if addr == d {
115-
return true
116-
}
117-
}
118-
119-
return false
114+
return slices.Contains(ctx.delegates, addr)
120115
}
121116

122117
func (ctx *roundCtx) Block(blkHash []byte) *block.Block {

ioctl/config/configsetget.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111
"regexp"
12+
"slices"
1213
"strconv"
1314
"strings"
1415

@@ -226,12 +227,7 @@ func isValidEndpoint(endpoint string) bool {
226227

227228
// isValidExplorer checks if the explorer is a valid option
228229
func isValidExplorer(arg string) bool {
229-
for _, exp := range _validExpl {
230-
if arg == exp {
231-
return true
232-
}
233-
}
234-
return false
230+
return slices.Contains(_validExpl, arg)
235231
}
236232

237233
// isSupportedLanguage checks if the language is a supported option and returns index when supported

ioctl/newcmd/config/config.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313
"regexp"
14+
"slices"
1415
"strconv"
1516
"strings"
1617

@@ -352,12 +353,7 @@ func isValidEndpoint(endpoint string) bool {
352353

353354
// isValidExplorer checks if the explorer is a valid option
354355
func isValidExplorer(arg string) bool {
355-
for _, exp := range _validExpl {
356-
if arg == exp {
357-
return true
358-
}
359-
}
360-
return false
356+
return slices.Contains(_validExpl, arg)
361357
}
362358

363359
// writeConfig writes to config file

ioctl/util/util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"os/signal"
1515
"path/filepath"
16+
"slices"
1617
"strconv"
1718
"strings"
1819
"syscall"
@@ -189,10 +190,8 @@ func JwtAuth() (jwt metadata.MD, err error) {
189190
// CheckArgs used for check ioctl cmd arg(s)'s num
190191
func CheckArgs(validNum ...int) cobra.PositionalArgs {
191192
return func(cmd *cobra.Command, args []string) error {
192-
for _, n := range validNum {
193-
if len(args) == n {
194-
return nil
195-
}
193+
if slices.Contains(validNum, len(args)) {
194+
return nil
196195
}
197196
nums := strings.Replace(strings.Trim(fmt.Sprint(validNum), "[]"), " ", " or ", -1)
198197
return fmt.Errorf("accepts "+nums+" arg(s), received %d", len(args))

0 commit comments

Comments
 (0)