From 9aeafb3e899ca96a7514d9756728afebcb907d55 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 23 Feb 2022 21:37:34 +0530 Subject: made accessing anime ranking more convinient by directly adding RankNum to the rAnime datatype --- anime/anime.go | 20 ++++++++++---------- anime/ranking.structs.go | 16 ++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/anime/anime.go b/anime/anime.go index 9047033..292abe2 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -134,20 +134,20 @@ func (c Client) GetAnimeRanking(rankingType string, limit, offset int, fields [] json.Unmarshal([]byte(data), &rankingData) // Adding all the animes in ranking list to a slice - var animeRankingTitles []AnimeRankingTitle - for _, element := range rankingData.Data { - animeRankingTitles = append( - animeRankingTitles, - AnimeRankingTitle { - Anime: element.Anime, - RankNum: element.Ranking.Rank, - }, - ) + var animes []rAnime + + for _, anime := range rankingData.Data { + // set RankNum for anime + newManga := anime.Anime + newManga.RankNum = anime.Ranking.Rank + + // add newManga to list + animes = append(animes, newManga) } // Finally, create the AnimeRanking object animeRanking = AnimeRanking { - Titles: animeRankingTitles, + Animes: animes, Paging: ListPaging { NextPage: rankingData.Paging.NextPage, PrevPage: rankingData.Paging.PrevPage, diff --git a/anime/ranking.structs.go b/anime/ranking.structs.go index 03a4589..b0d357f 100644 --- a/anime/ranking.structs.go +++ b/anime/ranking.structs.go @@ -16,10 +16,16 @@ package anime +// Anime but with an extra RankNum field +type rAnime struct { + Anime + RankNum int +} + // this is how the API returns data (looks horrible) type RawRanking struct { Data []struct { - Anime Anime `json:"node"` + Anime rAnime `json:"node"` Ranking struct { Rank int `json:"rank"` } `json:"ranking"` @@ -28,14 +34,8 @@ type RawRanking struct { Paging ListPaging `json:"paging"` } -// each anime has a ranking number -type AnimeRankingTitle struct { - Anime Anime - RankNum int -} - // this is how mal2go returns data type AnimeRanking struct { - Titles []AnimeRankingTitle + Animes []rAnime Paging ListPaging } -- cgit v1.2.3