Skip to content

Commit 8900785

Browse files
removed search data type
1 parent 58d85e4 commit 8900785

2 files changed

Lines changed: 10 additions & 25 deletions

File tree

‎src/chapter2/search/find/find.go‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import (
88
)
99

1010
type (
11-
// SearchData contains the data we can search on.
12-
SearchData struct {
13-
Title string
14-
Description string
15-
}
16-
1711
// Result contains the result of a search.
1812
Result struct {
1913
Field string

‎src/chapter2/search/rss/rss.go‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ func (s *Search) Match(site *feeds.Site, searchTerm string) ([]find.Result, erro
6363
var results []find.Result
6464

6565
// Retrieve the data to search.
66-
searchData, err := s.retrieve(site)
66+
document, err := s.retrieve(site)
6767
if err != nil {
6868
return nil, err
6969
}
7070

71-
for _, data := range searchData {
71+
for _, item := range document.Channel.Item {
7272
// Check the title for the search term.
73-
matched, err := regexp.MatchString(searchTerm, data.Title)
73+
matched, err := regexp.MatchString(searchTerm, item.Title)
7474
if err != nil {
7575
return nil, err
7676
}
@@ -79,12 +79,12 @@ func (s *Search) Match(site *feeds.Site, searchTerm string) ([]find.Result, erro
7979
if matched {
8080
results = append(results, find.Result{
8181
Field: "Title",
82-
Content: data.Title,
82+
Content: item.Title,
8383
})
8484
}
8585

8686
// Check the description for the search term.
87-
matched, err = regexp.MatchString(searchTerm, data.Description)
87+
matched, err = regexp.MatchString(searchTerm, item.Description)
8888
if err != nil {
8989
return nil, err
9090
}
@@ -93,7 +93,7 @@ func (s *Search) Match(site *feeds.Site, searchTerm string) ([]find.Result, erro
9393
if matched {
9494
results = append(results, find.Result{
9595
Field: "Description",
96-
Content: data.Description,
96+
Content: item.Description,
9797
})
9898
}
9999
}
@@ -102,7 +102,7 @@ func (s *Search) Match(site *feeds.Site, searchTerm string) ([]find.Result, erro
102102
}
103103

104104
// retrieve performs a HTTP Get request for the rss feed and unmarshals the results.
105-
func (s *Search) retrieve(site *feeds.Site) ([]find.SearchData, error) {
105+
func (s *Search) retrieve(site *feeds.Site) (*Document, error) {
106106
if site.Uri == "" {
107107
return nil, errors.New("No RSS Feed Uri Provided")
108108
}
@@ -117,20 +117,11 @@ func (s *Search) retrieve(site *feeds.Site) ([]find.SearchData, error) {
117117
defer resp.Body.Close()
118118

119119
// Unmarshal the rss feed document into our struct type.
120-
var rssDocument Document
121-
err = xml.NewDecoder(resp.Body).Decode(&rssDocument)
120+
var document Document
121+
err = xml.NewDecoder(resp.Body).Decode(&document)
122122
if err != nil {
123123
return nil, err
124124
}
125125

126-
// Create the slice of search data to be returned.
127-
searchData := make([]find.SearchData, len(rssDocument.Channel.Item))
128-
for _, item := range rssDocument.Channel.Item {
129-
searchData = append(searchData, find.SearchData{
130-
Title: item.Title,
131-
Description: item.Description,
132-
})
133-
}
134-
135-
return searchData, nil
126+
return &document, nil
136127
}

0 commit comments

Comments
 (0)