18 Commits

Author SHA1 Message Date
irlndts
618b882634 Use go mod 2018-12-11 16:47:31 +03:00
Artem Piskun
a4b6e4193a Merge pull request #27 from pluyckx/master
Add 'Artists' field to 'Track'
2018-04-30 21:27:27 +03:00
Philip Luyckx
e1e0446c29 Add 'Artists' field to 'Track'
Some tracks contain different artists than specified in the release. In
this case there is a field 'artists' in the JSON response. This field is
added to the 'Track' struct. When the field 'artists' is available in the
JSON response, the length of 'Artists' is not 0. Otherwise the 'Artists'
from the release should be used.

Signed-off-by: Philip Luyckx <philip.luyckx@gmail.com>
2018-04-30 10:37:04 +02:00
irlndts
dd501a3f65 fix 2018-04-10 15:39:54 +03:00
irlndts
110a37661a goreportcard 2018-03-28 20:18:24 +03:00
irlndts
3775bc39c7 travis pic 2018-03-28 20:13:31 +03:00
irlndts
e9ad282056 added travis-ci.yml 2018-03-28 20:11:47 +03:00
Artem Piskun
31a2e3b959 Merge pull request #26 from irlndts/APIErrors 2018-03-24 16:19:05 +03:00
irlndts
8e69f9334b Added several tests 2018-03-24 16:17:35 +03:00
irlndts
788518e3e2 Added several tests 2018-03-24 16:14:54 +03:00
irlndts
0a3a17b69d Lint errors fixed 2018-03-24 13:46:20 +03:00
Artem Piskun
ebfd052f67 Merge pull request #24 from irlndts/DSCGS-23
Move services to database
2018-03-21 21:18:54 +03:00
irlndts
c526ccd692 Move services to database 2018-03-21 21:17:40 +03:00
Artem Piskun
91d056b186 Merge pull request #22 from irlndts/DSCGS-20
Fix govet errors
2018-03-21 20:58:14 +03:00
irlndts
441de35813 Fix govet errors 2018-03-21 20:57:39 +03:00
Artem Piskun
872ff94673 Merge pull request #21 from irlndts/DSCGS-19
DSCGS-19 fix lint
2018-03-20 13:57:39 +03:00
irlndts
afd778f11c Cleanup 2018-03-20 13:56:17 +03:00
irlndts
dd29f70850 DSCGS-19 Fix golint errors 2018-03-17 23:12:25 +03:00
19 changed files with 318 additions and 655 deletions

3
.travis.yml Normal file
View File

@@ -0,0 +1,3 @@
language: go
go:
- "1.10"

View File

