anilist/page.go

46 lines
1.2 KiB
Go
Raw Normal View History

2022-02-03 17:23:29 +01:00
package anilist
import (
"reflect"
)
type page[T any] struct {
PageInfo *pageInfo `json:"pageInfo"`
Media []Media `json:"media"`
MediaList []MediaList `json:"mediaList"`
Users []User `json:"users"`
AiringSchedules []AiringSchedule `json:"airingSchedules"`
}
func (p *page[T]) Data() []T {
if reflect.TypeOf(new(T)).Elem() == reflect.TypeOf(new(User)).Elem() {
var data interface{} = p.Users
return data.([]T)
}
if reflect.TypeOf(new(T)).Elem() == reflect.TypeOf(new(Media)).Elem() {
var data interface{} = p.Media
return data.([]T)
}
if reflect.TypeOf(new(T)).Elem() == reflect.TypeOf(new(MediaList)).Elem() {
var data interface{} = p.MediaList
return data.([]T)
}
if reflect.TypeOf(new(T)).Elem() == reflect.TypeOf(new(AiringSchedule)).Elem() {
var data interface{} = p.AiringSchedules
return data.([]T)
}
panic("generic type not implemented: " + reflect.TypeOf(new(T)).Elem().Name())
}
type pageInfo struct {
Total int `json:"total"`
CurrentPage int `json:"currentPage"`
LastPage int `json:"lastPage"`
HasNextPage bool `json:"hasNextPage"`
PerPage int `json:"perPage"`
}