-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
97 lines (80 loc) · 1.7 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package spotify
// Structures used to parse JSON returned by Spotify API
import (
"strings"
)
type SpotifyArtist struct {
Id string
Name string
}
type SpotifyAlbum struct {
Id string
Name string
Artists []SpotifyArtist
}
type SpotifyTrack struct {
Id string
Name string
DurationMs int64 `json:"duration_ms"`
Popularity int
Artists []SpotifyArtist
Album SpotifyAlbum
AvailableMarkets []string `json:"available_markets"`
}
type PlaylistItem struct {
Track SpotifyTrack
}
type PlaylistResponse struct {
Items []PlaylistItem
Total int
Next string
}
type SearchResponseBody struct {
Items []SpotifyTrack
}
type SearchResponse struct {
Tracks SearchResponseBody
}
type ImmutableSpotifyTrack struct {
artist string
title string
id string
}
func (t SpotifyTrack) String() string {
artist := t.ArtistAsString()
if len(artist)+len(t.Name) > 0 {
return t.ArtistAsString() + " - " + t.Name
} else {
return ""
}
}
func (t SpotifyTrack) ArtistAsString() string {
var artists = []string{}
for _, artist := range t.Artists {
artists = append(artists, artist.Name)
}
return strings.Join(artists, ", ")
}
func (t *SpotifyTrack) immutable() *ImmutableSpotifyTrack {
return &ImmutableSpotifyTrack{
artist: t.ArtistAsString(),
title: t.Name,
id: t.Id,
}
}
func (t *ImmutableSpotifyTrack) Id() string {
return t.id
}
func (t *ImmutableSpotifyTrack) Title() string {
return t.title
}
func (t *ImmutableSpotifyTrack) Artist() string {
return t.artist
}
func (t *ImmutableSpotifyTrack) String() string {
if t == nil || len(t.Artist())+len(t.Title()) == 0 {
return ""
} else {
return t.Artist() + " - " + t.Title()
}
}