From c840268678897c4bd95857a9b099fa4f149fcd8f Mon Sep 17 00:00:00 2001 From: Ilya Glotov Date: Fri, 5 Jul 2019 19:05:42 +0300 Subject: Sort feed categories before serialization A function is added for feeds and its categories normalization. The test will ensure that the order is right. --- reader/opml/serializer.go | 47 +++++++++++++++++++++++++++--------------- reader/opml/serializer_test.go | 24 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 17 deletions(-) (limited to 'reader') diff --git a/reader/opml/serializer.go b/reader/opml/serializer.go index c094f4c..31619e1 100644 --- a/reader/opml/serializer.go +++ b/reader/opml/serializer.go @@ -8,6 +8,7 @@ import ( "bufio" "bytes" "encoding/xml" + "sort" "miniflux.app/logger" ) @@ -18,23 +19,7 @@ func Serialize(subscriptions SubcriptionList) string { writer := bufio.NewWriter(&b) writer.WriteString(xml.Header) - feeds := new(opml) - feeds.Version = "2.0" - for categoryName, subs := range groupSubscriptionsByFeed(subscriptions) { - category := outline{Text: categoryName} - - for _, subscription := range subs { - category.Outlines = append(category.Outlines, outline{ - Title: subscription.Title, - Text: subscription.Title, - FeedURL: subscription.FeedURL, - SiteURL: subscription.SiteURL, - }) - } - - feeds.Outlines = append(feeds.Outlines, category) - } - + feeds := normalizeFeeds(subscriptions) encoder := xml.NewEncoder(writer) encoder.Indent(" ", " ") if err := encoder.Encode(feeds); err != nil { @@ -54,3 +39,31 @@ func groupSubscriptionsByFeed(subscriptions SubcriptionList) map[string]Subcript return groups } + +func normalizeFeeds(subscriptions SubcriptionList) *opml { + feeds := new(opml) + feeds.Version = "2.0" + + groupedSubs := groupSubscriptionsByFeed(subscriptions) + var categories []string + for k := range groupedSubs { + categories = append(categories, k) + } + sort.Strings(categories) + + for _, categoryName := range categories { + category := outline{Text: categoryName} + for _, subscription := range groupedSubs[categoryName] { + category.Outlines = append(category.Outlines, outline{ + Title: subscription.Title, + Text: subscription.Title, + FeedURL: subscription.FeedURL, + SiteURL: subscription.SiteURL, + }) + } + + feeds.Outlines = append(feeds.Outlines, category) + } + + return feeds +} diff --git a/reader/opml/serializer_test.go b/reader/opml/serializer_test.go index 9c598ee..c0acd90 100644 --- a/reader/opml/serializer_test.go +++ b/reader/opml/serializer_test.go @@ -40,3 +40,27 @@ func TestSerialize(t *testing.T) { t.Error("Serialized feed is incorrect") } } + +func TestNormalizedCategoriesOrder(t *testing.T) { + var orderTests = []struct { + naturalOrderName string + correctOrderName string + }{ + {"Category 2", "Category 1"}, + {"Category 3", "Category 2"}, + {"Category 1", "Category 3"}, + } + + var subscriptions SubcriptionList + subscriptions = append(subscriptions, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: orderTests[0].naturalOrderName}) + subscriptions = append(subscriptions, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: orderTests[1].naturalOrderName}) + subscriptions = append(subscriptions, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: orderTests[2].naturalOrderName}) + + feeds := normalizeFeeds(subscriptions) + + for i, o := range orderTests { + if feeds.Outlines[i].Text != o.correctOrderName { + t.Fatalf("need %v, got %v", o.correctOrderName, feeds.Outlines[i].Text) + } + } +} -- cgit v1.2.3