@@ -1,17 +1,20 @@
# REST API 2.0 Discogs.com client
[![Build Status](https://travis-ci.org/irlndts/go-discogs.svg?branch=master)](https://travis-ci.org/irlndts/go-discogs)[![Go Report Card](https://goreportcard.com/badge/github.com/irlndts/go-discogs)](https://goreportcard.com/report/github.com/irlndts/go-discogs)
go-discogs is a Go client library for the [Discogs API](https://www.discogs.com/developers/). Check the usage section to see how to access the Discogs API.
### Feauteres
* Database
* [Releases](#releases)
* Release Rating
* Master Releases
* Release Versions
* Master Versions
* Artists
* Artist Releases
* Label
* All Label Releases
* [Search](#search)
* [Search](#search)
Install
--------
@@ -37,7 +40,7 @@ client, err := discogs.NewClient(&discogs.Options{
#### Releases
```go
release, _ := client.Release.Release(9893847)
release, _ := client.Database.Release(9893847)
fmt.Println(release.Artists[0].Name, " - ", release.Title)
// St. Petersburg Ska-Jazz Review - Elephant Riddim
```

View File

@@ -1,52 +0,0 @@
package discogs
import "strconv"
// ArtistService ...
type ArtistService struct {
url string
}
func newArtistService(url string) *ArtistService {
return &ArtistService{
url: url,
}
}
// Artist ...
type Artist struct {
Namevariations []string `json:"namevariations"`
Profile string `json:"profile"`
Releases_url string `json:"releases_url"`
Resource_url string `json:"resource_url"`
Uri string `json:"uri"`
Urls []string `json:"urls"`
Data_quality string `json:"data_quality"`
Id int `json:"id"`
Images []Image `json:"images"`
Members []Member `json:"members"`
}
// Artist represents a person in the discogs database
func (s *ArtistService) Artist(artistID int) (*Artist, error) {
var artist *Artist
if err := request(s.url+strconv.Itoa(artistID), nil, &artist); err != nil {
return nil, err
}
return artist, nil
}
// ArtistReleases ...
type ArtistReleases struct {
Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
// Releases returns a list of releases and masters associated with the artist.
func (s *ArtistService) Releases(artistID int, pagination *Pagination) (*ArtistReleases, error) {
var releases *ArtistReleases
if err := request(s.url+strconv.Itoa(artistID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
}
return releases, nil
}

214
database.go Normal file
View File

@@ -0,0 +1,214 @@
package discogs
import (
"net/url"
"strconv"
)
const (
releasesURI = "/releases/"
artistsURI = "/artists/"
labelsURI = "/labels/"
mastersURI = "/masters/"
)
// DatabaseService ...
type DatabaseService struct {
url string
currency string
}
func newDatabaseService(url string, currency string) *DatabaseService {
return &DatabaseService{
url: url,
currency: currency,
}
}
// Release serves relesase response from discogs
type Release struct {
Title string `json:"title"`
ID int `json:"id"`
Artists []ArtistSource `json:"artists"`
DataQuality string `json:"data_quality"`
Thumb string `json:"thumb"`
Community Community `json:"community"`
Companies []Company `json:"companies"`
Country string `json:"country"`
DateAdded string `json:"date_added"`
DateChanged string `json:"date_changed"`
EstimatedWeight int `json:"estimated_weight"`
ExtraArtists []ArtistSource `json:"extraartists"`
FormatQuantity int `json:"format_quantity"`
Formats []Format `json:"formats"`
Genres []string `json:"genres"`
Identifiers []Identifier `json:"identifiers"`
Images []Image `json:"images"`
Labels []LabelSource `json:"labels"`
LowestPrice float64 `json:"lowest_price"`
MasterID int `json:"master_id"`
MasterURL string `json:"master_url"`
Notes string `json:"notes,omitempty"`
NumForSale int `json:"num_for_sale,omitempty"`
Released string `json:"released"`
ReleasedFormatted string `json:"released_formatted"`
ResourceURL string `json:"resource_url"`
// Series
Status string `json:"status"`
Styles []string `json:"styles"`
Tracklist []Track `json:"tracklist"`
URI string `json:"uri"`
Videos []Video `json:"videos"`
Year int `json:"year"`
}
// Release returns release by release's ID
func (s *DatabaseService) Release(releaseID int) (*Release, error) {
params := url.Values{}
params.Set("curr_abbr", s.currency)
var release *Release
if err := request(s.url+releasesURI+strconv.Itoa(releaseID), params, &release); err != nil {
return nil, err
}
return release, nil
}
// ReleaseRating serves response for community release rating request
type ReleaseRating struct {
ID int `json:"release_id"`
Rating Rating `json:"rating"`
}
// ReleaseRating retruns community release rating
func (s *DatabaseService) ReleaseRating(releaseID int) (*ReleaseRating, error) {
var rating *ReleaseRating
if err := request(s.url+releasesURI+strconv.Itoa(releaseID)+"/rating", nil, &rating); err != nil {
return nil, err
}
return rating, nil
}
// Artist ...
type Artist struct {
Namevariations []string `json:"namevariations"`
Profile string `json:"profile"`
ReleasesURL string `json:"releases_url"`
ResourceURL string `json:"resource_url"`
URI string `json:"uri"`
URLs []string `json:"urls"`
DataQuality string `json:"data_quality"`
ID int `json:"id"`
Images []Image `json:"images"`
Members []Member `json:"members"`
}
// Artist represents a person in the discogs database
func (s *DatabaseService) Artist(artistID int) (*Artist, error) {
var artist *Artist
if err := request(s.url+artistsURI+strconv.Itoa(artistID), nil, &artist); err != nil {
return nil, err
}
return artist, nil
}
// ArtistReleases ...
type ArtistReleases struct {
Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
// ArtistReleases returns a list of releases and masters associated with the artist.
func (s *DatabaseService) ArtistReleases(artistID int, pagination *Pagination) (*ArtistReleases, error) {
var releases *ArtistReleases
if err := request(s.url+artistsURI+strconv.Itoa(artistID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
}
return releases, nil
}
// Label resource represents a label, company, recording studio, location,
// or other entity involved with artists and releases.
type Label struct {
Profile string `json:"profile"`
ReleasesURL string `json:"releases_url"`
Name string `json:"name"`
ContactInfo string `json:"contact_info"`
URI string `json:"uri"`
Sublabels []Sublable `json:"sublabels"`
URLs []string `json:"urls"`
Images []Image `json:"images"`
ResourceURL string `json:"resource_url"`
ID int `json:"id"`
DataQuality string `json:"data_quality"`
}
// Label returns a label.
func (s *DatabaseService) Label(labelID int) (*Label, error) {
var label *Label
if err := request(s.url+labelsURI+strconv.Itoa(labelID), nil, &label); err != nil {
return nil, err
}
return label, nil
}
// LabelReleases is a list of Releases associated with the label.
type LabelReleases struct {
Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
// LabelReleases returns a list of Releases associated with the label.
func (s *DatabaseService) LabelReleases(labelID int, pagination *Pagination) (*LabelReleases, error) {
var releases *LabelReleases
if err := request(s.url+labelsURI+strconv.Itoa(labelID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
}
return releases, nil
}
// Master resource represents a set of similar releases.
// Masters (also known as `master releases`) have a `main release` which is often the chronologically earliest.
type Master struct {
Styles []string `json:"styles"`
Genres []string `json:"genres"`
Videos []Video `json:"videos"`
Title string `json:"title"`
MainRelease int `json:"main_release"`
MainReleaseURL string `json:"main_release_url"`
URI string `json:"uri"`
Artists []Artist `json:"artists"`
VersionURL string `json:"version_url"`
Year int `json:"year"`
Images []Image `json:"images"`
ResourceURL string `json:"resource_url"`
Tracklist []Track `json:"tracklist"`
ID int `json:"id"`
DataQuality string `json:"data_quality"`
}
// Master returns a master release
func (s *DatabaseService) Master(masterID int) (*Master, error) {
var master *Master
if err := request(s.url+mastersURI+strconv.Itoa(masterID), nil, &master); err != nil {
return nil, err
}
return master, nil
}
// MasterVersions retrieves a list of all releases that are versions of this master.
type MasterVersions struct {
Pagination Page `json:"pagination"`
Versions []Version `json:"versions"`
}
// MasterVersions retrieves a list of all Releases that are versions of this master
func (s *DatabaseService) MasterVersions(masterID int, pagination *Pagination) (*MasterVersions, error) {
var versions *MasterVersions
if err := request(s.url+mastersURI+strconv.Itoa(masterID)+"/versions", pagination.toParams(), &versions); err != nil {
return nil, err
}
return versions, nil
}

View File

@@ -19,7 +19,7 @@ func TestReleaseServiceRelease(t *testing.T) {
defer ts.Close()
d := initDiscogsClient(t, &Options{URL: ts.URL})
release, err := d.Release.Release(8138518)
release, err := d.Database.Release(8138518)
if err != nil {
t.Fatalf("failed to get release: %s", err)
}

View File

@@ -2,16 +2,16 @@ package discogs
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const (
discogsAPI = "https://api.discogs.com/"
discogsAPI = "https://api.discogs.com"
)
// Options is a set of options to use discogs API client
type Options struct {
URL string
Currency string
@@ -21,11 +21,8 @@ type Options struct {
// Client is a Discogs client for making Discogs API requests.
type Client struct {
Release *ReleaseService
Master *MasterService
Artist *ArtistService
Label *LabelService
Search *SearchService
Database *DatabaseService
Search *SearchService
}
var header *http.Header
@@ -35,7 +32,7 @@ func NewClient(o *Options) (*Client, error) {
header = &http.Header{}
if o == nil || o.UserAgent == "" {
return nil, fmt.Errorf("failed to set user-agent")
return nil, ErrUserAgentInvalid
}
header.Add("User-Agent", o.UserAgent)
@@ -55,11 +52,8 @@ func NewClient(o *Options) (*Client, error) {
}
return &Client{
Release: newReleaseService(o.URL+"releases/", cur),
Artist: newArtistService(o.URL + "artists/"),
Label: newLabelService(o.URL + "labels/"),
Master: newMasterService(o.URL + "masters/"),
Search: newSearchService(o.URL + "database/search"),
Database: newDatabaseService(o.URL, cur),
Search: newSearchService(o.URL + "/database/search"),
}, nil
}
@@ -73,12 +67,11 @@ func currency(c string) (string, error) {
case "":
return "USD", nil
default:
return "", fmt.Errorf("%v\n", "Invalid currency abbreviation.")
return "", ErrCurrencyNotSupported
}
}
func request(path string, params url.Values, resp interface{}) error {
fmt.Println(path + "?" + params.Encode())
r, err := http.NewRequest("GET", path+"?"+params.Encode(), nil)
if err != nil {
return err

View File

@@ -29,3 +29,64 @@ func initDiscogsClient(t *testing.T, options *Options) *Client {
return client
}
func TestNewClient(t *testing.T) {
tests := map[string]struct {
options *Options
err error
}{
"normal": {&Options{
UserAgent: testUserAgent,
Currency: "USD",
Token: "some token",
}, nil},
"incorrect user-agent": {&Options{
UserAgent: "",
Currency: "USD",
}, ErrUserAgentInvalid},
"incorrect currency": {&Options{
UserAgent: testUserAgent,
Currency: "RUR",
}, ErrCurrencyNotSupported},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if _, err := NewClient(tt.options); err != tt.err {
t.Errorf("err got=%s; want=%s", err, tt.err)
}
})
}
}
func TestCurrency(t *testing.T) {
tests := []struct {
currency string
want string
err error
}{
{currency: "", want: "USD", err: nil},
{currency: "USD", want: "USD", err: nil},
{currency: "GBP", want: "GBP", err: nil},
{currency: "EUR", want: "EUR", err: nil},
{currency: "CAD", want: "CAD", err: nil},
{currency: "AUD", want: "AUD", err: nil},
{currency: "JPY", want: "JPY", err: nil},
{currency: "CHF", want: "CHF", err: nil},
{currency: "MXN", want: "MXN", err: nil},
{currency: "BRL", want: "BRL", err: nil},
{currency: "NZD", want: "NZD", err: nil},
{currency: "SEK", want: "SEK", err: nil},
{currency: "ZAR", want: "ZAR", err: nil},
{currency: "RUR", want: "", err: ErrCurrencyNotSupported},
}
for i, tt := range tests {
cur, err := currency(tt.currency)
if err != tt.err {
t.Errorf("#%d err got=%s; want=%s", i, err, tt.err)
}
if cur != tt.want {
t.Errorf("#%d currency got=%s; want=%s", i, cur, tt.want)
}
}
}

View File

@@ -2,39 +2,20 @@ package discogs
import (
"fmt"
"strings"
)
// APIError represents a Discogs API Error response
type APIError struct {
Message string `json:"message"`
// Error represents a Discogs API error
type Error struct {
Message string
}
// Error ...
func (e APIError) Error() string {
if e.Message != "" {
return fmt.Sprintf("discogs: %v", e.Message)
}
return ""
func (e *Error) Error() string {
return fmt.Sprintf("discogs error: %s", strings.ToLower(e.Message))
}
// Empty returns true if empty. Otherwise, at least 1 error message/code is
// present and false is returned.
func (e APIError) Empty() bool {
if e.Message == "" {
return true
}
return false
}
// relevantError returns any non-nil http-related error (creating the request,
// getting the response, decoding) if any. If the decoded apiError is non-zero
// the apiError is returned. Otherwise, no errors occurred, returns nil.
func relevantError(httpError error, apiError APIError) error {
if httpError != nil {
return httpError
}
if apiError.Empty() {
return nil
}
return apiError
}
// APIErrors
var (
ErrCurrencyNotSupported = &Error{"currency does not supported"}
ErrUserAgentInvalid = &Error{"invalid user-agent"}
)

View File

@@ -9,7 +9,7 @@ import (
func main() {
d, err := discogs.NewClient(&discogs.Options{
UserAgent: "TestDiscogsClient/0.0.1 +http://example.com",
Currency: "EUR",
Currency: "AAA",
Token: "",
})
if err != nil {
@@ -17,9 +17,7 @@ func main() {
return
}
release, err := d.Search.Search(discogs.SearchRequest{
Q: "Ska-Jazz Review",
})
release, err := d.Database.Release(12345)
if err != nil {
fmt.Println(err)
return

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/irlndts/go-discogs
require github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 h1:zLTLjkaOFEFIOxY5BWLFLwh+cL8vOBW4XJ2aqLE/Tf0=
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=

View File

@@ -1,56 +0,0 @@
package discogs
import (
"strconv"
)
// LabelService ...
type LabelService struct {
url string
}
func newLabelService(url string) *LabelService {
return &LabelService{
url: url,
}
}
// Label resource represents a label, company, recording studio, location,
// or other entity involved with artists and releases.
type Label struct {
Profile string `json:"profile"`
ReleasesURL string `json:"releases_url"`
Name string `json:"name"`
ContactInfo string `json:"contact_info"`
URI string `json:"uri"`
Sublabels []Sublable `json:"sublabels"`
URLs []string `json:"urls"`
Images []Image `json:"images"`
ResourceURL string `json:"resource_url"`
ID int `json:"id"`
DataQuality string `json:"data_quality"`
}
// Label returns a label.
func (s *LabelService) Label(labelID int) (*Label, error) {
var label *Label
if err := request(s.url+strconv.Itoa(labelID), nil, &label); err != nil {
return nil, err
}
return label, nil
}
// LabelReleases is a list of Releases associated with the label.
type LabelReleases struct {
Pagination Page `json:"pagination"`
Releases []ReleaseSource `json:"releases"`
}
// Releases returns a list of Releases associated with the label.
func (s *LabelService) Releases(labelID int, pagination *Pagination) (*LabelReleases, error) {
var releases *LabelReleases
if err := request(s.url+strconv.Itoa(labelID)+"/releases", pagination.toParams(), &releases); err != nil {
return nil, err
}
return releases, nil
}

View File

@@ -1,59 +0,0 @@
package discogs
import (
"strconv"
)
type MasterService struct {
url string
}
func newMasterService(url string) *MasterService {
return &MasterService{
url: url,
}
}
// Master resource represents a set of similar releases.
// Masters (also known as `master releases`) have a `main release` which is often the chronologically earliest.
type Master struct {
Styles []string `json:"styles"`
Genres []string `json:"genres"`
Videos []Video `json:"videos"`
Title string `json:"title"`
Main_release int `json:"main_release"`
Main_release_url string `json:"main_release_url"`
Uri string `json:"uri"`
Artists []Artist `json:"artists"`
Version_url string `json:"version_url"`
Year int `json:"year"`
Images []Image `json:"images"`
Resource_url string `json:"resource_url"`
Tracklist []Track `json:"tracklist"`
Id int `json:"id"`
Data_quality string `json:"data_quality"`
}
// Master returns a master release
func (s *MasterService) Master(masterID int) (*Master, error) {
var master *Master
if err := request(s.url+strconv.Itoa(masterID), nil, &master); err != nil {
return nil, err
}
return master, nil
}
// MasterVersions retrieves a list of all releases that are versions of this master.
type MasterVersions struct {
Pagination Page `json:"pagination"`
Versions []Version `json:"versions"`
}
// Versions retrieves a list of all Releases that are versions of this master
func (s *MasterService) Versions(masterID int, pagination *Pagination) (*MasterVersions, error) {
var versions *MasterVersions
if err := request(s.url+strconv.Itoa(masterID)+"/versions", pagination.toParams(), &versions); err != nil {
return nil, err
}
return versions, nil
}

View File

@@ -19,7 +19,7 @@ type ArtistSource struct {
Anv string `json:"anv"`
ID int `json:"id"`
Join string `json:"join"`
Name string `json:"name:`
Name string `json:"name"`
ResourceURL string `json:"resource_url"`
Role string `json:"role"`
Tracks string `json:"tracks"`
@@ -42,6 +42,7 @@ type Track struct {
Title string `json:"title"`
Type string `json:"type_"`
Extraartists []ArtistSource `json:"extraartists"`
Artists []ArtistSource `json:"artists"`
}
// LabelSource ...
@@ -53,7 +54,7 @@ type LabelSource struct {
ResourceURL string `json:"resource_url"`
}
// Itentifier ...
// Identifier ...
type Identifier struct {
Type string `json:"type"`
Value string `json:"value"`

View File

@@ -1,90 +0,0 @@
package discogs
import (
"net/url"
"strconv"
)
// ReleaseService ...
type ReleaseService struct {
url string
currency string
}
func newReleaseService(url string, currency string) *ReleaseService {
return &ReleaseService{
url: url,
currency: currency,
}
}
// ReqRelease serves release request
type ReqRelease struct {
CurrAbbr string
}
// Release serves relesase response from discogs
type Release struct {
Title string `json:"title"`
ID int `json:"id"`
Artists []ArtistSource `json:"artists"`
DataQuality string `json:"data_quality"`
Thumb string `json:"thumb"`
Community Community `json:"community"`
Companies []Company `json:"companies"`
Country string `json:"country"`
DateAdded string `json:"date_added"`
DateChanged string `json:"date_changed"`
EstimatedWeight int `json:"estimated_weight"`
ExtraArtists []ArtistSource `json:"extraartists"`
FormatQuantity int `json:"format_quantity"`
Formats []Format `json:"formats"`
Genres []string `json:"genres"`
Identifiers []Identifier `json:"identifiers"`
Images []Image `json:"images"`
Labels []LabelSource `json:"labels"`
LowestPrice float64 `json:"lowest_price"`
MasterID int `json:"master_id"`
MasterURL string `json:"master_url"`
Notes string `json:"notes,omitempty"`
NumForSale int `json:"num_for_sale,omitempty"`
Released string `json:"released"`
ReleasedFormatted string `json:"released_formatted"`
ResourceURL string `json:"resource_url"`
// Series
Status string `json:"status"`
Styles []string `json:"styles"`
Tracklist []Track `json:"tracklist"`
URI string `json:"uri"`
Videos []Video `json:"videos"`
Year int `json:"year"`
}
// Release returns release by release's ID
func (s *ReleaseService) Release(releaseID int) (*Release, error) {
params := url.Values{}
params.Set("CurrAbbr", s.currency)
var release *Release
if err := request(s.url+strconv.Itoa(releaseID), params, &release); err != nil {
return nil, err
}
return release, nil
}
// ReleaseRating serves response for community release rating request
type ReleaseRating struct {
ID int `json:"release_id"`
Rating Rating `json:"rating"`
}
// Ratings retruns community release rating
func (s *ReleaseService) Rating(releaseID int) (*ReleaseRating, error) {
var rating *ReleaseRating
if err := request(s.url+strconv.Itoa(releaseID)+"/rating", nil, &rating); err != nil {
return nil, err
}
return rating, nil
}

View File

@@ -15,7 +15,7 @@ func newSearchService(url string) *SearchService {
}
}
// SerachRequest describes search request
// SearchRequest describes search request
type SearchRequest struct {
Q string `url:"q,omitempty"` // search query
Type string `url:"type,omitempty"` // one of release, master, artist, label
@@ -34,7 +34,7 @@ type SearchRequest struct {
Barcode string `url:"barcode,omitempty"` // search barcodes
Track string `url:"track,omitempty"` // search track titles
Submitter string `url:"submitter,omitempty"` // search submitter username
Contributer string `url:"contributer,omitempty"` // search contributor usernames
Contributor string `url:"contributor,omitempty"` // search contributor usernames
Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`

View File

@@ -1,328 +0,0 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package query
import (
"fmt"
"net/url"
"reflect"
"testing"
"time"
)
type Nested struct {
A SubNested `url:"a"`
B *SubNested `url:"b"`
Ptr *SubNested `url:"ptr,omitempty"`
}
type SubNested struct {
Value string `url:"value"`
}
func TestValues_types(t *testing.T) {
str := "string"
strPtr := &str
timeVal := time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)
tests := []struct {
in interface{}
want url.Values
}{
{
// basic primitives
struct {
A string
B int
C uint
D float32
E bool
}{},
url.Values{
"A": {""},
"B": {"0"},
"C": {"0"},
"D": {"0"},
"E": {"false"},
},
},
{
// pointers
struct {
A *string
B *int
C **string
D *time.Time
}{
A: strPtr,
C: &strPtr,
D: &timeVal,
},
url.Values{
"A": {str},
"B": {""},
"C": {str},
"D": {"2000-01-01T12:34:56Z"},
},
},
{
// slices and arrays
struct {
A []string
B []string `url:",comma"`
C []string `url:",space"`
D [2]string
E [2]string `url:",comma"`
F [2]string `url:",space"`
G []*string `url:",space"`
H []bool `url:",int,space"`
I []string `url:",brackets"`
J []string `url:",semicolon"`
K []string `url:",numbered"`
}{
A: []string{"a", "b"},
B: []string{"a", "b"},
C: []string{"a", "b"},
D: [2]string{"a", "b"},
E: [2]string{"a", "b"},
F: [2]string{"a", "b"},
G: []*string{&str, &str},
H: []bool{true, false},
I: []string{"a", "b"},
J: []string{"a", "b"},
K: []string{"a", "b"},
},
url.Values{
"A": {"a", "b"},
"B": {"a,b"},
"C": {"a b"},
"D": {"a", "b"},
"E": {"a,b"},
"F": {"a b"},
"G": {"string string"},
"H": {"1 0"},
"I[]": {"a", "b"},
"J": {"a;b"},
"K0": {"a"},
"K1": {"b"},
},
},
{
// other types
struct {
A time.Time
B time.Time `url:",unix"`
C bool `url:",int"`
D bool `url:",int"`
}{
A: time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC),
B: time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC),
C: true,
D: false,
},
url.Values{
"A": {"2000-01-01T12:34:56Z"},
"B": {"946730096"},
"C": {"1"},
"D": {"0"},
},
},
{
struct {
Nest Nested `url:"nest"`
}{
Nested{
A: SubNested{
Value: "that",
},
},
},
url.Values{
"nest[a][value]": {"that"},
"nest[b]": {""},
},
},
{
struct {
Nest Nested `url:"nest"`
}{
Nested{
Ptr: &SubNested{
Value: "that",
},
},
},
url.Values{
"nest[a][value]": {""},
"nest[b]": {""},
"nest[ptr][value]": {"that"},
},
},
{
nil,
url.Values{},
},
}
for i, tt := range tests {
v, err := Values(tt.in)
if err != nil {
t.Errorf("%d. Values(%q) returned error: %v", i, tt.in, err)
}
if !reflect.DeepEqual(tt.want, v) {
t.Errorf("%d. Values(%q) returned %v, want %v", i, tt.in, v, tt.want)
}
}
}
func TestValues_omitEmpty(t *testing.T) {
str := ""
s := struct {
a string
A string
B string `url:",omitempty"`
C string `url:"-"`
D string `url:"omitempty"` // actually named omitempty, not an option
E *string `url:",omitempty"`
}{E: &str}
v, err := Values(s)
if err != nil {
t.Errorf("Values(%q) returned error: %v", s, err)
}
want := url.Values{
"A": {""},
"omitempty": {""},
"E": {""}, // E is included because the pointer is not empty, even though the string being pointed to is
}
if !reflect.DeepEqual(want, v) {
t.Errorf("Values(%q) returned %v, want %v", s, v, want)
}
}
type A struct {
B
}
type B struct {
C string
}
type D struct {
B
C string
}
type e struct {
B
C string
}
type F struct {
e
}
func TestValues_embeddedStructs(t *testing.T) {
tests := []struct {
in interface{}
want url.Values
}{
{
A{B{C: "foo"}},
url.Values{"C": {"foo"}},
},
{
D{B: B{C: "bar"}, C: "foo"},
url.Values{"C": {"foo", "bar"}},
},
{
F{e{B: B{C: "bar"}, C: "foo"}}, // With unexported embed
url.Values{"C": {"foo", "bar"}},
},
}
for i, tt := range tests {
v, err := Values(tt.in)
if err != nil {
t.Errorf("%d. Values(%q) returned error: %v", i, tt.in, err)
}
if !reflect.DeepEqual(tt.want, v) {
t.Errorf("%d. Values(%q) returned %v, want %v", i, tt.in, v, tt.want)
}
}
}
func TestValues_invalidInput(t *testing.T) {
_, err := Values("")
if err == nil {
t.Errorf("expected Values() to return an error on invalid input")
}
}
type EncodedArgs []string
func (m EncodedArgs) EncodeValues(key string, v *url.Values) error {
for i, arg := range m {
v.Set(fmt.Sprintf("%s.%d", key, i), arg)
}
return nil
}
func TestValues_Marshaler(t *testing.T) {
s := struct {
Args EncodedArgs `url:"arg"`
}{[]string{"a", "b", "c"}}
v, err := Values(s)
if err != nil {
t.Errorf("Values(%q) returned error: %v", s, err)
}
want := url.Values{
"arg.0": {"a"},
"arg.1": {"b"},
"arg.2": {"c"},
}
if !reflect.DeepEqual(want, v) {
t.Errorf("Values(%q) returned %v, want %v", s, v, want)
}
}
func TestValues_MarshalerWithNilPointer(t *testing.T) {
s := struct {
Args *EncodedArgs `url:"arg"`
}{}
v, err := Values(s)
if err != nil {
t.Errorf("Values(%q) returned error: %v", s, err)
}
want := url.Values{}
if !reflect.DeepEqual(want, v) {
t.Errorf("Values(%q) returned %v, want %v", s, v, want)
}
}
func TestTagParsing(t *testing.T) {
name, opts := parseTag("field,foobar,foo")
if name != "field" {
t.Fatalf("name = %q, want field", name)
}
for _, tt := range []struct {
opt string
want bool
}{
{"foobar", true},
{"foo", true},
{"bar", false},
{"field", false},
} {
if opts.Contains(tt.opt) != tt.want {
t.Errorf("Contains(%q) = %v", tt.opt, !tt.want)
}
}
}

2
vendor/modules.txt vendored Normal file
View File

@@ -0,0 +1,2 @@
# github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135
github.com/google/go-querystring/query

13
vendor/vendor.json vendored
View File

@@ -1,13 +0,0 @@
{
"comment": "",
"ignore": "",
"package": [
{
"checksumSHA1": "aDjb3mG2PnOgRgQXVXBxzPNc9I0=",
"path": "github.com/google/go-querystring/query",
"revision": "53e6ce116135b80d037921a7fdd5138cf32d7a8a",
"revisionTime": "2017-01-11T10:11:55Z"
}
],
"rootPath": "github.com/irlndts/go-discogs"
}