@@ -2,7 +2,6 @@ package find
22
33import (
44 "log"
5- "regexp"
65 "sync"
76
87 "github.com/goinaction/code/src/chapter2/search/feeds"
2524type (
2625 // Matcher defines the behavior required by the Search function.
2726 Matcher interface {
28- Retrieve (site feeds.Site ) ([]SearchData , error )
27+ Match (site * feeds.Site , searchTerm string ) ([]Result , error )
2928 }
3029)
3130
@@ -38,15 +37,8 @@ func Search(matcher Matcher, searchTerm string, site feeds.Site, result chan Res
3837
3938 log .Printf ("Search Feed Type[%s] Site[%s] For Uri[%s]\n " , site .Type , site .Name , site .Uri )
4039
41- // Retrieve the search data.
42- searchData , err := matcher .Retrieve (site )
43- if err != nil {
44- log .Printf ("%s : %s" , site .Uri , err )
45- return
46- }
47-
4840 // Search the data for the search term.
49- searchResults , err := match ( searchData , searchTerm )
41+ searchResults , err := matcher . Match ( & site , searchTerm )
5042 if err != nil {
5143 log .Printf ("%s : %s" , site .Uri , err )
5244 return
@@ -57,40 +49,3 @@ func Search(matcher Matcher, searchTerm string, site feeds.Site, result chan Res
5749 result <- searchResult
5850 }
5951}
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- }
0 commit comments