Skip to content

Commit 6046d4c

Browse files
authored
refactor: use slices.Contains to simplify code (#123)
Signed-off-by: pennylees <techdashen@msn.com>
1 parent 4da2f60 commit 6046d4c

6 files changed

Lines changed: 19 additions & 46 deletions

File tree

pkg/bertyvcissuer/verifiable_public_key_fetcher.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bertyvcissuer
33
import (
44
"crypto/ed25519"
55
"fmt"
6+
"slices"
67
"strings"
78

89
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/verifier"
@@ -18,13 +19,7 @@ func embeddedPublicKeyFetcher(issuerID string, allowList []string) (*verifier.Pu
1819
}
1920

2021
if len(allowList) > 0 {
21-
found := false
22-
for _, allowed := range allowList {
23-
if allowed == issuerID {
24-
found = true
25-
break
26-
}
27-
}
22+
found := slices.Contains(allowList, issuerID)
2823

2924
if !found {
3025
return nil, errcode.ErrCode_ErrServicesDirectoryInvalidVerifiedCredentialID.Wrap(fmt.Errorf("issuer is not allowed"))

pkg/errcode/error.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package errcode
33
import (
44
"fmt"
55
"io"
6+
"slices"
67

78
"golang.org/x/xerrors"
89
"google.golang.org/grpc/codes"
@@ -43,12 +44,7 @@ func Codes(err error) []ErrCode {
4344
// Has returns true if one of the error is or contains (wraps) an expected errcode
4445
func Has(err error, code WithCode) bool {
4546
codeCode := code.Code()
46-
for _, otherCode := range Codes(err) {
47-
if otherCode == codeCode {
48-
return true
49-
}
50-
}
51-
return false
47+
return slices.Contains(Codes(err), codeCode)
5248
}
5349

5450
// Is returns true if the top-level error (it doesn't unwrap it) is actually an ErrCode of the same value

pkg/tinder/driver_localdiscovery.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"fmt"
88
"math/rand"
9+
"slices"
910
"sync"
1011
"time"
1112

@@ -449,11 +450,8 @@ func (ld *LocalDiscovery) monitorConnection(ctx context.Context) error {
449450
ld.handleConnection(ctx, evt.Peer)
450451
}
451452
case event.EvtPeerProtocolsUpdated:
452-
for _, added := range evt.Added {
453-
if added == recProtocolID {
454-
ld.handleConnection(ctx, evt.Peer)
455-
break
456-
}
453+
if slices.Contains(evt.Added, recProtocolID) {
454+
ld.handleConnection(ctx, evt.Peer)
457455
}
458456
}
459457
}

pkg/tinder/driver_mock.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tinder
33
import (
44
"context"
55
"fmt"
6+
"slices"
67
"sync"
78
"time"
89

@@ -130,10 +131,8 @@ func (s *MockDriverServer) WaitForPeer(topic string, p peer.ID, timeout time.Dur
130131
}
131132

132133
// send peers
133-
for _, upeer := range updated {
134-
if upeer == p {
135-
return nil
136-
}
134+
if slices.Contains(updated, p) {
135+
return nil
137136
}
138137
}
139138
}

pkg/tyber/format.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"slices"
78

89
"go.uber.org/zap"
910
"go.uber.org/zap/zapcore"
@@ -52,12 +53,7 @@ const (
5253
var KnownLogTypes = []LogType{TraceType, StepType, EventType, SubscribeType}
5354

5455
func (lt LogType) IsKnown() bool {
55-
for _, vlt := range KnownLogTypes {
56-
if lt == vlt {
57-
return true
58-
}
59-
}
60-
return false
56+
return slices.Contains(KnownLogTypes, lt)
6157
}
6258

6359
type StatusType string

store_metadata.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/base64"
77
"fmt"
88
"io"
9+
"slices"
910
"strings"
1011

1112
coreiface "github.com/ipfs/kubo/core/coreiface"
@@ -427,13 +428,7 @@ func (m *MetadataStore) ListContactsByStatus(states ...protocoltypes.ContactStat
427428
contacts := []*protocoltypes.ShareableContact(nil)
428429

429430
for _, c := range idx.contacts {
430-
hasState := false
431-
for _, s := range states {
432-
if c.state == s {
433-
hasState = true
434-
break
435-
}
436-
}
431+
hasState := slices.Contains(states, c.state)
437432

438433
if hasState {
439434
contacts = append(contacts, c.contact)
@@ -876,13 +871,7 @@ func (m *MetadataStore) getContactStatus(pk crypto.PubKey) protocoltypes.Contact
876871
func (m *MetadataStore) checkContactStatus(pk crypto.PubKey, states ...protocoltypes.ContactState) bool {
877872
contactStatus := m.getContactStatus(pk)
878873

879-
for _, s := range states {
880-
if contactStatus == s {
881-
return true
882-
}
883-
}
884-
885-
return false
874+
return slices.Contains(states, contactStatus)
886875
}
887876

888877
type EventMetadataReceived struct {
@@ -1027,19 +1016,19 @@ func constructorFactoryGroupMetadata(s *WeshOrbitDB, logger *zap.Logger) iface.S
10271016

10281017
func (m *MetadataStore) initEmitter() (err error) {
10291018
if m.emitters.metadataReceived, err = m.eventBus.Emitter(new(EventMetadataReceived)); err != nil {
1030-
return
1019+
return err
10311020
}
10321021

10331022
if m.emitters.groupMetadata, err = m.eventBus.Emitter(new(*protocoltypes.GroupMetadataEvent)); err != nil {
1034-
return
1023+
return err
10351024
}
10361025

1037-
return
1026+
return err
10381027
}
10391028

10401029
func genNewSeed() (seed []byte, err error) {
10411030
seed, err = io.ReadAll(io.LimitReader(crand.Reader, protocoltypes.RendezvousSeedLength))
1042-
return
1031+
return seed, err
10431032
}
10441033

10451034
func (m *MetadataStore) Close() error {

0 commit comments

Comments
 (0)