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 ab40d570eb
Signed by: yonah
SSH Key Fingerprint: SHA256:vnQvK8+XXH9Tbni2AV1a/8qdVK/zPcXw52GM0ruQvwA
5 changed files with 11 additions and 12 deletions

View File

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

View File

@ -11,7 +11,7 @@ import (
var albumsJSON []byte var albumsJSON []byte
func TestAlbums(t *testing.T) { func TestAlbums(t *testing.T) {
checkJSONRoundTrip(t, Albums{Code: 0, Msg: "", Data: []Album{ checkJSONRoundTrip(t, Albums{Data: []Album{
{CID: 2444, Name: "无忧梦呓OST", CoverURL: "https://web.hycdn.cn/siren/pic/20250905/2039eb5cf7f7d3a951d5f653c6d33f64.jpg", Artists: []string{"塞壬唱片-MSR"}}, {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: 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"}}, {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" "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. // StringInt is a JSON string representing an integer.
type StringInt int type StringInt int

View File

@ -1,11 +1,7 @@
package monstersirenfetch package monstersirenfetch
// Songs is the response of /api/songs. // Songs is the response of /api/songs.
type Songs struct { type Songs Response[SongData]
Code int `json:"code"`
Msg string `json:"msg"`
Data SongData `json:"data"`
}
// SongData represents [Songs.Data]. // SongData represents [Songs.Data].
type SongData = struct { type SongData = struct {

View File

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