Skip to content

[Refactor] Remove duplicate anonymous structs and better API response types #1

Description

@Sethispr

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).

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions