From 8dea3f83bc8abbb25ce95545ed9fec36aa9dd2ac Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 1 Jan 2019 15:09:47 +0100 Subject: [PATCH] Refactor sorting a bit --- parser/parser.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index c25e5a4..ad78f97 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -67,8 +67,31 @@ func NewSortConfig() qEntrySortConfig { return qEntrySortConfig{} } -func (c qEntrySortConfig) By(field string, order string) qEntrySortConfig { - newAttributeConfig := qEntryAttributeSortConfig{attribute: field, order: order} +func isQEntryAttribute(attributeName string) bool { + var isValidAttribute bool + switch attributeName { + case "Id": + case "Status": + case "Date": + case "Size": + case "Sender": + case "Recipients": + case "Reason": + isValidAttribute = true + default: + isValidAttribute = false + } + return isValidAttribute +} + +func (c qEntrySortConfig) By(attributeName string, order string) qEntrySortConfig { + if !isQEntryAttribute(attributeName) { + panic(fmt.Sprintf("Invalid sort attribute: '%s' given!", attributeName)) + } + if order != "ASC" && order != "DESC" { + panic(fmt.Sprintf("Invalid sort order '%s' given!", order)) + } + newAttributeConfig := qEntryAttributeSortConfig{attribute: attributeName, order: order} c.attributes = append(c.attributes, newAttributeConfig) return c } @@ -77,6 +100,7 @@ func (queue MailQ) Sort(config qEntrySortConfig) { if len(config.attributes) == 0 { return } + fmt.Printf("SortConfig: %q\n", config) sort.Slice(queue.Entries, func(a int, b int) bool { for _, sortBy := range config.attributes { var cmp bool