Skip to content

Commit 75b0348

Browse files
committed
Merge pull request #3 from davecheney/001-dave-feedback
001 dave feedback
2 parents 57d3aa4 + 1e10844 commit 75b0348

5 files changed

Lines changed: 54 additions & 69 deletions

File tree

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package data
1+
package main
22

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

12-
type (
13-
// Feed contains information about an rss feed.
14-
Feed struct {
15-
Name string `json:"site"`
16-
Uri string `json:"link"`
17-
Type string `json:"type"`
18-
}
19-
)
12+
// Feed contains information about an rss feed.
13+
type Feed struct {
14+
Name string `json:"site"`
15+
Uri string `json:"link"`
16+
Type string `json:"type"`
17+
}
2018

2119
// Load retrieves and unmarshals the data for the program.
2220
func Load() ([]Feed, error) {
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
package feed
1+
package main
22

33
import (
4+
"fmt"
45
"log"
56
)
67

7-
type (
8-
// Result contains the result of a search.
9-
Result struct {
10-
Field string
11-
Content string
12-
}
8+
// Result contains the result of a search.
9+
type Result struct {
10+
Field string
11+
Content string
12+
}
1313

14-
// Matcher defines the behavior required by the Search function.
15-
Matcher interface {
16-
Match(searchTerm string) ([]Result, error)
17-
}
18-
)
14+
// Matcher defines the behavior required by the Search function.
15+
type Matcher interface {
16+
Match(searchTerm string) ([]Result, error)
17+
}
1918

2019
// Search pulls down each feed looking for the search term.
2120
func Search(matcher Matcher, searchTerm string, results chan<- Result) {
@@ -37,6 +36,6 @@ func Display(results chan Result) {
3736
// Wait for results from the different feeds and
3837
// display them.
3938
for result := range results {
40-
log.Printf("%s:\n%s\n\n", result.Field, result.Content)
39+
fmt.Printf("%s:\n%s\n\n", result.Field, result.Content)
4140
}
4241
}

‎src/chapter2/search/main.go‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@ package main
22

33
import (
44
"log"
5-
"os"
65
"sync"
7-
8-
"github.com/goinaction/code/src/chapter2/search/data"
9-
search "github.com/goinaction/code/src/chapter2/search/feed"
10-
"github.com/goinaction/code/src/chapter2/search/rss"
116
)
127

13-
// init is called before main.
14-
func init() {
15-
// Change the log to write to stdout instead
16-
// of its default device stderr.
17-
log.SetOutput(os.Stdout)
18-
}
19-
208
// NewMatcher is a factory that creates matcher values based
219
// on the type of feed specified.
22-
func NewMatcher(feed data.Feed) search.Matcher {
10+
func NewMatcher(feed Feed) Matcher {
2311
// Create the right type of matcher for this search.
2412
switch feed.Type {
2513
case "rss":
26-
return rss.NewMatcher(&feed)
14+
return &rssMatcher{Feed: feed}
2715

2816
// TODO: Add new Matchers here
2917
}
3018

31-
log.Fatalln("Invalid Feed Type")
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")
3225
return nil
3326
}
3427

@@ -38,16 +31,18 @@ func main() {
3831
searchTerm := "president"
3932

4033
// Load the feeds from the data file.
41-
feeds, err := data.Load()
34+
feeds, err := Load()
4235
if err != nil {
4336
log.Fatal(err)
4437
}
4538

4639
// Create a channel to receive the results on.
47-
results := make(chan search.Result)
40+
results := make(chan Result)
4841

4942
// Setup a wait group so we can process all the feeds.
5043
var waitGroup sync.WaitGroup
44+
45+
// TODO: needs a comment why this is done here rather than inside the loop.
5146
waitGroup.Add(len(feeds))
5247

5348
// Launch a goroutine for each feed to find the results.
@@ -57,13 +52,17 @@ func main() {
5752

5853
// Launch the goroutine to perform the search.
5954
go func() {
60-
search.Search(matcher, searchTerm, results)
55+
Search(matcher, searchTerm, results)
6156
waitGroup.Done()
6257
}()
6358
}
6459

65-
// Launch a goroutine so we can shutdown the program
66-
// once the last feed sends its results.
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+
//
6766
go func() {
6867
// Wait for everything to be processed.
6968
waitGroup.Wait()
@@ -73,5 +72,5 @@ func main() {
7372
}()
7473

7574
// Display the results.
76-
search.Display(results)
75+
Display(results)
7776
}
Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
package rss
1+
package main
22

33
import (
44
"encoding/xml"
55
"errors"
66
"log"
77
"net/http"
88
"regexp"
9-
10-
"github.com/goinaction/code/src/chapter2/search/data"
11-
"github.com/goinaction/code/src/chapter2/search/feed"
129
)
1310

1411
type (
@@ -54,25 +51,16 @@ type (
5451
}
5552
)
5653

57-
type (
58-
// Implements the Matcher interface.
59-
matcher struct {
60-
Feed *data.Feed
61-
}
62-
)
63-
64-
// NewMatcher creates a value of matcher for use.
65-
func NewMatcher(feed *data.Feed) feed.Matcher {
66-
return &matcher{
67-
Feed: feed,
68-
}
54+
// rssMatcher implements the Matcher interface.
55+
type rssMatcher struct {
56+
Feed
6957
}
7058

7159
// Match looks at the document for the specified search term.
72-
func (m *matcher) Match(searchTerm string) ([]feed.Result, error) {
73-
var results []feed.Result
60+
func (m *rssMatcher) Match(searchTerm string) ([]Result, error) {
61+
var results []Result
7462

75-
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", m.Feed.Type, m.Feed.Name, m.Feed.Uri)
63+
log.Printf("Search Feed Type[%s] Site[%s] For Uri[%s]\n", m.Type, m.Name, m.Uri)
7664

7765
// Retrieve the data to search.
7866
document, err := m.retrieve()
@@ -89,7 +77,7 @@ func (m *matcher) Match(searchTerm string) ([]feed.Result, error) {
8977

9078
// If we found a match save the result.
9179
if matched {
92-
results = append(results, feed.Result{
80+
results = append(results, Result{
9381
Field: "Title",
9482
Content: channelItem.Title,
9583
})
@@ -103,7 +91,7 @@ func (m *matcher) Match(searchTerm string) ([]feed.Result, error) {
10391

10492
// If we found a match save the result.
10593
if matched {
106-
results = append(results, feed.Result{
94+
results = append(results, Result{
10795
Field: "Description",
10896
Content: channelItem.Description,
10997
})
@@ -114,24 +102,25 @@ func (m *matcher) Match(searchTerm string) ([]feed.Result, error) {
114102
}
115103

116104
// retrieve performs a HTTP Get request for the rss feed and unmarshals the results.
117-
func (m *matcher) retrieve() (*document, error) {
118-
if m.Feed.Uri == "" {
105+
func (m *rssMatcher) retrieve() (*document, error) {
106+
if m.Uri == "" {
119107
return nil, errors.New("No rss feed uri provided")
120108
}
121109

122110
// Retrieve the rss feed document from the web.
123-
resp, err := http.Get(m.Feed.Uri)
111+
resp, err := http.Get(m.Uri)
124112
if err != nil {
125113
return nil, err
126114
}
127115

128116
// Close the response once we return from the function.
129117
defer resp.Body.Close()
130118

119+
// TODO: check response status, don't process a non 200 response body.
120+
131121
// Unmarshal the rss feed document into our struct type.
132122
var document document
133-
err = xml.NewDecoder(resp.Body).Decode(&document)
134-
if err != nil {
123+
if err := xml.NewDecoder(resp.Body).Decode(&document); err != nil {
135124
return nil, err
136125
}
137126

‎src/chapter2/search/search‎

6.25 MB
Binary file not shown.

0 commit comments

Comments
 (0)