From 7dd470f9ae09ddfd6716c9400c0ab348826a2ded Mon Sep 17 00:00:00 2001 From: Yonah Date: Wed, 17 Sep 2025 06:52:38 +0900 Subject: [PATCH] generic: struct for response common fields This is common across all responses. Signed-off-by: Yonah --- albums.go | 11 +++++------ albums_test.go | 2 +- generic.go | 7 +++++++ songs.go | 12 ++++-------- songs_test.go | 2 +- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/albums.go b/albums.go index b585909..b7f667c 100644 --- a/albums.go +++ b/albums.go @@ -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 { diff --git a/albums_test.go b/albums_test.go index 486896e..e605c00 100644 --- a/albums_test.go +++ b/albums_test.go @@ -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"}}, diff --git a/generic.go b/generic.go index 6a925df..47a71d9 100644 --- a/generic.go +++ b/generic.go @@ -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 diff --git a/songs.go b/songs.go index fc79ac0..b4f540a 100644 --- a/songs.go +++ b/songs.go @@ -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"` } diff --git a/songs_test.go b/songs_test.go index 2509676..6bcc408 100644 --- a/songs_test.go +++ b/songs_test.go @@ -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"}},