song: struct for /api/song/%d

This also includes tests against a sample response from the
https://monster-siren.hypergryph.com/api/song/048794 endpoint.

Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
Yonah 2025-09-17 07:49:06 +09:00
parent c2c4171a3a
commit ea54772c03
Signed by: yonah
SSH Key Fingerprint: SHA256:vnQvK8+XXH9Tbni2AV1a/8qdVK/zPcXw52GM0ruQvwA
4 changed files with 79 additions and 8 deletions

55
song.go Normal file
View File

@ -0,0 +1,55 @@
package monstersirenfetch
import (
"bytes"
"encoding/json"
)
// SongResponse is the response of /api/song/%d.
type SongResponse Response[Song]
// Song holds the metadata of a song.
type Song struct {
CID StringInt `json:"cid"`
Name string `json:"name"`
AlbumCID StringInt `json:"albumCid"`
SourceURL string `json:"sourceUrl,omitempty"`
LyricURL string `json:"lyricUrl,omitempty"`
MvURL string `json:"mvUrl,omitempty"`
MvCoverURL string `json:"mvCoverUrl,omitempty"`
Artists []string `json:"artists"`
}
// IsFull returns whether the metadata held by [Song] is considered full (originating from a [SongResponse]).
func (s *Song) IsFull() bool { return s.SourceURL != "" }
// songDirect is [Song] without its MarshalJSON method.
type songDirect Song
// songNullable is [Song] with corresponding nullable string fields.
type songNullable struct {
CID StringInt `json:"cid"`
Name string `json:"name"`
AlbumCID StringInt `json:"albumCid"`
SourceURL string `json:"sourceUrl"`
LyricURL NullableString `json:"lyricUrl"`
MvURL NullableString `json:"mvUrl"`
MvCoverURL NullableString `json:"mvCoverUrl"`
Artists []string `json:"artists"`
}
func (s *Song) MarshalJSON() (data []byte, err error) {
buf := new(bytes.Buffer)
e := json.NewEncoder(buf)
e.SetEscapeHTML(false)
if !s.IsFull() {
err = e.Encode((*songDirect)(s))
data = buf.Bytes()
return
}
return json.Marshal(&songNullable{s.CID, s.Name, s.AlbumCID, s.SourceURL,
NullableString(s.LyricURL), NullableString(s.MvURL), NullableString(s.MvCoverURL),
s.Artists})
}

23
song_test.go Normal file
View File

@ -0,0 +1,23 @@
package monstersirenfetch_test
import (
_ "embed"
"testing"
. "git.gensokyo.uk/yonah/monstersirenfetch"
)
//go:embed testdata/song.json
var songJSON []byte
func TestSong(t *testing.T) {
checkJSONRoundTrip(t, SongResponse{Data: Song{
CID: 48794,
Name: "Warm and Small Light",
AlbumCID: 6660,
SourceURL: "https://res01.hycdn.cn/04ce5de54bb52eb85008644d541d40fa/68CA0442/siren/audio/20240709/a7f650238eaefc9c30a9627d7f78d819.wav",
LyricURL: "https://web.hycdn.cn/siren/lyric/20240709/4a10c70629b68a187fdbef4a27bd32d8.lrc",
MvURL: "", MvCoverURL: "",
Artists: []string{"塞壬唱片-MSR"},
}}, songJSON)
}

View File

@ -8,11 +8,3 @@ type SongsData = struct {
List []Song `json:"list"`
Autoplay string `json:"autoplay"`
}
// Song represents the metadata of a song.
type Song struct {
CID StringInt `json:"cid"`
Name string `json:"name"`
AlbumCID StringInt `json:"albumCid"`
Artists []string `json:"artists"`
}

1
testdata/song.json vendored Normal file
View File

@ -0,0 +1 @@
{"code":0,"msg":"","data":{"cid":"048794","name":"Warm and Small Light","albumCid":"6660","sourceUrl":"https://res01.hycdn.cn/04ce5de54bb52eb85008644d541d40fa/68CA0442/siren/audio/20240709/a7f650238eaefc9c30a9627d7f78d819.wav","lyricUrl":"https://web.hycdn.cn/siren/lyric/20240709/4a10c70629b68a187fdbef4a27bd32d8.lrc","mvUrl":null,"mvCoverUrl":null,"artists":["塞壬唱片-MSR"]}}