return pointer to Cursor instead of Cursor itself

This commit is contained in:
Timon Ringwald 2022-02-24 21:15:14 +01:00
parent 79d24f2ba0
commit e5e717f3e1
6 changed files with 10 additions and 10 deletions

View File

@ -2,7 +2,7 @@ package anilist
import "context"
func (api *Api) GetAiringSchedule(ctx context.Context, vars AiringScheduleQuery, onError func(error)) Cursor[AiringSchedule] {
func (api *Api) GetAiringSchedule(ctx context.Context, vars AiringScheduleQuery, onError func(error)) *Cursor[AiringSchedule] {
resp := responseObj[*page[AiringSchedule]]{}
return requestPaged(api, ctx, getAiringScheduleQuery, vars.toMap(), &resp, onError)
}

4
api.go
View File

@ -61,7 +61,7 @@ func request[T any](api *Api, query string, vars map[string]interface{}, respObj
return nil
}
func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[string]interface{}, respObj *responseObj[*page[R]], onError func(error)) Cursor[R] {
func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[string]interface{}, respObj *responseObj[*page[R]], onError func(error)) *Cursor[R] {
resp := responseObj[struct {
Page *page[R] `json:"Page"`
}]{}
@ -105,7 +105,7 @@ func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[s
}
}()
return Cursor[R]{
return &Cursor[R]{
Chan: out,
ctx: ctx,
cancelFunc: cancelFunc,

View File

@ -8,16 +8,16 @@ type Cursor[T any] struct {
Chan <-chan *T
}
func (c Cursor[T]) First() *T {
func (c *Cursor[T]) First() *T {
defer c.cancelFunc()
return <-c.Chan
}
func (c Cursor[T]) Close() {
func (c *Cursor[T]) Close() {
c.cancelFunc()
}
func (c Cursor[T]) Next() (*T, bool) {
func (c *Cursor[T]) Next() (*T, bool) {
if c.ctx.Err() == nil {
value, ok := <-c.Chan
return value, ok
@ -26,7 +26,7 @@ func (c Cursor[T]) Next() (*T, bool) {
return nil, false
}
func (c Cursor[T]) Slice() []T {
func (c *Cursor[T]) Slice() []T {
s := make([]T, 0)
for value, ok := c.Next(); ok; value, ok = c.Next() {
s = append(s, *value)

View File

@ -2,7 +2,7 @@ package anilist
import "context"
func (api *Api) GetMedia(ctx context.Context, vars MediaQuery, onError func(error)) Cursor[Media] {
func (api *Api) GetMedia(ctx context.Context, vars MediaQuery, onError func(error)) *Cursor[Media] {
resp := responseObj[*page[Media]]{}
return requestPaged(api, ctx, getMediaQuery, vars.toMap(), &resp, onError)
}

View File

@ -2,7 +2,7 @@ package anilist
import "context"
func (api *Api) GetMediaList(ctx context.Context, vars MediaListQuery, onError func(error)) Cursor[MediaList] {
func (api *Api) GetMediaList(ctx context.Context, vars MediaListQuery, onError func(error)) *Cursor[MediaList] {
resp := responseObj[*page[MediaList]]{}
return requestPaged(api, ctx, getMediaListQuery, vars.toMap(), &resp, onError)
}

View File

@ -44,7 +44,7 @@ func (api *Api) GetUserByID(id int) (*User, error) {
return resp.Data.User, nil
}
func (api *Api) SearchUsers(ctx context.Context, search string, onError func(error)) Cursor[User] {
func (api *Api) SearchUsers(ctx context.Context, search string, onError func(error)) *Cursor[User] {
vars := map[string]interface{}{"search": search}
resp := responseObj[*page[User]]{}
return requestPaged(api, ctx, searchUsersQuery, vars, &resp, onError)