Skip to content

Commit e2b6e41

Browse files
Clean up interface
1 parent e948d7e commit e2b6e41

2 files changed

Lines changed: 39 additions & 40 deletions

File tree

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package find
22

33
import (
44
"log"
5+
"regexp"
56
"sync"
67

78
"github.com/goinaction/code/src/chapter2/search/feeds"
@@ -25,7 +26,6 @@ type (
2526
// Matcher defines the behavior required by the Search function.
2627
Matcher interface {
2728
Retrieve(site feeds.Site) ([]SearchData, error)
28-
Match(document []SearchData, searchTerm string) ([]Result, error)
2929
}
3030
)
3131

@@ -46,7 +46,7 @@ func Search(matcher Matcher, searchTerm string, site feeds.Site, result chan Res
4646
}
4747

4848
// Search the data for the search term.
49-
searchResults, err := matcher.Match(searchData, searchTerm)
49+
searchResults, err := match(searchData, searchTerm)
5050
if err != nil {
5151
log.Printf("%s : %s", site.Uri, err)
5252
return
@@ -57,3 +57,40 @@ func Search(matcher Matcher, searchTerm string, site feeds.Site, result chan Res
5757
result <- searchResult
5858
}
5959
}
60+
61+
// match looks at the document for the specified search term.
62+
func match(searchData []SearchData, searchTerm string) ([]Result, error) {
63+
var results []Result
64+
65+
for _, data := range searchData {
66+
// Check the title for the search term.
67+
matched, err := regexp.MatchString(searchTerm, data.Title)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
// If we found a match save the result.
73+
if matched {
74+
results = append(results, Result{
75+
Field: "Title",
76+
Content: data.Title,
77+
})
78+
}
79+
80+
// Check the description for the search term.
81+
matched, err = regexp.MatchString(searchTerm, data.Description)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
// If we found a match save the result.
87+
if matched {
88+
results = append(results, Result{
89+
Field: "Description",
90+
Content: data.Description,
91+
})
92+
}
93+
}
94+
95+
return results, nil
96+
}

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/xml"
55
"errors"
66
"net/http"
7-
"regexp"
87

98
"github.com/goinaction/code/src/chapter2/search/feeds"
109
"github.com/goinaction/code/src/chapter2/search/find"
@@ -91,40 +90,3 @@ func (s *Search) Retrieve(site feeds.Site) ([]find.SearchData, error) {
9190

9291
return searchData, nil
9392
}
94-
95-
// Match looks at the document for the specified search term.
96-
func (s *Search) Match(searchData []find.SearchData, searchTerm string) ([]find.Result, error) {
97-
var results []find.Result
98-
99-
for _, data := range searchData {
100-
// Check the title for the search term.
101-
matched, err := regexp.MatchString(searchTerm, data.Title)
102-
if err != nil {
103-
return nil, err
104-
}
105-
106-
// If we found a match save the result.
107-
if matched {
108-
results = append(results, find.Result{
109-
Field: "Title",
110-
Content: data.Title,
111-
})
112-
}
113-
114-
// Check the description for the search term.
115-
matched, err = regexp.MatchString(searchTerm, data.Description)
116-
if err != nil {
117-
return nil, err
118-
}
119-
120-
// If we found a match save the result.
121-
if matched {
122-
results = append(results, find.Result{
123-
Field: "Description",
124-
Content: data.Description,
125-
})
126-
}
127-
}
128-
129-
return results, nil
130-
}

0 commit comments

Comments
 (0)