Skip to content

Commit 8bafc9f

Browse files
Refactored to create packages for the different matchers and business logic.
1 parent 75b0348 commit 8bafc9f

8 files changed

Lines changed: 114 additions & 92 deletions

File tree

‎src/chapter2/search/feed.go‎

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

‎src/chapter2/search/main.go‎

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,55 @@ package main
33
import (
44
"log"
55
"sync"
6-
)
7-
8-
// NewMatcher is a factory that creates matcher values based
9-
// on the type of feed specified.
10-
func NewMatcher(feed Feed) Matcher {
11-
// Create the right type of matcher for this search.
12-
switch feed.Type {
13-
case "rss":
14-
return &rssMatcher{Feed: feed}
15-
16-
// TODO: Add new Matchers here
17-
}
186

19-
// TODO: make a matcher implementation that never matches anything
20-
// then return that.
21-
// Right now your program will do this when you hit an atom feed
22-
// "Invalid Feed Type"
23-
// Panic: call to nil interface method.
24-
log.Fatal("Invalid Feed Type")
25-
return nil
26-
}
7+
"github.com/goinaction/code/src/chapter2/search/match"
8+
"github.com/goinaction/code/src/chapter2/search/matchers"
9+
)
2710

2811
// main is the entry point for the program.
2912
func main() {
3013
// Search term we are looking for.
3114
searchTerm := "president"
3215

33-
// Load the feeds from the data file.
34-
feeds, err := Load()
16+
// RetrieveFeed returns the list of feeds to search through.
17+
feeds, err := match.RetrieveFeed()
3518
if err != nil {
3619
log.Fatal(err)
3720
}
3821

39-
// Create a channel to receive the results on.
40-
results := make(chan Result)
22+
// Create a channel to receive match results to display.
23+
results := make(chan *match.Result)
4124

4225
// Setup a wait group so we can process all the feeds.
4326
var waitGroup sync.WaitGroup
4427

45-
// TODO: needs a comment why this is done here rather than inside the loop.
28+
// Set the number of goroutines we need to wait for while
29+
// they process the individual feeds.
4630
waitGroup.Add(len(feeds))
4731

4832
// Launch a goroutine for each feed to find the results.
4933
for _, feed := range feeds {
5034
// Create a matcher for the search.
51-
matcher := NewMatcher(feed)
35+
matcher := matchers.NewMatcher(feed)
5236

5337
// Launch the goroutine to perform the search.
5438
go func() {
55-
Search(matcher, searchTerm, results)
39+
match.Match(matcher, searchTerm, results)
5640
waitGroup.Done()
5741
}()
5842
}
5943

60-
// TODO: this is correct, but needs an explanation, ie
61-
// why not write
62-
// waitGroup.Wait() ; close(results); search.Display(results)
63-
// or
64-
// go search.Display(results); waitGroup.Wait(); close(results)
65-
//
44+
// Launch a groutine to monitor when all the work is done.
6645
go func() {
6746
// Wait for everything to be processed.
6847
waitGroup.Wait()
6948

70-
// Close the channel.
49+
// Close the channel to signal to the Display
50+
// function that we can exit the program.
7151
close(results)
7252
}()
7353

74-
// Display the results.
75-
Display(results)
54+
// Start displaying results as they are avaiable and
55+
// return after the final result is displayed.
56+
match.Display(results)
7657
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package match
22

33
import (
44
"encoding/json"
@@ -9,15 +9,16 @@ const (
99
dataFile = "data/data.json"
1010
)
1111

12-
// Feed contains information about an rss feed.
12+
// Feed contains information we need to process a feed.
1313
type Feed struct {
1414
Name string `json:"site"`
1515
Uri string `json:"link"`
1616
Type string `json:"type"`
1717
}
1818

19-
// Load retrieves and unmarshals the data for the program.
20-
func Load() ([]Feed, error) {
19+
// RetrieveFeed reads and unmarshals the feed data file
20+
// into a native slice for use.
21+
func RetrieveFeed() ([]*Feed, error) {
2122
// Open the file.
2223
file, err := os.Open(dataFile)
2324
if err != nil {
@@ -29,7 +30,7 @@ func Load() ([]Feed, error) {
2930
defer file.Close()
3031

3132
// Decode the file into a slice of feed values.
32-
var feeds []Feed
33+
var feeds []*Feed
3334
err = json.NewDecoder(file).Decode(&feeds)
3435
if err != nil {
3536
return nil, err

‎src/chapter2/search/match/match.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package match
2+
3+
import (
4+
"fmt"
5+
"log"
6+
)
7+
8+
// Result contains the result of a search.
9+
type Result struct {
10+
Field string
11+
Content string
12+
}
13+
14+
// Matcher defines the behavior required by the Search function.
15+
type Matcher interface {
16+
Search(searchTerm string) ([]*Result, error)
17+
}
18+
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)
23+
if err != nil {
24+
log.Println(err)
25+
return
26+
}
27+
28+
// Write the results to the channel.
29+
for _, result := range searchResults {
30+
results <- result
31+
}
32+
}
33+
34+
// Display writes results to the console window as they
35+
// are received by the indivdual goroutines.
36+
func Display(results chan *Result) {
37+
// The channel blocks until a result is written to the channel.
38+
// Once the channel is close the for loop terminates.
39+
for result := range results {
40+
fmt.Printf("%s:\n%s\n\n", result.Field, result.Content)
41+
}
42+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package matchers
2+
3+
import (
4+
"github.com/goinaction/code/src/chapter2/search/match"
5+
)
6+
7+
// defaultMatcher implements the Matcher interface.
8+
type defaultMatcher struct {
9+
*match.Feed
10+
}
11+
12+
// Search looks at the document for the specified search term.
13+
func (m *defaultMatcher) Search(searchTerm string) ([]*match.Result, error) {
14+
return nil, nil
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package matchers
2+
3+
import (
4+
"github.com/goinaction/code/src/chapter2/search/match"
5+
)
6+
7+
// NewMatcher is a factory that creates matcher values based
8+
// on the type of feed specified.
9+
func NewMatcher(feed *match.Feed) match.Matcher {
10+
// Create the right type of matcher for this search.
11+
switch feed.Type {
12+
case "rss":
13+
return &rssMatcher{feed}
14+
15+
default:
16+
return &defaultMatcher{feed}
17+
}
18+
}
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package main
1+
package matchers
22

33
import (
44
"encoding/xml"
55
"errors"
66
"log"
77
"net/http"
88
"regexp"
9+
10+
"github.com/goinaction/code/src/chapter2/search/match"
911
)
1012

1113
type (
@@ -53,12 +55,12 @@ type (
5355

5456
// rssMatcher implements the Matcher interface.
5557
type rssMatcher struct {
56-
Feed
58+
*match.Feed
5759
}
5860

59-
// Match looks at the document for the specified search term.
60-
func (m *rssMatcher) Match(searchTerm string) ([]Result, error) {
61-
var results []Result
61+
// 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
6264

6365
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", m.Type, m.Name, m.Uri)
6466

@@ -77,7 +79,7 @@ func (m *rssMatcher) Match(searchTerm string) ([]Result, error) {
7779

7880
// If we found a match save the result.
7981
if matched {
80-
results = append(results, Result{
82+
results = append(results, &match.Result{
8183
Field: "Title",
8284
Content: channelItem.Title,
8385
})
@@ -91,7 +93,7 @@ func (m *rssMatcher) Match(searchTerm string) ([]Result, error) {
9193

9294
// If we found a match save the result.
9395
if matched {
94-
results = append(results, Result{
96+
results = append(results, &match.Result{
9597
Field: "Description",
9698
Content: channelItem.Description,
9799
})
@@ -101,7 +103,7 @@ func (m *rssMatcher) Match(searchTerm string) ([]Result, error) {
101103
return results, nil
102104
}
103105

104-
// retrieve performs a HTTP Get request for the rss feed and unmarshals the results.
106+
// retrieve performs a HTTP Get request for the rss feed and decodes the results.
105107
func (m *rssMatcher) retrieve() (*document, error) {
106108
if m.Uri == "" {
107109
return nil, errors.New("No rss feed uri provided")
@@ -116,9 +118,13 @@ func (m *rssMatcher) retrieve() (*document, error) {
116118
// Close the response once we return from the function.
117119
defer resp.Body.Close()
118120

119-
// TODO: check response status, don't process a non 200 response body.
121+
// Check the status code for a 200 so we know we have received a
122+
// proper response.
123+
if resp.StatusCode != 200 {
124+
return nil, err
125+
}
120126

121-
// Unmarshal the rss feed document into our struct type.
127+
// Decode the rss feed document into our struct type.
122128
var document document
123129
if err := xml.NewDecoder(resp.Body).Decode(&document); err != nil {
124130
return nil, err

‎src/chapter2/search/search‎

-6.25 MB
Binary file not shown.

0 commit comments

Comments
 (0)