diff --git a/internal/reader/opml/handler.go b/internal/reader/opml/handler.go index b51c938b..f716f7b9 100644 --- a/internal/reader/opml/handler.go +++ b/internal/reader/opml/handler.go @@ -23,9 +23,9 @@ func (h *Handler) Export(userID int64) (string, error) { return "", err } - subscriptions := make(subcriptionList, 0, len(feeds)) + subscriptions := make([]subcription, 0, len(feeds)) for _, feed := range feeds { - subscriptions = append(subscriptions, &subcription{ + subscriptions = append(subscriptions, subcription{ Title: feed.Title, FeedURL: feed.FeedURL, SiteURL: feed.SiteURL, diff --git a/internal/reader/opml/parser.go b/internal/reader/opml/parser.go index d7e56d5e..d814e630 100644 --- a/internal/reader/opml/parser.go +++ b/internal/reader/opml/parser.go @@ -11,8 +11,8 @@ import ( "miniflux.app/v2/internal/reader/encoding" ) -// parse reads an OPML file and returns a SubcriptionList. -func parse(data io.Reader) (subcriptionList, error) { +// parse reads an OPML file and returns a list of subscription. +func parse(data io.Reader) ([]subcription, error) { opmlDocument := &opmlDocument{} decoder := xml.NewDecoder(data) decoder.Entity = xml.HTMLEntity @@ -27,10 +27,12 @@ func parse(data io.Reader) (subcriptionList, error) { return getSubscriptionsFromOutlines(opmlDocument.Outlines, ""), nil } -func getSubscriptionsFromOutlines(outlines opmlOutlineCollection, category string) (subscriptions subcriptionList) { +func getSubscriptionsFromOutlines(outlines opmlOutlineCollection, category string) []subcription { + subscriptions := make([]subcription, 0, len(outlines)) + for _, outline := range outlines { if outline.IsSubscription() { - subscriptions = append(subscriptions, &subcription{ + subscriptions = append(subscriptions, subcription{ Title: outline.GetTitle(), FeedURL: outline.FeedURL, SiteURL: outline.GetSiteURL(), diff --git a/internal/reader/opml/parser_test.go b/internal/reader/opml/parser_test.go index 89624444..4252e4d8 100644 --- a/internal/reader/opml/parser_test.go +++ b/internal/reader/opml/parser_test.go @@ -9,7 +9,7 @@ import ( ) // equals compare two subscriptions. -func (s subcription) equals(subscription *subcription) bool { +func (s subcription) equals(subscription subcription) bool { return s.Title == subscription.Title && s.SiteURL == subscription.SiteURL && s.FeedURL == subscription.FeedURL && s.CategoryName == subscription.CategoryName && s.Description == subscription.Description @@ -39,8 +39,8 @@ func TestParseOpmlWithoutCategories(t *testing.T) { ` - var expected subcriptionList - expected = append(expected, &subcription{Title: "CNET News.com", FeedURL: "http://news.com.com/2547-1_3-0-5.xml", SiteURL: "http://news.com.com/", Description: "Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media."}) + var expected []subcription + expected = append(expected, subcription{Title: "CNET News.com", FeedURL: "http://news.com.com/2547-1_3-0-5.xml", SiteURL: "http://news.com.com/", Description: "Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media."}) subscriptions, err := parse(bytes.NewBufferString(data)) if err != nil { @@ -74,10 +74,10 @@ func TestParseOpmlWithCategories(t *testing.T) { ` - var expected subcriptionList - expected = append(expected, &subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"}) - expected = append(expected, &subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"}) - expected = append(expected, &subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"}) + var expected []subcription + expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"}) + expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"}) + expected = append(expected, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"}) subscriptions, err := parse(bytes.NewBufferString(data)) if err != nil { @@ -108,9 +108,9 @@ func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) { ` - var expected subcriptionList - expected = append(expected, &subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""}) - expected = append(expected, &subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""}) + var expected []subcription + expected = append(expected, subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""}) + expected = append(expected, subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""}) subscriptions, err := parse(bytes.NewBufferString(data)) if err != nil { @@ -146,9 +146,9 @@ func TestParseOpmlVersion1(t *testing.T) { ` - var expected subcriptionList - expected = append(expected, &subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Category 1"}) - expected = append(expected, &subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Category 2"}) + var expected []subcription + expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Category 1"}) + expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Category 2"}) subscriptions, err := parse(bytes.NewBufferString(data)) if err != nil { @@ -180,9 +180,9 @@ func TestParseOpmlVersion1WithoutOuterOutline(t *testing.T) { ` - var expected subcriptionList - expected = append(expected, &subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""}) - expected = append(expected, &subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""}) + var expected []subcription + expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""}) + expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""}) subscriptions, err := parse(bytes.NewBufferString(data)) if err != nil { @@ -221,10 +221,10 @@ func TestParseOpmlVersion1WithSeveralNestedOutlines(t *testing.T) { ` - var expected subcriptionList - expected = append(expected, &subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Some Category"}) - expected = append(expected, &subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Some Category"}) - expected = append(expected, &subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "Another Category"}) + var expected []subcription + expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Some Category"}) + expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Some Category"}) + expected = append(expected, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "Another Category"}) subscriptions, err := parse(bytes.NewBufferString(data)) if err != nil { @@ -256,8 +256,8 @@ func TestParseOpmlWithInvalidCharacterEntity(t *testing.T) { ` - var expected subcriptionList - expected = append(expected, &subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/a&b", SiteURL: "http://example.org/c&d", CategoryName: "Feed 1"}) + var expected []subcription + expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/a&b", SiteURL: "http://example.org/c&d", CategoryName: "Feed 1"}) subscriptions, err := parse(bytes.NewBufferString(data)) if err != nil { diff --git a/internal/reader/opml/serializer.go b/internal/reader/opml/serializer.go index fae55ed1..cebf8c21 100644 --- a/internal/reader/opml/serializer.go +++ b/internal/reader/opml/serializer.go @@ -13,7 +13,7 @@ import ( ) // serialize returns a SubcriptionList in OPML format. -func serialize(subscriptions subcriptionList) string { +func serialize(subscriptions []subcription) string { var b bytes.Buffer writer := bufio.NewWriter(&b) writer.WriteString(xml.Header) @@ -31,7 +31,7 @@ func serialize(subscriptions subcriptionList) string { return b.String() } -func convertSubscriptionsToOPML(subscriptions subcriptionList) *opmlDocument { +func convertSubscriptionsToOPML(subscriptions []subcription) *opmlDocument { opmlDocument := &opmlDocument{} opmlDocument.Version = "2.0" opmlDocument.Header.Title = "Miniflux" @@ -62,8 +62,8 @@ func convertSubscriptionsToOPML(subscriptions subcriptionList) *opmlDocument { return opmlDocument } -func groupSubscriptionsByFeed(subscriptions subcriptionList) map[string]subcriptionList { - groups := make(map[string]subcriptionList) +func groupSubscriptionsByFeed(subscriptions []subcription) map[string][]subcription { + groups := make(map[string][]subcription) for _, subscription := range subscriptions { groups[subscription.CategoryName] = append(groups[subscription.CategoryName], subscription) diff --git a/internal/reader/opml/serializer_test.go b/internal/reader/opml/serializer_test.go index 4b6f693a..5be4f36e 100644 --- a/internal/reader/opml/serializer_test.go +++ b/internal/reader/opml/serializer_test.go @@ -9,10 +9,10 @@ import ( ) func TestSerialize(t *testing.T) { - var subscriptions subcriptionList - subscriptions = append(subscriptions, &subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: "Category 1"}) - subscriptions = append(subscriptions, &subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: "Category 1"}) - subscriptions = append(subscriptions, &subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: "Category 2"}) + var subscriptions []subcription + subscriptions = append(subscriptions, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: "Category 1"}) + subscriptions = append(subscriptions, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: "Category 1"}) + subscriptions = append(subscriptions, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: "Category 2"}) output := serialize(subscriptions) feeds, err := parse(bytes.NewBufferString(output)) @@ -48,10 +48,10 @@ func TestNormalizedCategoriesOrder(t *testing.T) { {"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}) + var subscriptions []subcription + 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 := convertSubscriptionsToOPML(subscriptions) diff --git a/internal/reader/opml/subscription.go b/internal/reader/opml/subscription.go index 1920fd4e..20823ae7 100644 --- a/internal/reader/opml/subscription.go +++ b/internal/reader/opml/subscription.go @@ -11,6 +11,3 @@ type subcription struct { CategoryName string Description string } - -// subcriptionList is a list of subscriptions. -type subcriptionList []*subcription