Skip to content

Commit 03edbba

Browse files
More refactoring
1 parent aa92a51 commit 03edbba

8 files changed

Lines changed: 82 additions & 59 deletions

File tree

‎chapter2/sample/main.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
_ "github.com/goinaction/code/chapter2/sample/matchers"
5+
"github.com/goinaction/code/chapter2/sample/search"
6+
)
7+
8+
// main is the entry point for the program.
9+
func main() {
10+
// Perform the search.
11+
search.Run("president")
12+
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net/http"
88
"regexp"
99

10-
"github.com/goinaction/code/src/chapter2/search/match"
10+
"github.com/goinaction/code/chapter2/sample/search"
1111
)
1212

1313
type (
@@ -54,18 +54,21 @@ type (
5454
)
5555

5656
// rssMatcher implements the Matcher interface.
57-
type rssMatcher struct {
58-
*match.Feed
57+
type rssMatcher struct{}
58+
59+
// init registered the matcher with the program
60+
func init() {
61+
search.Register("rss", &rssMatcher{})
5962
}
6063

6164
// Search looks at the document for the specified search term.
62-
func (m *rssMatcher) Search(searchTerm string) ([]*match.Result, error) {
63-
var results []*match.Result
65+
func (m *rssMatcher) Search(feed *search.Feed, searchTerm string) ([]*search.Result, error) {
66+
var results []*search.Result
6467

65-
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", m.Type, m.Name, m.Uri)
68+
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", feed.Type, feed.Name, feed.Uri)
6669

6770
// Retrieve the data to search.
68-
document, err := m.retrieve()
71+
document, err := m.retrieve(feed)
6972
if err != nil {
7073
return nil, err
7174
}
@@ -79,7 +82,7 @@ func (m *rssMatcher) Search(searchTerm string) ([]*match.Result, error) {
7982

8083
// If we found a match save the result.
8184
if matched {
82-
results = append(results, &match.Result{
85+
results = append(results, &search.Result{
8386
Field: "Title",
8487
Content: channelItem.Title,
8588
})
@@ -93,7 +96,7 @@ func (m *rssMatcher) Search(searchTerm string) ([]*match.Result, error) {
9396

9497
// If we found a match save the result.
9598
if matched {
96-
results = append(results, &match.Result{
99+
results = append(results, &search.Result{
97100
Field: "Description",
98101
Content: channelItem.Description,
99102
})
@@ -104,13 +107,13 @@ func (m *rssMatcher) Search(searchTerm string) ([]*match.Result, error) {
104107
}
105108

106109
// retrieve performs a HTTP Get request for the rss feed and decodes the results.
107-
func (m *rssMatcher) retrieve() (*document, error) {
108-
if m.Uri == "" {
110+
func (m *rssMatcher) retrieve(feed *search.Feed) (*document, error) {
111+
if feed.Uri == "" {
109112
return nil, errors.New("No rss feed uri provided")
110113
}
111114

112115
// Retrieve the rss feed document from the web.
113-
resp, err := http.Get(m.Uri)
116+
resp, err := http.Get(feed.Uri)
114117
if err != nil {
115118
return nil, err
116119
}

‎chapter2/sample/search/default.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package search
2+
3+
// defaultMatcher implements the defaut matcher.
4+
type defaultMatcher struct{}
5+
6+
// init registered the matcher with the program
7+
func init() {
8+
Register("default", &defaultMatcher{})
9+
}
10+
11+
// Search implements the behavior for the default matcher.
12+
func (m *defaultMatcher) Search(feed *Feed, searchTerm string) ([]*Result, error) {
13+
return nil, nil
14+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package match
1+
package search
22

33
import (
44
"encoding/json"
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package match
1+
package search
22

33
import (
44
"fmt"
@@ -13,13 +13,14 @@ type Result struct {
1313

1414
// Matcher defines the behavior required by the Search function.
1515
type Matcher interface {
16-
Search(searchTerm string) ([]*Result, error)
16+
Search(feed *Feed, searchTerm string) ([]*Result, error)
1717
}
1818

19-
// Match pulls down each feed looking for the search term.
20-
func Match(matcher Matcher, searchTerm string, results chan<- *Result) {
21-
// Search the data for the search term.
22-
searchResults, err := matcher.Search(searchTerm)
19+
// Match is launched as a goroutine for each indidivudal feed to perform
20+
// the specific search logic for the feed type.
21+
func Match(matcher Matcher, feed *Feed, searchTerm string, results chan<- *Result) {
22+
// Perform the search agains the specified matcher.
23+
searchResults, err := matcher.Search(feed, searchTerm)
2324
if err != nil {
2425
log.Println(err)
2526
return
Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
1-
package main
1+
package search
22

33
import (
44
"log"
55
"sync"
6+
)
7+
8+
var (
9+
// Used to synchronize access to the matchers map.
10+
lock sync.Mutex
611

7-
"github.com/goinaction/code/src/chapter2/search/match"
8-
"github.com/goinaction/code/src/chapter2/search/matchers"
12+
// A map of registered matchers for searching
13+
matchers map[string]Matcher = make(map[string]Matcher)
914
)
1015

11-
// main is the entry point for the program.
12-
func main() {
13-
// Search term we are looking for.
14-
searchTerm := "president"
16+
// Register is called to register a matcher for use
17+
// by the program.
18+
func Register(feedType string, matcher Matcher) {
19+
// Enter a critical section and schedule the unlock.
20+
lock.Lock()
21+
defer lock.Unlock()
1522

23+
// Assign the matcher to the specified key.
24+
log.Println("Register", feedType)
25+
matchers[feedType] = matcher
26+
}
27+
28+
// Run performs the search logic.
29+
func Run(searchTerm string) {
1630
// RetrieveFeed returns the list of feeds to search through.
17-
feeds, err := match.RetrieveFeed()
31+
feeds, err := RetrieveFeed()
1832
if err != nil {
1933
log.Fatal(err)
2034
}
2135

2236
// Create a channel to receive match results to display.
23-
results := make(chan *match.Result)
37+
results := make(chan *Result)
2438

2539
// Setup a wait group so we can process all the feeds.
2640
var waitGroup sync.WaitGroup
@@ -32,11 +46,18 @@ func main() {
3246
// Launch a goroutine for each feed to find the results.
3347
for _, feed := range feeds {
3448
// Create a matcher for the search.
35-
matcher := matchers.NewMatcher(feed)
49+
matcher, ok := matchers[feed.Type]
50+
if !ok {
51+
matcher = matchers["default"]
52+
}
53+
54+
// Make a copy of the value to give each goroutine
55+
// there own copy of the value.
56+
find := feed
3657

3758
// Launch the goroutine to perform the search.
3859
go func() {
39-
match.Match(matcher, searchTerm, results)
60+
Match(matcher, find, searchTerm, results)
4061
waitGroup.Done()
4162
}()
4263
}
@@ -53,5 +74,5 @@ func main() {
5374

5475
// Start displaying results as they are avaiable and
5576
// return after the final result is displayed.
56-
match.Display(results)
77+
Display(results)
5778
}

‎chapter2/search/matchers/matchers.go‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)