Browse Source

Provide text match metadata

Kyle Kelley 12 years ago
committed by Will Norris
parent
commit
e840a8a0e6
5 changed files with 105 additions and 5 deletions
  1. +4
    -0
      github/issues.go
  2. +4
    -0
      github/repos.go
  3. +34
    -5
      github/search.go
  4. +59
    -0
      github/search_test.go
  5. +4
    -0
      github/users.go

+ 4
- 0
github/issues.go View File

@ -34,6 +34,10 @@ type Issue struct {
URL *string `json:"url,omitempty"`
HTMLURL *string `json:"html_url,omitempty"`
Milestone *Milestone `json:"milestone,omitempty"`
// TextMatches is only populated from search results that request text matches
// See: search.go and https://developer.github.com/v3/search/#text-match-metadata
TextMatches []TextMatch `json:"text_matches,omitempty"`
}
func (i Issue) String() string {


+ 4
- 0
github/repos.go View File

@ -91,6 +91,10 @@ type Repository struct {
TagsURL *string `json:"tags_url,omitempty"`
TreesURL *string `json:"trees_url,omitempty"`
TeamsURL *string `json:"teams_url,omitempty"`
// TextMatches is only populated from search results that request text matches
// See: search.go and https://developer.github.com/v3/search/#text-match-metadata
TextMatches []TextMatch `json:"text_matches,omitempty"`
}
func (r Repository) String() string {


+ 34
- 5
github/search.go View File

@ -34,6 +34,9 @@ type SearchOptions struct {
// desc. Default is desc.
Order string `url:"order,omitempty"`
// Whether to retrieve text match metadata with a query
TextMatch bool `url:"-"`
ListOptions
}
@ -82,6 +85,25 @@ func (s *SearchService) Users(query string, opt *SearchOptions) (*UsersSearchRes
return result, resp, err
}
// Match represents a single text match.
type Match struct {
Text *string `json:"text,omitempty"`
Indices []int `json:"indices,omitempty"`
}
// TextMatch represents a text match for a SearchResult
type TextMatch struct {
ObjectURL *string `json:"object_url,omitempty"`
ObjectType *string `json:"object_type,omitempty"`
Property *string `json:"property,omitempty"`
Fragment *string `json:"fragment,omitempty"`
Matches []Match `json:"matches,omitempty"`
}
func (tm TextMatch) String() string {
return Stringify(tm)
}
// CodeSearchResult represents the result of an code search.
type CodeSearchResult struct {
Total *int `json:"total_count,omitempty"`
@ -90,11 +112,12 @@ type CodeSearchResult struct {
// CodeResult represents a single search result.
type CodeResult struct {
Name *string `json:"name,omitempty"`
Path *string `json:"path,omitempty"`
SHA *string `json:"sha,omitempty"`
HTMLURL *string `json:"html_url,omitempty"`
Repository *Repository `json:"repository,omitempty"`
Name *string `json:"name,omitempty"`
Path *string `json:"path,omitempty"`
SHA *string `json:"sha,omitempty"`
HTMLURL *string `json:"html_url,omitempty"`
Repository *Repository `json:"repository,omitempty"`
TextMatches []TextMatch `json:"text_matches,omitempty"`
}
func (c CodeResult) String() string {
@ -125,5 +148,11 @@ func (s *SearchService) search(searchType string, query string, opt *SearchOptio
return nil, err
}
if opt.TextMatch {
// Accept header defaults to "application/vnd.github.v3+json"
// We change it here to fetch back text-match metadata
req.Header.Set("Accept", "application/vnd.github.v3.text-match+json")
}
return s.client.Do(req, result)
}

+ 59
- 0
github/search_test.go View File

@ -135,3 +135,62 @@ func TestSearchService_Code(t *testing.T) {
t.Errorf("Search.Code returned %+v, want %+v", result, want)
}
}
func TestSearchService_CodeTextMatch(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/search/code", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
textMatchResponse := `
{
"total_count": 1,
"items": [
{
"name":"gopher1",
"text_matches": [
{
"fragment": "I'm afraid my friend what you have found\nIs a gopher who lives to feed",
"matches": [
{
"text": "gopher",
"indices": [
14,
21
]
}
]
}
]
}
]
}
`
fmt.Fprint(w, textMatchResponse)
})
opts := &SearchOptions{Sort: "forks", Order: "desc", ListOptions: ListOptions{Page: 2, PerPage: 2}, TextMatch: true}
result, _, err := client.Search.Code("blah", opts)
if err != nil {
t.Errorf("Search.Code returned error: %v", err)
}
wantedCodeResult := CodeResult{
Name: String("gopher1"),
TextMatches: []TextMatch{TextMatch{
Fragment: String("I'm afraid my friend what you have found\nIs a gopher who lives to feed"),
Matches: []Match{Match{Text: String("gopher"), Indices: []int{14, 21}}},
},
},
}
want := &CodeSearchResult{
Total: Int(1),
CodeResults: []CodeResult{wantedCodeResult},
}
if !reflect.DeepEqual(result, want) {
t.Errorf("Search.Code returned %+v, want %+v", result, want)
}
}

+ 4
- 0
github/users.go View File

@ -55,6 +55,10 @@ type User struct {
ReposURL *string `json:"repos_url,omitempty"`
StarredURL *string `json:"starred_url,omitempty"`
SubscriptionsURL *string `json:"subscriptions_url,omitempty"`
// TextMatches is only populated from search results that request text matches
// See: search.go and https://developer.github.com/v3/search/#text-match-metadata
TextMatches []TextMatch `json:"text_matches,omitempty"`
}
func (u User) String() string {


Loading…
Cancel
Save