Use time.Duration for track lengths
This commit is contained in:
parent
ffe0ef235d
commit
fbab76d269
|
@ -23,3 +23,7 @@
|
|||
# End of https://www.gitignore.io/api/go
|
||||
|
||||
.env
|
||||
|
||||
# IntelliJ
|
||||
.idea/
|
||||
*.iml
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"encoding/binary"
|
||||
"github.com/valyala/fastjson"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
const trackInfoVersioned int32 = 1
|
||||
|
@ -109,7 +110,7 @@ func Decode(r io.Reader) (*TrackInfo, error) {
|
|||
Author: author,
|
||||
URI: url,
|
||||
Stream: stream == 1,
|
||||
Length: int(length),
|
||||
Length: time.Duration(length) * time.Millisecond,
|
||||
}
|
||||
|
||||
return track, nil
|
||||
|
@ -128,7 +129,7 @@ func readString(r io.Reader) (string, error) {
|
|||
return string(buf), nil
|
||||
}
|
||||
|
||||
func jsonStringValue(v *fastjson.Value, keys ... string) string {
|
||||
func jsonStringValue(v *fastjson.Value, keys ...string) string {
|
||||
value := v.Get(keys...)
|
||||
|
||||
if value == nil {
|
||||
|
|
34
model.go
34
model.go
|
@ -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
|
||||
|
@ -43,7 +45,7 @@ type PlaylistInfo struct {
|
|||
type Track struct {
|
||||
// Data contains the base64 encoded Lavaplayer track
|
||||
Data string `json:"track"`
|
||||
Info TrackInfo `json:"info"`
|
||||
Info *TrackInfo `json:"info"`
|
||||
}
|
||||
|
||||
// TrackInfo contains more data about a loaded track
|
||||
|
@ -54,10 +56,36 @@ type TrackInfo struct {
|
|||
URI string `json:"uri"`
|
||||
Seekable bool `json:"isSeekable"`
|
||||
Stream bool `json:"isStream"`
|
||||
Length int `json:"length"`
|
||||
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 (
|
||||
opVoiceUpdate = "voiceUpdate"
|
||||
opVoiceProcessed = "voiceProcessed"
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package gavalink
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTrackInfo_JSON(t *testing.T) {
|
||||
i := &TrackInfo{
|
||||
Length: 10 * time.Second,
|
||||
}
|
||||
|
||||
b, err := json.Marshal(i)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Log(string(b))
|
||||
|
||||
deserialize := &TrackInfo{}
|
||||
|
||||
if err = json.Unmarshal(b, &deserialize); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Log("Deserialized length:", deserialize.Length)
|
||||
|
||||
if deserialize.Length != time.Second*10 {
|
||||
t.Fatal("Expected deserialized time to be 10 seconds!")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue