Unexport some structs, rename root struct to thing

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-08-11 21:06:34 -04:00
parent 62054d2973
commit aa1de29812
4 changed files with 56 additions and 104 deletions

View file

@ -95,15 +95,13 @@ func (s *CommentService) LoadMoreReplies(ctx context.Context, comment *Comment)
return nil, err return nil, err
} }
type rootResponse struct { root := new(struct {
JSON struct { JSON struct {
Data struct { Data struct {
Things Things `json:"things"` Things things `json:"things"`
} `json:"data"` } `json:"data"`
} `json:"json"` } `json:"json"`
} })
root := new(rootResponse)
resp, err := s.client.Do(ctx, req, root) resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return resp, err return resp, err

View file

@ -83,7 +83,7 @@ func (s *ModerationService) GetActionsByType(ctx context.Context, subreddit stri
return nil, resp, err return nil, resp, err
} }
return root.getModeratorActions(), resp, nil return root.getModActions(), resp, nil
} }
// AcceptInvite accepts a pending invite to moderate the specified subreddit. // AcceptInvite accepts a pending invite to moderate the specified subreddit.

View file

@ -463,15 +463,13 @@ func (s *PostService) LoadMoreComments(ctx context.Context, pc *PostAndComments)
return nil, err return nil, err
} }
type rootResponse struct { root := new(struct {
JSON struct { JSON struct {
Data struct { Data struct {
Things Things `json:"things"` Things things `json:"things"`
} `json:"data"` } `json:"data"`
} `json:"json"` } `json:"json"`
} })
root := new(rootResponse)
resp, err := s.client.Do(ctx, req, root) resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return resp, err return resp, err

120
things.go
View file

