Pārlūkot izejas kodu

Add support for multiple authors in Atom feeds

Frédéric Guillot 4 gadi atpakaļ
vecāks
revīzija
f18ded6117
3 mainītis faili ar 138 papildinājumiem un 11 dzēšanām
  1. 11 11
      reader/atom/atom_10.go
  2. 115 0
      reader/atom/atom_10_test.go
  3. 12 0
      reader/atom/atom_common.go

+ 11 - 11
reader/atom/atom_10.go

@@ -26,7 +26,7 @@ type atom10Feed struct {
 	XMLName xml.Name      `xml:"http://www.w3.org/2005/Atom feed"`
 	ID      string        `xml:"id"`
 	Title   atom10Text    `xml:"title"`
-	Author  atomPerson    `xml:"author"`
+	Authors atomAuthors   `xml:"author"`
 	Links   atomLinks     `xml:"link"`
 	Entries []atom10Entry `xml:"entry"`
 }
@@ -61,7 +61,7 @@ func (a *atom10Feed) Transform(baseURL string) *model.Feed {
 		}
 
 		if item.Author == "" {
-			item.Author = a.Author.String()
+			item.Author = a.Authors.String()
 		}
 
 		if item.Title == "" {
@@ -75,14 +75,14 @@ func (a *atom10Feed) Transform(baseURL string) *model.Feed {
 }
 
 type atom10Entry struct {
-	ID        string     `xml:"id"`
-	Title     atom10Text `xml:"title"`
-	Published string     `xml:"published"`
-	Updated   string     `xml:"updated"`
-	Links     atomLinks  `xml:"link"`
-	Summary   atom10Text `xml:"summary"`
-	Content   atom10Text `xml:"http://www.w3.org/2005/Atom content"`
-	Author    atomPerson `xml:"author"`
+	ID        string      `xml:"id"`
+	Title     atom10Text  `xml:"title"`
+	Published string      `xml:"published"`
+	Updated   string      `xml:"updated"`
+	Links     atomLinks   `xml:"link"`
+	Summary   atom10Text  `xml:"summary"`
+	Content   atom10Text  `xml:"http://www.w3.org/2005/Atom content"`
+	Authors   atomAuthors `xml:"author"`
 	media.Element
 }
 
@@ -90,7 +90,7 @@ func (a *atom10Entry) Transform() *model.Entry {
 	entry := new(model.Entry)
 	entry.URL = a.Links.originalLink()
 	entry.Date = a.entryDate()
-	entry.Author = a.Author.String()
+	entry.Author = a.Authors.String()
 	entry.Hash = a.entryHash()
 	entry.Content = a.entryContent()
 	entry.Title = a.entryTitle()

+ 115 - 0
reader/atom/atom_10_test.go

@@ -732,6 +732,121 @@ func TestParseEntryWithoutAuthorName(t *testing.T) {
 	}
 }
 
+func TestParseEntryWithMultipleAuthors(t *testing.T) {
+	data := `<?xml version="1.0" encoding="utf-8"?>
+	<feed xmlns="http://www.w3.org/2005/Atom">
+	  <title>Example Feed</title>
+	  <link href="http://example.org/"/>
+
+	  <entry>
+		<link href="http://example.org/2003/12/13/atom03"/>
+		<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
+		<updated>2003-12-13T18:30:02Z</updated>
+		<summary>Some text.</summary>
+		<author>
+			<name>Alice</name>
+		</author>
+		<author>
+			<name>Bob</name>
+		</author>
+	  </entry>
+
+	</feed>`
+
+	feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if feed.Entries[0].Author != "Alice, Bob" {
+		t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
+	}
+}
+
+func TestParseEntryWithoutAuthor(t *testing.T) {
+	data := `<?xml version="1.0" encoding="utf-8"?>
+	<feed xmlns="http://www.w3.org/2005/Atom">
+	  <title>Example Feed</title>
+	  <link href="http://example.org/"/>
+	  <author>
+		<name>John Doe</name>
+	  </author>
+
+	  <entry>
+		<link href="http://example.org/2003/12/13/atom03"/>
+		<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
+		<updated>2003-12-13T18:30:02Z</updated>
+		<summary>Some text.</summary>
+	  </entry>
+
+	</feed>`
+
+	feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if feed.Entries[0].Author != "John Doe" {
+		t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
+	}
+}
+
+func TestParseFeedWithMultipleAuthors(t *testing.T) {
+	data := `<?xml version="1.0" encoding="utf-8"?>
+	<feed xmlns="http://www.w3.org/2005/Atom">
+	  <title>Example Feed</title>
+	  <link href="http://example.org/"/>
+	  <author>
+		<name>Alice</name>
+	  </author>
+	  <author>
+		<name>Bob</name>
+	  </author>
+
+	  <entry>
+		<link href="http://example.org/2003/12/13/atom03"/>
+		<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
+		<updated>2003-12-13T18:30:02Z</updated>
+		<summary>Some text.</summary>
+	  </entry>
+
+	</feed>`
+
+	feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if feed.Entries[0].Author != "Alice, Bob" {
+		t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
+	}
+}
+
+func TestParseFeedWithoutAuthor(t *testing.T) {
+	data := `<?xml version="1.0" encoding="utf-8"?>
+	<feed xmlns="http://www.w3.org/2005/Atom">
+	  <title>Example Feed</title>
+	  <link href="http://example.org/"/>
+
+	  <entry>
+		<link href="http://example.org/2003/12/13/atom03"/>
+		<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
+		<updated>2003-12-13T18:30:02Z</updated>
+		<summary>Some text.</summary>
+	  </entry>
+
+	</feed>`
+
+	feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if feed.Entries[0].Author != "" {
+		t.Errorf("Incorrect entry author, got: %q", feed.Entries[0].Author)
+	}
+}
+
 func TestParseEntryWithEnclosures(t *testing.T) {
 	data := `<?xml version="1.0" encoding="utf-8"?>
 	<feed xmlns="http://www.w3.org/2005/Atom">

+ 12 - 0
reader/atom/atom_common.go

@@ -24,6 +24,18 @@ func (a *atomPerson) String() string {
 	return strings.TrimSpace(name)
 }
 
+type atomAuthors []*atomPerson
+
+func (a atomAuthors) String() string {
+	var authors []string
+
+	for _, person := range a {
+		authors = append(authors, person.String())
+	}
+
+	return strings.Join(authors, ", ")
+}
+
 type atomLink struct {
 	URL    string `xml:"href,attr"`
 	Type   string `xml:"type,attr"`