Right now in AnimeService this relies heavily on nested anonymous structs defined within individual method bodies. While this keeps the package namespace small, it introduces significant maintenance overhead, prevents type reuse by library consumers, and leads to logic duplication across different service methods.
Here it contains redundant struct definitions that should be extracted into named types:
Episodes vs EpisodeByID: Both define nearly identical "Episode" models.
Videos: Redefines Trailer and Image structures already present in the base Anime struct.
Characters & Staff: Internal structures for "Voice Actor" and "Person" should be unified.
1. Extract Domain Models
Move nested anonymous structs to top level named types. This so that users of the library can pass these objects into their own functions.
- Extract
Trailer and VideoImages into standalone types.
- Extract a unified
Episode struct to be shared by Episodes() and EpisodeByID().
- Extract
CharacterRole and StaffPosition types.
2. Implement a Generic Response Wrapper
To eliminate the repetitive var r struct { Data T } pattern, implement a private helper or use a generic internal type to handle the Jikan API "data" envelope.
Example Pattern:
type response[T any] struct {
Data T `json:"data"`
Pagination *Pagination `json:"pagination,omitempty"`
}
3. Standardize URL Encoding
Refactor methods currently using fmt.Sprintf for query parameters to consistently use url.Values to ensure proper character escaping (following the pattern already used in the News method).
Right now in
AnimeServicethis relies heavily on nested anonymous structs defined within individual method bodies. While this keeps the package namespace small, it introduces significant maintenance overhead, prevents type reuse by library consumers, and leads to logic duplication across different service methods.Here it contains redundant struct definitions that should be extracted into named types:
EpisodesvsEpisodeByID: Both define nearly identical "Episode" models.Videos: RedefinesTrailerandImagestructures already present in the baseAnimestruct.Characters&Staff: Internal structures for "Voice Actor" and "Person" should be unified.1. Extract Domain Models
Move nested anonymous structs to top level named types. This so that users of the library can pass these objects into their own functions.
TrailerandVideoImagesinto standalone types.Episodestruct to be shared byEpisodes()andEpisodeByID().CharacterRoleandStaffPositiontypes.2. Implement a Generic Response Wrapper
To eliminate the repetitive
var r struct { Data T }pattern, implement a private helper or use a generic internal type to handle the Jikan API "data" envelope.Example Pattern:
3. Standardize URL Encoding
Refactor methods currently using
fmt.Sprintffor query parameters to consistently useurl.Valuesto ensure proper character escaping (following the pattern already used in theNewsmethod).