@ -2,7 +2,6 @@ package reddit
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"strings" "strings"
) )
@ -10,7 +9,7 @@ import (
const ( const (
kindComment = "t1" kindComment = "t1"
kindAccount = "t2" kindAccount = "t2"
kindLink = "t3" // a link is a post kindPost = "t3"
kindMessage = "t4" kindMessage = "t4"
kindSubreddit = "t5" kindSubreddit = "t5"
kindAward = "t6" kindAward = "t6"
@ -37,30 +36,30 @@ func (p *Permalink) UnmarshalJSON(data []byte) error {
return nil return nil
} }
// todo: rename this to thing // thing is an entity on Reddit.
type root struct { // Its kind reprsents what it is and what is stored in the Data field
Kind string `json:"kind,omitempty"` // e.g. t1 = comment, t2 = user, t3 = post, etc.
Data interface{} `json:"data,omitempty"` type thing struct {
Kind string `json:"kind"`
Data json.RawMessage `json:"data"`
} }
type rootListing struct { type rootListing struct {
Kind string `json:"kind,omitempty"` Kind string `json:"kind"`
Data *Listing `json:"data"` Data listing `json:"data"`
} }
// Listing holds things coming from the Reddit API // listing holds things coming from the Reddit API
// It also contains the after/before anchors useful for subsequent requests // It also contains the after/before anchors useful for subsequent requests
type Listing struct { type listing struct {
Things Things `json:"children"` Things things `json:"children"`
After string `json:"after"` After string `json:"after"`
Before string `json:"before"` Before string `json:"before"`
} }
// Things are objects/entities coming from the Reddit API. type things struct {
type Things struct {
Comments []*Comment Comments []*Comment
MoreComments *More MoreComments *More
Users []*User Users []*User
Posts []*Post Posts []*Post
Subreddits []*Subreddit Subreddits []*Subreddit
@ -68,33 +67,19 @@ type Things struct {
// todo: add the other kinds of things // todo: add the other kinds of things
} }
func (t *Things) init() { // init initializes or clears the listing.
if t.Comments == nil { func (t *things) init() {
t.Comments = make([]*Comment, 0) t.Comments = make([]*Comment, 0)
}
if t.Users == nil {
t.Users = make([]*User, 0) t.Users = make([]*User, 0)
}
if t.Posts == nil {
t.Posts = make([]*Post, 0) t.Posts = make([]*Post, 0)
}
if t.Subreddits == nil {
t.Subreddits = make([]*Subreddit, 0) t.Subreddits = make([]*Subreddit, 0)
}
if t.ModActions == nil {
t.ModActions = make([]*ModAction, 0) t.ModActions = make([]*ModAction, 0)
} }
}
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
func (t *Things) UnmarshalJSON(b []byte) error { func (t *things) UnmarshalJSON(b []byte) error {
t.init() t.init()
type thing struct {
Kind string `json:"kind"`
Data json.RawMessage `json:"data"`
}
var things []thing var things []thing
if err := json.Unmarshal(b, &things); err != nil { if err := json.Unmarshal(b, &things); err != nil {
return err return err
@ -117,7 +102,7 @@ func (t *Things) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(thing.Data, v); err == nil { if err := json.Unmarshal(thing.Data, v); err == nil {
t.Users = append(t.Users, v) t.Users = append(t.Users, v)
} }
case kindLink: case kindPost:
v := new(Post) v := new(Post)
if err := json.Unmarshal(thing.Data, v); err == nil { if err := json.Unmarshal(thing.Data, v); err == nil {
t.Posts = append(t.Posts, v) t.Posts = append(t.Posts, v)
@ -216,10 +201,8 @@ func (r *Replies) UnmarshalJSON(data []byte) error {
return err return err
} }
if root.Data != nil {
r.Comments = root.Data.Things.Comments r.Comments = root.Data.Things.Comments
r.MoreComments = root.Data.Things.MoreComments r.MoreComments = root.Data.Things.MoreComments
}
return nil return nil
} }
@ -307,75 +290,48 @@ type Subreddit struct {
Favorite bool `json:"user_has_favorited"` Favorite bool `json:"user_has_favorited"`
} }
func (rl *rootListing) getAfter() string {
if rl == nil || rl.Data == nil {
return ""
}
return rl.Data.After
}
func (rl *rootListing) getBefore() string {
if rl == nil || rl.Data == nil {
return ""
}
return rl.Data.Before
}
func (rl *rootListing) getComments() *Comments { func (rl *rootListing) getComments() *Comments {
v := new(Comments) return &Comments{
if rl != nil && rl.Data != nil { Comments: rl.Data.Things.Comments,
v.Comments = rl.Data.Things.Comments After: rl.Data.After,
v.After = rl.Data.After Before: rl.Data.Before,
v.Before = rl.Data.Before
} }
return v
} }
func (rl *rootListing) getMoreComments() *More { func (rl *rootListing) getMoreComments() *More {
if rl == nil || rl.Data == nil {
return nil
}
return rl.Data.Things.MoreComments return rl.Data.Things.MoreComments
} }
func (rl *rootListing) getUsers() *Users { func (rl *rootListing) getUsers() *Users {
v := new(Users) return &Users{
if rl != nil && rl.Data != nil { Users: rl.Data.Things.Users,
v.Users = rl.Data.Things.Users After: rl.Data.After,
v.After = rl.Data.After Before: rl.Data.Before,
v.Before = rl.Data.Before
} }
return v
} }
func (rl *rootListing) getPosts() *Posts { func (rl *rootListing) getPosts() *Posts {
v := new(Posts) return &Posts{
if rl != nil && rl.Data != nil { Posts: rl.Data.Things.Posts,
v.Posts = rl.Data.Things.Posts After: rl.Data.After,
v.After = rl.Data.After Before: rl.Data.Before,
v.Before = rl.Data.Before
} }
return v
} }
func (rl *rootListing) getSubreddits() *Subreddits { func (rl *rootListing) getSubreddits() *Subreddits {
v := new(Subreddits) return &Subreddits{
if rl != nil && rl.Data != nil { Subreddits: rl.Data.Things.Subreddits,
v.Subreddits = rl.Data.Things.Subreddits After: rl.Data.After,
v.After = rl.Data.After Before: rl.Data.Before,
v.Before = rl.Data.Before
} }
return v
} }
func (rl *rootListing) getModeratorActions() *ModActions { func (rl *rootListing) getModActions() *ModActions {
v := new(ModActions) return &ModActions{
if rl != nil && rl.Data != nil { ModActions: rl.Data.Things.ModActions,
v.ModActions = rl.Data.Things.ModActions After: rl.Data.After,
v.After = rl.Data.After Before: rl.Data.Before,
v.Before = rl.Data.Before
} }
return v
} }
// Comments is a list of comments // Comments is a list of comments