Skip to content

v1: add support for raw search fields #334

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 5 commits into
base: v1
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
40 changes: 37 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ func popSearchField(fields []interface{}) (interface{}, []interface{}, error) {
return fields[0], fields[1:], nil
}

func parseRawField(fields []interface{}) (interface{}, []interface{}) {
// Fields is empty. This means this isn't a raw field value but rather another raw key
if len(fields) == 0 {
return nil, fields
}

// First element in fields is an array of interfaces, this means this field doesn't have a value and is nil
if _, ok := fields[0].([]interface{}); ok {
return nil, fields
}

return fields[0], fields[1:]
}

type Raw struct {
Key string
Value interface{}
}

// SearchCriteria is a search criteria. A message matches the criteria if and
// only if it matches each one of its fields.
type SearchCriteria struct {
Expand All @@ -80,6 +99,8 @@ type SearchCriteria struct {

Not []*SearchCriteria // Each criteria doesn't match
Or [][2]*SearchCriteria // Each criteria pair has at least one match of two

Raw []Raw // Raw keys and values to be added to the search
}

// NewSearchCriteria creates a new search criteria.
Expand Down Expand Up @@ -249,9 +270,14 @@ func (c *SearchCriteria) parseField(fields []interface{}, charsetReader func(io.
} else {
c.WithoutFlags = append(c.WithoutFlags, CanonicalFlag(maybeString(f)))
}
default: // Try to parse a sequence set
if c.SeqNum, err = ParseSeqSet(key); err != nil {
return nil, err
default: // Try to parse a sequence set, otherwise assume we have raw fields
if seqNum, err := ParseSeqSet(key); err == nil {
c.SeqNum = seqNum
} else {
// If ParseSeqSet fails, assume we have raw fields
var rawValue interface{}
rawValue, fields = parseRawField(fields)
c.Raw = append(c.Raw, Raw{Key: key, Value: rawValue})
}
}

Expand Down Expand Up @@ -362,6 +388,14 @@ func (c *SearchCriteria) Format() []interface{} {
fields = append(fields, RawString("OR"), or[0].Format(), or[1].Format())
}

for _, raw := range c.Raw {
field := []interface{}{RawString(raw.Key)}
if raw.Value != nil {
field = append(field, raw.Value)
}
fields = append(fields, field)
}

// Not a single criteria given, add ALL criteria as fallback
if len(fields) == 0 {
fields = append(fields, RawString("ALL"))
Expand Down
20 changes: 20 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ var searchCriteriaTests = []struct {
}},
},
},
{
expected: `((X-GM-THRID) (X-GM-RAW "has:attachment") (X-GM-MSGID))`,
criteria: &SearchCriteria{
Raw: []Raw{
{"X-GM-THRID", nil},
{"X-GM-RAW", "has:attachment"},
{"X-GM-MSGID", nil},
},
},
},
{
expected: `((X-GM-THRID) (X-GM-MSGID) (X-GM-RAW "has:attachment"))`,
criteria: &SearchCriteria{
Raw: []Raw{
{"X-GM-THRID", nil},
{"X-GM-MSGID", nil},
{"X-GM-RAW", "has:attachment"},
},
},
},
{
expected: "(ALL)",
criteria: &SearchCriteria{},
Expand Down