Use time.Duration for track lengths

This commit is contained in:
Tyler
2020-04-12 12:03:08 -04:00
parent ffe0ef235d
commit fbab76d269
4 changed files with 80 additions and 14 deletions

View File

@ -1,9 +1,11 @@
package gavalink
import (
"encoding/json"
"io"
"net/http"
"os"
"time"
)
const (
@ -27,7 +29,7 @@ type Tracks struct {
// NoMatches, or LoadFailed
Type string `json:"loadType"`
PlaylistInfo *PlaylistInfo `json:"playlistInfo"`
Tracks []Track `json:"tracks"`
Tracks []*Track `json:"tracks"`
}
// PlaylistInfo contains information about a loaded playlist
@ -42,20 +44,46 @@ type PlaylistInfo struct {
// Track contains information about a loaded track
type Track struct {
// Data contains the base64 encoded Lavaplayer track
Data string `json:"track"`
Info TrackInfo `json:"info"`
Data string `json:"track"`
Info *TrackInfo `json:"info"`
}
// TrackInfo contains more data about a loaded track
type TrackInfo struct {
Identifier string `json:"identifier"`
Title string `json:"title"`
Author string `json:"author"`
URI string `json:"uri"`
Seekable bool `json:"isSeekable"`
Stream bool `json:"isStream"`
Length int `json:"length"`
Position int `json:"position"`
Identifier string `json:"identifier"`
Title string `json:"title"`
Author string `json:"author"`
URI string `json:"uri"`
Seekable bool `json:"isSeekable"`
Stream bool `json:"isStream"`
Length time.Duration `json:"length"`
Position int `json:"position"`
}
func (t *TrackInfo) MarshalJSON() ([]byte, error) {
type Alias TrackInfo
return json.Marshal(&struct {
Length int64 `json:"length"`
*Alias
}{
Length: int64(t.Length / time.Millisecond),
Alias: (*Alias)(t),
})
}
func (t *TrackInfo) UnmarshalJSON(data []byte) error {
type Alias TrackInfo
aux := &struct {
Length int64 `json:"length"`
*Alias
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
t.Length = time.Duration(aux.Length) * time.Millisecond
return nil
}
const (