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 c9221c1..d330ae2 100644 --- a/search.go +++ b/search.go @@ -1,93 +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 } -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, parameters SearchParameters) { - 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 98fe56a..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") + } +}