Skip to content

Commit b4bb320

Browse files
committed
imapclient: use slice instead of map for FetchMessageBuffer sections
A map is misleading: the *imap.FetchItemBodySection from the command will not match the one in the reply.
1 parent d01842f commit b4bb320

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

imapclient/fetch.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,20 @@ type FetchItemDataModSeq struct {
479479

480480
func (FetchItemDataModSeq) fetchItemData() {}
481481

482+
// FetchBodySectionBuffer is a buffer for the data returned by
483+
// FetchItemBodySection.
484+
type FetchBodySectionBuffer struct {
485+
Section *imap.FetchItemBodySection
486+
Bytes []byte
487+
}
488+
489+
// FetchBinarySectionBuffer is a buffer for the data returned by
490+
// FetchItemBinarySection.
491+
type FetchBinarySectionBuffer struct {
492+
Section *imap.FetchItemBinarySection
493+
Bytes []byte
494+
}
495+
482496
// FetchMessageBuffer is a buffer for the data returned by FetchMessageData.
483497
//
484498
// The SeqNum field is always populated. All remaining fields are optional.
@@ -490,8 +504,8 @@ type FetchMessageBuffer struct {
490504
RFC822Size int64
491505
UID imap.UID
492506
BodyStructure imap.BodyStructure
493-
BodySection map[*imap.FetchItemBodySection][]byte
494-
BinarySection map[*imap.FetchItemBinarySection][]byte
507+
BodySection []FetchBodySectionBuffer
508+
BinarySection []FetchBinarySectionBuffer
495509
BinarySectionSize []FetchItemDataBinarySectionSize
496510
ModSeq uint64 // requires CONDSTORE
497511
}
@@ -507,10 +521,10 @@ func (buf *FetchMessageBuffer) populateItemData(item FetchItemData) error {
507521
return err
508522
}
509523
}
510-
if buf.BodySection == nil {
511-
buf.BodySection = make(map[*imap.FetchItemBodySection][]byte)
512-
}
513-
buf.BodySection[item.Section] = b
524+
buf.BodySection = append(buf.BodySection, FetchBodySectionBuffer{
525+
Section: item.Section,
526+
Bytes: b,
527+
})
514528
case FetchItemDataBinarySection:
515529
var b []byte
516530
if item.Literal != nil {
@@ -520,10 +534,10 @@ func (buf *FetchMessageBuffer) populateItemData(item FetchItemData) error {
520534
return err
521535
}
522536
}
523-
if buf.BinarySection == nil {
524-
buf.BinarySection = make(map[*imap.FetchItemBinarySection][]byte)
525-
}
526-
buf.BinarySection[item.Section] = b
537+
buf.BinarySection = append(buf.BinarySection, FetchBinarySectionBuffer{
538+
Section: item.Section,
539+
Bytes: b,
540+
})
527541
case FetchItemDataFlags:
528542
buf.Flags = item.Flags
529543
case FetchItemDataEnvelope:
@@ -550,9 +564,9 @@ func (buf *FetchMessageBuffer) populateItemData(item FetchItemData) error {
550564
//
551565
// If the body section is not found, nil is returned.
552566
func (buf *FetchMessageBuffer) FindBodySection(section *imap.FetchItemBodySection) []byte {
553-
for s, b := range buf.BodySection {
554-
if matchFetchItemBodySection(section, s) {
555-
return b
567+
for _, s := range buf.BodySection {
568+
if matchFetchItemBodySection(section, s.Section) {
569+
return s.Bytes
556570
}
557571
}
558572
return nil
@@ -562,9 +576,9 @@ func (buf *FetchMessageBuffer) FindBodySection(section *imap.FetchItemBodySectio
562576
//
563577
// If the binary section is not found, nil is returned.
564578
func (buf *FetchMessageBuffer) FindBinarySection(section *imap.FetchItemBinarySection) []byte {
565-
for s, b := range buf.BinarySection {
566-
if matchFetchItemBinarySection(section, s) {
567-
return b
579+
for _, s := range buf.BinarySection {
580+
if matchFetchItemBinarySection(section, s.Section) {
581+
return s.Bytes
568582
}
569583
}
570584
return nil

0 commit comments

Comments
 (0)