Skip to content

Commit

Permalink
Update references to types package in mailstore package #10
Browse files Browse the repository at this point in the history
  • Loading branch information
jordwest committed Jun 9, 2015
1 parent 19196ae commit f8febc8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 36 deletions.
42 changes: 22 additions & 20 deletions mailstore/dummy_mailstore.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package imap
package mailstore

import (
"errors"
"fmt"
"time"

"github.com/jordwest/imap-server/types"
)

// DummyMailstore is an in-memory mail storage for testing purposes and to
Expand Down Expand Up @@ -166,7 +168,7 @@ func (m DummyMailbox) MessageByUID(uidno uint32) Message {

// MessageSetByUID returns a slice of messages given a set of UID ranges.
// eg 1,5,9,28:140,190:*
func (m DummyMailbox) MessageSetByUID(set SequenceSet) []Message {
func (m DummyMailbox) MessageSetByUID(set types.SequenceSet) []Message {
var msgs []Message

// If the mailbox is empty, return empty array
Expand All @@ -175,26 +177,26 @@ func (m DummyMailbox) MessageSetByUID(set SequenceSet) []Message {
}

for _, msgRange := range set {
// If min is "*", meaning the last UID in the mailbox, max should
// If Min is "*", meaning the last UID in the mailbox, Max should
// always be Nil
if msgRange.min.Last() {
if msgRange.Min.Last() {
// Return the last message in the mailbox
msgs = append(msgs, m.MessageByUID(m.LastUID()))
continue
}

start, err := msgRange.min.Value()
start, err := msgRange.Min.Value()
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return msgs
}

// If no max is specified, the sequence number must be either a fixed
// If no Max is specified, the sequence number must be either a fixed
// sequence number or
if msgRange.max.Nil() {
if msgRange.Max.Nil() {
var uid uint32
// Fetch specific message by sequence number
uid, err = msgRange.min.Value()
uid, err = msgRange.Min.Value()
msgs = append(msgs, m.MessageByUID(uid))
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
Expand All @@ -204,10 +206,10 @@ func (m DummyMailbox) MessageSetByUID(set SequenceSet) []Message {
}

var end uint32
if msgRange.max.Last() {
if msgRange.Max.Last() {
end = m.LastUID()
} else {
end, err = msgRange.max.Value()
end, err = msgRange.Max.Value()
}

// Note this is very inefficient when
Expand All @@ -230,7 +232,7 @@ func (m DummyMailbox) MessageSetByUID(set SequenceSet) []Message {

// MessageSetBySequenceNumber returns a slice of messages given a set of
// sequence number ranges
func (m DummyMailbox) MessageSetBySequenceNumber(set SequenceSet) []Message {
func (m DummyMailbox) MessageSetBySequenceNumber(set types.SequenceSet) []Message {
var msgs []Message

// If the mailbox is empty, return empty array
Expand All @@ -240,26 +242,26 @@ func (m DummyMailbox) MessageSetBySequenceNumber(set SequenceSet) []Message {

// For each sequence range in the sequence set
for _, msgRange := range set {
// If min is "*", meaning the last message in the mailbox, max should
// If Min is "*", meaning the last message in the mailbox, Max should
// always be Nil
if msgRange.min.Last() {
if msgRange.Min.Last() {
// Return the last message in the mailbox
msgs = append(msgs, m.MessageBySequenceNumber(m.Messages()))
continue
}

start, err := msgRange.min.Value()
start, err := msgRange.Min.Value()
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return msgs
}

// If no max is specified, the sequence number must be either a fixed
// If no Max is specified, the sequence number must be either a fixed
// sequence number or
if msgRange.max.Nil() {
if msgRange.Max.Nil() {
var sequenceNo uint32
// Fetch specific message by sequence number
sequenceNo, err = msgRange.min.Value()
sequenceNo, err = msgRange.Min.Value()
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return msgs
Expand All @@ -269,10 +271,10 @@ func (m DummyMailbox) MessageSetBySequenceNumber(set SequenceSet) []Message {
}

var end uint32
if msgRange.max.Last() {
if msgRange.Max.Last() {
end = uint32(len(m.messages))
} else {
end, err = msgRange.max.Value()
end, err = msgRange.Max.Value()
}

// Note this is very inefficient when
Expand Down Expand Up @@ -312,7 +314,7 @@ func (m *DummyMailbox) addEmail(from string, to string, subject string, date tim
type DummyMessage struct {
sequenceNumber uint32
uid uint32
header MIMEHeader
header types.MIMEHeader
seen bool
deleted bool
recent bool
Expand Down
2 changes: 1 addition & 1 deletion mailstore/dummy_mailstore_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imap
package mailstore_test

import "testing"

Expand Down
14 changes: 9 additions & 5 deletions mailstore/mailstore.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package imap
package mailstore

import "time"
import (
"time"

"github.com/jordwest/imap-server/types"
)

// Mailstore is an interface to be implemented to provide mail storage
type Mailstore interface {
Expand Down Expand Up @@ -46,17 +50,17 @@ type Mailbox interface {
MessageByUID(uidno uint32) Message

// Get messages that belong to a set of ranges of UIDs
MessageSetByUID(set SequenceSet) []Message
MessageSetByUID(set types.SequenceSet) []Message

// Get messages that belong to a set of ranges of sequence numbers
MessageSetBySequenceNumber(set SequenceSet) []Message
MessageSetBySequenceNumber(set types.SequenceSet) []Message
}

// Message represents a standard email message
type Message interface {
// Return the message's MIME headers as a map in format
// key: value
Header() MIMEHeader
Header() types.MIMEHeader

// Return the unique id of the email
UID() uint32
Expand Down
2 changes: 1 addition & 1 deletion types/headers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imap
package types

import (
"fmt"
Expand Down
16 changes: 8 additions & 8 deletions types/sequence_numbers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imap
package types

import (
"fmt"
Expand Down Expand Up @@ -54,8 +54,8 @@ func (s SequenceNumber) Value() (uint32, error) {

// SequenceRange represents a range of identifiers. eg in IMAP: 5:9 or 15:*
type SequenceRange struct {
min SequenceNumber
max SequenceNumber
Min SequenceNumber
Max SequenceNumber
}

// SequenceSet represents set of sequence ranges. eg in IMAP: 1,3,5:9,18:*
Expand Down Expand Up @@ -99,24 +99,24 @@ func interpretMessageRange(imapMessageRange string) (seqRange SequenceRange, err

// Reduce *:* to *
if first.Last() && second.Last() {
return SequenceRange{min: SequenceNumber("*"), max: SequenceNumber("")}, nil
return SequenceRange{Min: SequenceNumber("*"), Max: SequenceNumber("")}, nil
}

// Ensure "*" is always placed in 'max'
// Ensure "*" is always placed in 'Max'
if first.Last() && !second.Nil() {
return SequenceRange{min: second, max: first}, nil
return SequenceRange{Min: second, Max: first}, nil
}

// If both sequence numbers are integer values, we need to sort them
if first.IsValue() && second.IsValue() {
firstVal, _ := first.Value()
secondVal, _ := second.Value()
if firstVal > secondVal {
return SequenceRange{min: second, max: first}, nil
return SequenceRange{Min: second, Max: first}, nil
}
}

return SequenceRange{min: first, max: second}, nil
return SequenceRange{Min: first, Max: second}, nil
}

func interpretSequenceSet(imapSequenceSet string) (seqSet SequenceSet, err error) {
Expand Down
2 changes: 1 addition & 1 deletion types/sequence_numbers_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imap
package types_test

import (
"testing"
Expand Down

0 comments on commit f8febc8

Please sign in to comment.