Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions discogs.go
Original file line number Diff line number Diff line change
@@ -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")
}
201 changes: 119 additions & 82 deletions search.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 29 additions & 19 deletions search_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}