From 5064d1b5941644b11a84543521a332a428d38c6f Mon Sep 17 00:00:00 2001 From: Peter Novotnak Date: Fri, 25 Dec 2015 04:14:32 -0800 Subject: [PATCH 1/2] hanging it up for the night --- search.go | 15 +++++++++------ search_test.go | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/search.go b/search.go index c9221c1..9da0e21 100644 --- a/search.go +++ b/search.go @@ -3,9 +3,9 @@ import "errors" const URI = "/database/search" -type SearchParameters struct { +type searchParameters struct { // See; https://www.discogs.com/developers/#page:database,header:database-search - Query string `json:"q,omitempty"` + Query string `json:"q,omitempty"` // Your search query // string (optional) Example: nirvana Type string `choices:"release,master,artist,label"` @@ -77,16 +77,19 @@ type SearchParameters struct { // Search contributor usernames. } -func (SearchParameters) Validate() (error) { +type SearchResult struct {} + + +func (searchParameters) Validate() (error) { return errors.New("invalid search parameters") } -func (SearchParameters) Run() (results interface{}, err error) { +func (searchParameters) Run() (results interface{}, err error) { return nil, errors.New("search failed") } -func Search(query string, parameters SearchParameters) { - search := SearchParameters{ +func Search(query string, params []string) { + search := searchParameters{ Query: query, } search.Run() diff --git a/search_test.go b/search_test.go index 98fe56a..c7788a8 100644 --- a/search_test.go +++ b/search_test.go @@ -6,7 +6,7 @@ import ( ) func TestParameters(*testing.T) { - p := SearchParameters{} + p := searchParameters{} typ := reflect.TypeOf(p) for i := 0; i < typ.NumField(); i++ { f := typ.Field(i) @@ -18,7 +18,7 @@ func TestParameters(*testing.T) { } func TestSearchFunction(*testing.T) { - params := SearchParameters{ + params := searchParameters{ Query: "Blakroc", Type: "album", } From a460e3b0f3950c3fc5d9485889e9fdf142de17da Mon Sep 17 00:00:00 2001 From: Peter Novotnak Date: Sun, 27 Dec 2015 17:00:50 -0800 Subject: [PATCH 2/2] search input ironed out, validation being tested --- discogs.go | 5 ++ search.go | 204 ++++++++++++++++++++++++++++--------------------- search_test.go | 48 +++++++----- 3 files changed, 153 insertions(+), 104 deletions(-) diff --git a/discogs.go b/discogs.go index cb5231e..0fae275 100644 --- a/discogs.go +++ b/discogs.go @@ -1,3 +1,8 @@ package discogs +import "errors" +type API struct {} +func New(key string, secret string) (api *API, err error) { + return api, errors.New("empty method") +} diff --git a/search.go b/search.go index 9da0e21..d330ae2 100644 --- a/search.go +++ b/search.go @@ -1,96 +1,130 @@ package discogs -import "errors" +import ( + "errors" + "fmt" +) -const URI = "/database/search" +const searchURI = "/database/search" -type searchParameters struct { - // See; https://www.discogs.com/developers/#page:database,header:database-search - Query string `json:"q,omitempty"` - // Your search query - // string (optional) Example: nirvana - Type string `choices:"release,master,artist,label"` - // string (optional) Example: release - // String. One of release, master, artist, label - - Title string - // Search by combined “Artist Name - Release Title” title field. - // string (optional) Example: nirvana - nevermind - - ReleaseTitle string - // string (optional) Example: nevermind - // Search release titles. - - Credit string - // string (optional) Example: kurt - // Search release credits. - - Artist string - // string (optional) Example: nirvana - // Search artist names. - - Anv string - // string (optional) Example: nirvana - // Search artist ANV. - - Label string - // string (optional) Example: dgc - // Search label names. - - Genre string - // string (optional) Example: rock - // Search genres. - - Style string - // string (optional) Example: grunge - // Search styles. - - Country string - // string (optional) Example: canada - // Search release country. - - Year string - // string (optional) Example: 1991 - // Search release year. - - Format string - // string (optional) Example: album - // Search formats. - - Catno string - // string (optional) Example: DGCD-24425 - // Search catalog number. - - Barcode string - // string (optional) Example: 7 2064-24425-2 4 - // Search barcodes. - - Track string - // string (optional) Example: smells like teen spirit - // Search track titles. - - Submitter string - // string (optional) Example: milKt - // Search submitter username. - - Contributor string - // string (optional) Example: jerome99 - // Search contributor usernames. +type searchParameter struct { + // choices is a map so that we might query string choices membership + // without iterating over it (boolean is not currently used) + Choices map[string]bool } -type SearchResult struct {} - - -func (searchParameters) Validate() (error) { - return errors.New("invalid search parameters") +// See; https://www.discogs.com/developers/#page:database,header:database-search +var searchParameters = map[string]searchParameter { + "query": searchParameter{ + // Your search query + // string (optional) Example: nirvana + }, + "type": searchParameter{ + // string (optional) Example: release + // String. One of release, master, artist, label + Choices: map[string]bool{ + "release": true, + "master": true, + "artist": true, + "label": true, + }, + }, + "title": searchParameter{ + // Search by combined “Artist Name - Release Title” title field. + // string (optional) Example: nirvana - nevermind + }, + "release_title": searchParameter{ + // string (optional) Example: nevermind + // Search release titles. + }, + "credit": searchParameter{ + // string (optional) Example: kurt + // Search release credits. + }, + "artist": searchParameter{ + // string (optional) Example: nirvana + // Search artist names. + }, + "anv": searchParameter{ + // string (optional) Example: nirvana + // Search artist ANV. + }, + "label": searchParameter{ + // string (optional) Example: dgc + // Search label names. + }, + "genre": searchParameter{ + // string (optional) Example: rock + // Search genres. + }, + "style": searchParameter{ + // string (optional) Example: grunge + // Search styles. + }, + "country": searchParameter{ + // string (optional) Example: canada + // Search release country. + }, + "year": searchParameter{ + // string (optional) Example: 1991 + // Search release year. + }, + "format": searchParameter{ + // string (optional) Example: album + // Search formats. + }, + "catno": searchParameter{ + // string (optional) Example: DGCD-24425 + // Search catalog number. + }, + "barcode": searchParameter{ + // string (optional) Example: 7 2064-24425-2 4 + // Search barcodes. + }, + "track": searchParameter{ + // string (optional) Example: smells like teen spirit + // Search track titles. + }, + "submitter": searchParameter{ + // string (optional) Example: milKt + // Search submitter username. + }, + "contributor": searchParameter{ + // string (optional) Example: jerome99 + // Search contributor usernames. + }, } -func (searchParameters) Run() (results interface{}, err error) { - return nil, errors.New("search failed") +func validateQuery(params map[string]string) (error) { + for p, v := range(params) { + // check parameter name + param, ok := searchParameters[p] + if !ok { + return errors.New( + fmt.Sprintf("invalid parameter: %v (value was %v)", p, v)) + } + // check parameter value + if param.Choices != nil { + if _, ok := param.Choices[v]; !ok { + return errors.New( + fmt.Sprintf("invalid choice \"%v\" for parameter \"%v\"", v, p)) + } + } + } + // check parameter values + for k, v := range(params) { + if _, ok := searchParameters[k]; !ok { + return errors.New( + fmt.Sprintf("invalid parameter: %v (value was %v)", k, v)) + } + } + return nil } -func Search(query string, params []string) { - search := searchParameters{ - Query: query, +func (API) Search(query string, params map[string]string) (interface{}, error) { + // params should be a set of two-member arrays (key, value) + if err := validateQuery(params); err != nil { + return nil, err } - search.Run() + + return params, nil } diff --git a/search_test.go b/search_test.go index c7788a8..983f792 100644 --- a/search_test.go +++ b/search_test.go @@ -1,32 +1,42 @@ package discogs import ( "testing" - "reflect" - "fmt" ) -func TestParameters(*testing.T) { - p := searchParameters{} - typ := reflect.TypeOf(p) - for i := 0; i < typ.NumField(); i++ { - f := typ.Field(i) - choices := f.Tag.Get("choices") - if len(choices) != 0 { - fmt.Println(f.Name, "choices:", choices) - } - } -} - func TestSearchFunction(*testing.T) { - params := searchParameters{ - Query: "Blakroc", - Type: "album", + api := API{} + params := map[string]string{ + "type": "release", } - results, err := params.Run() + results, err := api.Search("blackroc", params) if err != nil { - panic("search error") + panic(err) } if results == nil { panic("empty result set") } } + +func TestSearchInputValidation(*testing.T) { + api := API{} + badParam := map[string]string{ + "typeee": "release", + } + results, err := api.Search("blackroc", badParam) + if err == nil { + panic("invalid search parameter given, but was not caught") + } + if results != nil { + panic("result set given for bad search") + } + invalidChoice := map[string]string{ + "type": "album", + } + results, err = api.Search("blackroc", invalidChoice) + if err == nil { + panic("invalid choice given for parameter, but was not caught") + } + if results != nil { + panic("result set given for bad search") + } +}