Skip to content

Commit

Permalink
chore: linter update, and CI action fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
xmichelo committed Feb 29, 2024
1 parent 0f3245e commit f49fd22
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 36 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ jobs:
- name: Get sources
uses: actions/checkout@v3

- name: Set up Go 1.18
- name: Set up Go 1.21
uses: actions/setup-go@v3
with:
go-version: '1.18'
go-version: '1.21'

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.50.0
version: v1.55.2
args: --timeout=180s
skip-cache: true

Expand Down
3 changes: 2 additions & 1 deletion keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/bradenaw/juniper/xslices"
"golang.org/x/exp/slices"
)

func ExtractSignatures(kr *crypto.KeyRing, arm string) ([]Signature, error) {
Expand Down Expand Up @@ -148,7 +149,7 @@ func (keys Keys) Unlock(passphrase []byte, userKR *crypto.KeyRing) (*crypto.KeyR
return nil, err
}

for _, key := range xslices.Filter(keys, func(key Key) bool { return bool(key.Active) }) {
for _, key := range slices.DeleteFunc(slices.Clone(keys), func(key Key) bool { return !bool(key.Active) }) {
unlocked, err := key.Unlock(passphrase, userKR)
if err != nil {
log.WithField("KeyID", key.ID).WithError(err).Warning("Cannot unlock key")
Expand Down
3 changes: 2 additions & 1 deletion message_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/bradenaw/juniper/stream"
"github.com/bradenaw/juniper/xslices"
"github.com/go-resty/resty/v2"
"golang.org/x/exp/slices"
)

const (
Expand All @@ -38,7 +39,7 @@ func (c *Client) ImportMessages(ctx context.Context, addrKR *crypto.KeyRing, wor
}

// If any of the messages exceed the maximum import size, return an error.
if xslices.Any(req, func(req ImportReq) bool { return len(req.Message) > maxImportSize }) {
if slices.ContainsFunc(req, func(req ImportReq) bool { return len(req.Message) > maxImportSize }) {
return nil, ErrImportSizeExceeded
}

Expand Down
4 changes: 2 additions & 2 deletions salt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/ProtonMail/go-srp"
"github.com/bradenaw/juniper/xslices"
"golang.org/x/exp/slices"
)

type Salt struct {
Expand All @@ -15,7 +15,7 @@ type Salt struct {
type Salts []Salt

func (salts Salts) SaltForKey(keyPass []byte, keyID string) ([]byte, error) {
idx := xslices.IndexFunc(salts, func(salt Salt) bool {
idx := slices.IndexFunc(salts, func(salt Salt) bool {
return salt.ID == keyID
})

Expand Down
26 changes: 13 additions & 13 deletions server/backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ func (b *Backend) GetLabels(userID string, types ...proton.LabelType) ([]proton.
}

if len(types) > 0 {
res = xslices.Filter(res, func(label proton.Label) bool {
return slices.Contains(types, label.Type)
res = slices.DeleteFunc(res, func(label proton.Label) bool {
return !slices.Contains(types, label.Type)
})
}

Expand Down Expand Up @@ -431,7 +431,7 @@ func (b *Backend) DeleteLabel(userID, labelID string) error {
return err
}

acc.labelIDs = xslices.Filter(acc.labelIDs, func(otherID string) bool { return otherID != labelID })
acc.labelIDs = slices.DeleteFunc(acc.labelIDs, func(otherID string) bool { return otherID == labelID })
acc.updateIDs = append(acc.updateIDs, updateID)
}

Expand Down Expand Up @@ -500,7 +500,7 @@ func (b *Backend) GetMessages(userID string, page, pageSize int, filter proton.M
// deleted in between this metadata request. The backend has the information stored differently and can
// resolve these gaps.
if filter.EndID != "" {
index := xslices.IndexFunc(metadata, func(metadata proton.MessageMetadata) bool {
index := slices.IndexFunc(metadata, func(metadata proton.MessageMetadata) bool {
return metadata.ID == filter.EndID
})

Expand All @@ -509,38 +509,38 @@ func (b *Backend) GetMessages(userID string, page, pageSize int, filter proton.M
}
}

metadata = xslices.Filter(metadata, func(metadata proton.MessageMetadata) bool {
metadata = slices.DeleteFunc(metadata, func(metadata proton.MessageMetadata) bool {
if len(filter.ID) > 0 {
if !slices.Contains(filter.ID, metadata.ID) {
return false
return true
}
}

if filter.Subject != "" {
if !strings.Contains(metadata.Subject, filter.Subject) {
return false
return true
}
}

if filter.AddressID != "" {
if filter.AddressID != metadata.AddressID {
return false
return true
}
}

if filter.ExternalID != "" {
if filter.ExternalID != metadata.ExternalID {
return false
return true
}
}

if filter.LabelID != "" {
if !slices.Contains(metadata.LabelIDs, filter.LabelID) {
return false
return true
}
}

return true
return false
})

pages := xslices.Chunk(metadata, pageSize)
Expand Down Expand Up @@ -712,7 +712,7 @@ func (b *Backend) DeleteMessage(userID, messageID string) error {
return err
}

acc.messageIDs = xslices.Filter(acc.messageIDs, func(otherID string) bool { return otherID != messageID })
acc.messageIDs = slices.DeleteFunc(acc.messageIDs, func(otherID string) bool { return otherID == messageID })
acc.updateIDs = append(acc.updateIDs, updateID)

return nil
Expand Down Expand Up @@ -1045,7 +1045,7 @@ func (b *Backend) GetEvent(userID, rawEventID string) (event proton.Event, more
return withMessages(b, func(messages map[string]*message) (proton.Event, error) {
return withLabels(b, func(labels map[string]*label) (proton.Event, error) {
return withAtts(b, func(attachments map[string]*attachment) (proton.Event, error) {
firstUpdate := xslices.Index(acc.updateIDs, eventID) + 1
firstUpdate := slices.Index(acc.updateIDs, eventID) + 1
lastUpdate := getLastUpdateIndex(len(acc.updateIDs), firstUpdate, b.maxUpdatesPerEvent)

updates, err := withUpdates(b, func(updates map[ID]update) ([]update, error) {
Expand Down
4 changes: 2 additions & 2 deletions server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (b *Backend) RemoveUserKey(userID, keyID string) error {
return fmt.Errorf("user %s does not exist", userID)
}

idx := xslices.IndexFunc(user.keys, func(key key) bool {
idx := slices.IndexFunc(user.keys, func(key key) bool {
return key.keyID == keyID
})

Expand Down Expand Up @@ -463,7 +463,7 @@ func (b *Backend) RemoveAddress(userID, addrID string) error {
func (b *Backend) RemoveAddressKey(userID, addrID, keyID string) error {
return writeBackendRet(b, func(b *unsafeBackend) error {
return b.withAcc(userID, func(acc *account) error {
idx := xslices.IndexFunc(acc.addresses[addrID].keys, func(key key) bool {
idx := slices.IndexFunc(acc.addresses[addrID].keys, func(key key) bool {
return key.keyID == keyID
})

Expand Down
16 changes: 8 additions & 8 deletions server/backend/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,25 +320,25 @@ func (msg *message) addLabel(labelID string, labels map[string]*label) {
}

func (msg *message) addFlagLabel(labelID string, labels map[string]*label) {
msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool {
return labels[otherLabelID].labelType == proton.LabelTypeLabel
msg.labelIDs = slices.DeleteFunc(msg.labelIDs, func(otherLabelID string) bool {
return labels[otherLabelID].labelType != proton.LabelTypeLabel
})

msg.sysLabel = nil
}

func (msg *message) addSystemLabel(labelID string, labels map[string]*label) {
msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool {
return labels[otherLabelID].labelType == proton.LabelTypeLabel
msg.labelIDs = slices.DeleteFunc(msg.labelIDs, func(otherLabelID string) bool {
return labels[otherLabelID].labelType != proton.LabelTypeLabel
})

msg.sysLabel = &labelID
}

func (msg *message) addUserLabel(label *label, labels map[string]*label) {
if label.labelType != proton.LabelTypeLabel {
msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool {
return labels[otherLabelID].labelType == proton.LabelTypeLabel
msg.labelIDs = slices.DeleteFunc(msg.labelIDs, func(otherLabelID string) bool {
return labels[otherLabelID].labelType != proton.LabelTypeLabel
})

msg.sysLabel = pointer("")
Expand Down Expand Up @@ -380,8 +380,8 @@ func (msg *message) remSystemLabel(labelID string, labels map[string]*label) {
}

func (msg *message) remUserLabel(label *label, labels map[string]*label) {
msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool {
return otherLabelID != label.labelID
msg.labelIDs = slices.DeleteFunc(msg.labelIDs, func(otherLabelID string) bool {
return otherLabelID == label.labelID
})
}

Expand Down
5 changes: 3 additions & 2 deletions server/backend/updates.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package backend

import (
"slices"

"github.com/ProtonMail/go-proton-api"
"github.com/bradenaw/juniper/xslices"
)

func merge(updates []update) []update {
if len(updates) < 2 {
return updates
}

if merged := merge(updates[1:]); xslices.IndexFunc(merged, func(other update) bool {
if merged := merge(updates[1:]); slices.IndexFunc(merged, func(other update) bool {
return other.replaces(updates[0])
}) < 0 {
return append([]update{updates[0]}, merged...)
Expand Down
8 changes: 4 additions & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2218,12 +2218,12 @@ func TestServer_GetMessageGroupCount(t *testing.T) {
counts, err := c.GetGroupedMessageCount(ctx)
require.NoError(t, err)

counts = xslices.Filter(counts, func(t proton.MessageGroupCount) bool {
counts = slices.DeleteFunc(counts, func(t proton.MessageGroupCount) bool {
switch t.LabelID {
case proton.InboxLabel, proton.TrashLabel, proton.ArchiveLabel, proton.AllMailLabel, proton.SentLabel:
return true
default:
return false
default:
return true
}
})
require.NotEmpty(t, counts)
Expand Down Expand Up @@ -2377,7 +2377,7 @@ func TestServer_Contacts(t *testing.T) {
require.Len(t, contacts, len(testContacts))

for _, v := range testContacts {
require.NotEqual(t, -1, xslices.IndexFunc(contacts, func(contact proton.Contact) bool {
require.NotEqual(t, -1, slices.IndexFunc(contacts, func(contact proton.Contact) bool {
return contact.Name == v.Name
}))
}
Expand Down

0 comments on commit f49fd22

Please sign in to comment.