Skip to content

refactor: use slices.Contains to simplify code #1685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions internal/reference/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strings"

"github.com/hyperledger/firefly-common/pkg/fftypes"
Expand Down Expand Up @@ -892,17 +893,11 @@ func writeStructFields(ctx context.Context, t reflect.Type, rootPageNames, simpl

fieldInRootPages := false
fieldInSimpleTypes := false
for _, rootPageName := range rootPageNames {
if strings.ToLower(fieldType.Name()) == rootPageName {
fieldInRootPages = true
break
}
if slices.Contains(rootPageNames, strings.ToLower(fieldType.Name())) {
fieldInRootPages = true
}
for _, simpleTypeName := range simpleTypeNames {
if strings.ToLower(fieldType.Name()) == simpleTypeName {
fieldInSimpleTypes = true
break
}
if slices.Contains(simpleTypeNames, strings.ToLower(fieldType.Name())) {
fieldInSimpleTypes = true
}

link := ""
Expand All @@ -920,13 +915,7 @@ func writeStructFields(ctx context.Context, t reflect.Type, rootPageNames, simpl
fireflyType = fmt.Sprintf("[%s](%s)", fireflyType, link)

// Generate the table for the sub type
tableAlreadyGenerated := false
for _, tableName := range generatedTableNames {
if strings.ToLower(fieldType.Name()) == tableName {
tableAlreadyGenerated = true
break
}
}
tableAlreadyGenerated := slices.Contains(generatedTableNames, strings.ToLower(fieldType.Name()))
if isStruct && !tableAlreadyGenerated && !fieldInRootPages && !fieldInSimpleTypes {
subFieldBuff.WriteString(fmt.Sprintf("## %s\n\n", fieldType.Name()))
subFieldMarkdown, newTableNames, _ := generateObjectReferenceMarkdown(ctx, false, nil, fieldType, rootPageNames, simpleTypeNames, generatedTableNames, outputPath)
Expand Down
25 changes: 4 additions & 21 deletions internal/spievents/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"io"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -66,36 +67,18 @@ func newWebSocket(ae *adminEventManager, wsConn *websocket.Conn) *webSocket {
}

func (wc *webSocket) eventMatches(changeEvent *core.ChangeEvent) bool {
collectionMatches := false
for _, c := range wc.collections {
if c == changeEvent.Collection {
collectionMatches = true
break
}
}
collectionMatches := slices.Contains(wc.collections, changeEvent.Collection)
if !collectionMatches {
return false
}
if len(wc.filter.Namespaces) > 0 {
namespaceMatches := false
for _, ns := range wc.filter.Namespaces {
if ns == changeEvent.Namespace {
namespaceMatches = true
break
}
}
namespaceMatches := slices.Contains(wc.filter.Namespaces, changeEvent.Namespace)
if !namespaceMatches {
return false
}
}
if len(wc.filter.Types) > 0 {
typeMatches := false
for _, t := range wc.filter.Types {
if t == changeEvent.Type {
typeMatches = true
break
}
}
typeMatches := slices.Contains(wc.filter.Types, changeEvent.Type)
if !typeMatches {
return false
}
Expand Down