generic: struct for response common fields

This is common across all responses.

Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
Yonah 2025-09-17 06:52:38 +09:00
parent 033357459f
commit 7dd470f9ae
Signed by: yonah
SSH Key Fingerprint: SHA256:vnQvK8+XXH9Tbni2AV1a/8qdVK/zPcXw52GM0ruQvwA
5 changed files with 18 additions and 16 deletions

View File

@ -1,11 +1,10 @@
package monstersirenfetch
// Albums is the response of /api/albums.
type Albums struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data []Album `json:"data"`
}
// AlbumsResponse is the response of /api/albums.
type AlbumsResponse Response[AlbumsData]
// AlbumsData is the type of [AlbumsResponse] data.
type AlbumsData []Album
// Album represents the metadata of an album.
type Album struct {

View File

@ -11,7 +11,7 @@ import (
var albumsJSON []byte
func TestAlbums(t *testing.T) {
checkJSONRoundTrip(t, Albums{Code: 0, Msg: "", Data: []Album{
checkJSONRoundTrip(t, AlbumsResponse{Data: AlbumsData{
{CID: 2444, Name: "无忧梦呓OST", CoverURL: "https://web.hycdn.cn/siren/pic/20250905/2039eb5cf7f7d3a951d5f653c6d33f64.jpg", Artists: []string{"塞壬唱片-MSR"}},
{CID: 5192, Name: "焰烬曙明OST", CoverURL: "https://web.hycdn.cn/siren/pic/20250905/6e32685dcc113791d9e90c14e18634c6.jpg", Artists: []string{"塞壬唱片-MSR"}},
{CID: 1019, Name: "in your blue eyes", CoverURL: "https://web.hycdn.cn/siren/pic/20250903/63d83dcec8698fde96edcd039d9f4fcf.jpg", Artists: []string{"Ave Mujica"}},

View File

@ -6,6 +6,13 @@ import (
"strings"
)
// Response is a generic API response.
type Response[T any] struct {
Code int `json:"code"`
Message string `json:"msg"`
Data T `json:"data"`
}
// StringInt is a JSON string representing an integer.
type StringInt int

View File

@ -1,14 +1,10 @@
package monstersirenfetch
// Songs is the response of /api/songs.
type Songs struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data SongData `json:"data"`
}
// SongsResponse is the response of /api/songs.
type SongsResponse Response[SongsData]
// SongData represents [Songs.Data].
type SongData = struct {
// SongsData is the type of [SongsResponse] data.
type SongsData = struct {
List []Song `json:"list"`
Autoplay string `json:"autoplay"`
}

View File

@ -11,7 +11,7 @@ import (
var songsJSON []byte
func TestSongs(t *testing.T) {
checkJSONRoundTrip(t, Songs{Code: 0, Msg: "", Data: SongData{List: []Song{
checkJSONRoundTrip(t, SongsResponse{Data: SongsData{List: []Song{
{CID: 125042, Name: "Sanctuary Inside", AlbumCID: 5194, Artists: []string{"塞壬唱片-MSR"}},
{CID: 880304, Name: "美梦必须成真", AlbumCID: 2444, Artists: []string{"塞壬唱片-MSR"}},
{CID: 779469, Name: "玩乐时间", AlbumCID: 2444, Artists: []string{"塞壬唱片-MSR"}},