fix(ncm): Handle mixed-type artist field in metadata parsing

The current implementation for parsing artist metadata in `ncmMetaMusic.GetArtists` does not handle cases where the "artist" JSON field is a list containing mixed-type inner lists.

For certain NCM files (often those with FLAC format), the artist data is structured as `[["Artist Name", 12345]]`. The existing type assertion for `[][]string` fails for this structure because the inner slice is of type `[]interface{}`, not `[]string`. This results in the artist metadata being dropped during the conversion process.

This commit enhances the `GetArtists` method by adding a new case to the `type switch` to specifically handle the `[]interface{}` type. The new logic iterates through the nested structure and correctly extracts the artist name, which is assumed to be the first string element in each inner slice.

This fix improves compatibility with a wider range of NCM files and ensures artist metadata is reliably parsed, preventing data loss for affected files.
This commit is contained in:
solitudechn 2025-08-09 23:58:03 +00:00
parent 0a94383ba3
commit 20b746bee5

View File

@ -43,23 +43,42 @@ func (m *ncmMetaMusic) GetAlbumImageURL() string {
return m.AlbumPic return m.AlbumPic
} }
func (m *ncmMetaMusic) GetArtists() []string {
m.logger.Debug("ncm artists", zap.Any("artists", m.Artist))
var artists []string = nil func (m *ncmMetaMusic) GetArtists() []string {
if jsonArtists, ok := m.Artist.([][]string); ok { m.logger.Debug("ncm artists raw", zap.Any("artists", m.Artist))
for _, artist := range jsonArtists { var artists []string
for _, name := range artist { switch v := m.Artist.(type) {
artists = append(artists, name)
// Case 1: Handles the format [['artistA'], ['artistB']]
case [][]string:
for _, artistSlice := range v {
artists = append(artists, artistSlice...)
}
// Case 2: Handles the simple format "artistA"
// Ref: https://git.unlock-music.dev/um/cli/issues/78
case string:
artists = []string{v}
// Case 3: Handles the mixed-type format [['artistA', 12345], ['artistB', 67890]]
// This is the key fix for correctly parsing artist info from certain files.
case []interface{}:
for _, item := range v {
if innerSlice, ok := item.([]interface{}); ok {
if len(innerSlice) > 0 {
// Assume the first element is the artist's name.
if artistName, ok := innerSlice[0].(string); ok {
artists = append(artists, artistName)
}
}
} }
} }
} else if artist, ok := m.Artist.(string); ok {
// #78: artist is a string type. default:
// https://git.unlock-music.dev/um/cli/issues/78 // Log a warning if the artist type is unexpected and not handled.
artists = []string{artist}
} else {
m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist)) m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist))
} }
return artists return artists
} }