Skip to content

Commit

Permalink
refactor: using slices.Contains to simplify the code
Browse files Browse the repository at this point in the history
Signed-off-by: damuzhi0810 <[email protected]>
  • Loading branch information
damuzhi0810 committed Feb 5, 2025
1 parent a81d54f commit 00e5073
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 73 deletions.
9 changes: 2 additions & 7 deletions cmd/algons/dnsCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"os"
"regexp"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -439,13 +440,7 @@ func checkedDelete(toDelete []cloudflare.DNSRecordResponseEntry, cloudflareDNS *

func getEntries(getNetwork string, recordType string) ([]cloudflare.DNSRecordResponseEntry, error) {
recordTypes := []string{"A", "CNAME", "SRV", "TXT"}
isKnown := false
for _, known := range append(recordTypes, "") {
if recordType == known {
isKnown = true
break
}
}
isKnown := slices.Contains(recordTypes, recordType) || recordType == ""
if !isKnown {
return nil, fmt.Errorf("invalid recordType specified %s", recordType)
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/goal/accountsList.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"os/user"
"path/filepath"
"slices"
"strings"

"github.com/algorand/go-algorand/daemon/algod/api/server/v2/generated/model"
Expand Down Expand Up @@ -101,12 +102,7 @@ func (accountList *AccountsList) setDefault(accountName string) {

// isTaken checks if the account friendly name is already being used by another account
func (accountList *AccountsList) isTaken(accountName string) bool {
for _, name := range accountList.Accounts {
if name == accountName {
return true
}
}
return false
return slices.Contains(accountList.Accounts, accountName)
}

// rename renames account's friendly name
Expand Down
12 changes: 4 additions & 8 deletions data/transactions/logic/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -5220,10 +5220,8 @@ func (cx *EvalContext) assignAsset(sv stackValue) (basics.AssetIndex, error) {
// an inner static array.
func (cx *EvalContext) availableAsset(aid basics.AssetIndex) bool {
// Ensure that aid is in Foreign Assets
for _, assetID := range cx.txn.Txn.ForeignAssets {
if assetID == aid {
return true
}
if slices.Contains(cx.txn.Txn.ForeignAssets, aid) {
return true
}
// or was created in group
if cx.version >= createdResourcesVersion {
Expand Down Expand Up @@ -5264,10 +5262,8 @@ func (cx *EvalContext) assignApp(sv stackValue) (basics.AppIndex, error) {

func (cx *EvalContext) availableApp(aid basics.AppIndex) bool {
// Ensure that aid is in Foreign Apps
for _, appID := range cx.txn.Txn.ForeignApps {
if appID == aid {
return true
}
if slices.Contains(cx.txn.Txn.ForeignApps, aid) {
return true
}
// or was created in group
if cx.version >= createdResourcesVersion {
Expand Down
8 changes: 2 additions & 6 deletions network/requestTracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"net/http"
"net/textproto"
"slices"
"sort"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -519,10 +520,5 @@ func (rt *RequestTracker) getForwardedConnectionAddress(header http.Header) (ip

// isLocalhost returns true if the given host is a localhost address.
func isLocalhost(host string) bool {
for _, v := range []string{"localhost", "127.0.0.1", "[::1]", "::1", "[::]"} {
if host == v {
return true
}
}
return false
return slices.Contains([]string{"localhost", "127.0.0.1", "[::1]", "::1", "[::]"}, host)
}
15 changes: 6 additions & 9 deletions network/wsNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"path"
"regexp"
"runtime"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -916,19 +917,15 @@ func (wn *WebsocketNetwork) checkProtocolVersionMatch(otherHeaders http.Header)
otherAcceptedVersions := otherHeaders[textproto.CanonicalMIMEHeaderKey(ProtocolAcceptVersionHeader)]
for _, otherAcceptedVersion := range otherAcceptedVersions {
// do we have a matching version ?
for _, supportedProtocolVersion := range wn.supportedProtocolVersions {
if supportedProtocolVersion == otherAcceptedVersion {
matchingVersion = supportedProtocolVersion
return matchingVersion, ""
}
if slices.Contains(wn.supportedProtocolVersions, otherAcceptedVersion) {
matchingVersion = otherAcceptedVersion
return matchingVersion, ""
}
}

otherVersion = otherHeaders.Get(ProtocolVersionHeader)
for _, supportedProtocolVersion := range wn.supportedProtocolVersions {
if supportedProtocolVersion == otherVersion {
return supportedProtocolVersion, otherVersion
}
if slices.Contains(wn.supportedProtocolVersions, otherVersion) {
return otherVersion, otherVersion
}

return "", filterASCII(otherVersion)
Expand Down
9 changes: 2 additions & 7 deletions network/wsPeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net"
"net/http"
"runtime"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -52,13 +53,7 @@ const averageMessageLength = 2 * 1024 // Most of the messages are smaller tha
const msgsInReadBufferPerPeer = 10

func init() {
matched := false
for _, version := range SupportedProtocolVersions {
if version == versionPeerFeatures {
matched = true
}
}
if !matched {
if !slices.Contains(SupportedProtocolVersions, versionPeerFeatures) {
panic(fmt.Sprintf("peer features version %s is not supported %v", versionPeerFeatures, SupportedProtocolVersions))
}

Expand Down
7 changes: 3 additions & 4 deletions shared/pingpong/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/rand"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -1003,10 +1004,8 @@ func (pps *WorkerState) generateAccounts() {
}

func uniqueAppend(they []string, x string) []string {
for _, v := range they {
if v == x {
return they
}
if slices.Contains(they, x) {
return they
}
return append(they, x)
}
10 changes: 2 additions & 8 deletions shared/pingpong/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"os"
"slices"
"time"

"github.com/algorand/go-algorand/util/codecs"
Expand Down Expand Up @@ -192,14 +193,7 @@ var accountSampleMethods = []string{

// Check returns an error if config is invalid.
func (cfg *PpConfig) Check() error {
sampleOk := false
for _, v := range accountSampleMethods {
if v == cfg.GeneratedAccountSampleMethod {
sampleOk = true
break
}
}
if !sampleOk {
if !slices.Contains(accountSampleMethods, cfg.GeneratedAccountSampleMethod) {
return fmt.Errorf("unknown GeneratedAccountSampleMethod: %s", cfg.GeneratedAccountSampleMethod)
}
if cfg.DeterministicKeys && (cfg.GeneratedAccountsOffset+uint64(cfg.NumPartAccounts) > cfg.GeneratedAccountsCount) {
Expand Down
10 changes: 2 additions & 8 deletions tools/block-generator/generator/generator_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/binary"
"fmt"
"os"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -333,14 +334,7 @@ func (g *generator) introspectLedgerVsGenerator(roundNumber, intra uint64) (errs
generatorExpectedOptinsNotFound := map[uint64][]uint64{}
for appId, appOptins := range expectedOptins[appKindBoxes] {
for optin := range appOptins {
missing := true
for _, boxOptin := range ledgerBoxEvidence[appId] {
if boxOptin == optin {
missing = false
break
}
}
if missing {
if !slices.Contains(ledgerBoxEvidence[appId], optin) {
generatorExpectedOptinsNotFound[appId] = append(generatorExpectedOptinsNotFound[appId], optin)
}
}
Expand Down
12 changes: 2 additions & 10 deletions util/codecs/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"os"
"reflect"
"slices"
"strings"
)

Expand Down Expand Up @@ -123,7 +124,7 @@ func WriteNonDefaultValues(writer io.Writer, object, defaultObject interface{},
return fmt.Errorf("error processing serialized object - should be at EOF: %s", line)
}

if inStringArray(valName, ignore) {
if slices.Contains(ignore, valName) {
newFile[newIndex] = line
newIndex++
continue
Expand Down Expand Up @@ -183,15 +184,6 @@ func extractValueName(line string) (name string) {
return line[start+1 : end]
}

func inStringArray(item string, set []string) bool {
for _, s := range set {
if item == s {
return true
}
}
return false
}

func createValueMap(object interface{}) map[string]interface{} {
valueMap := make(map[string]interface{})

Expand Down

0 comments on commit 00e5073

Please sign in to comment.