Make MoreComments field in Things a list

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-08-12 22:55:00 -04:00
parent aa1de29812
commit 4a2284755f
3 changed files with 28 additions and 20 deletions
+2 -2
View File
@@ -76,7 +76,7 @@ func (s *CommentService) LoadMoreReplies(ctx context.Context, comment *Comment)
} }
postID := comment.PostID postID := comment.PostID
commentIDs := comment.Replies.MoreComments.Children commentIDs := comment.Replies.More.Children
type query struct { type query struct {
PostID string `url:"link_id"` PostID string `url:"link_id"`
@@ -112,7 +112,7 @@ func (s *CommentService) LoadMoreReplies(ctx context.Context, comment *Comment)
addCommentToReplies(comment, c) addCommentToReplies(comment, c)
} }
comment.Replies.MoreComments = nil comment.Replies.More = nil
return resp, nil return resp, nil
} }
+4 -4
View File
@@ -717,7 +717,7 @@ func TestPostService_More(t *testing.T) {
ParentID: "t3_123", ParentID: "t3_123",
PostID: "t3_123", PostID: "t3_123",
Replies: Replies{ Replies: Replies{
MoreComments: &More{ More: &More{
Children: []string{"def,ghi"}, Children: []string{"def,ghi"},
}, },
}, },
@@ -743,7 +743,7 @@ func TestPostService_More(t *testing.T) {
_, err = client.Comment.LoadMoreReplies(ctx, parentComment) _, err = client.Comment.LoadMoreReplies(ctx, parentComment)
require.NoError(t, err) require.NoError(t, err)
require.Nil(t, parentComment.Replies.MoreComments) require.Nil(t, parentComment.Replies.More)
require.Len(t, parentComment.Replies.Comments, 1) require.Len(t, parentComment.Replies.Comments, 1)
require.Len(t, parentComment.Replies.Comments[0].Replies.Comments, 1) require.Len(t, parentComment.Replies.Comments[0].Replies.Comments, 1)
} }
@@ -757,7 +757,7 @@ func TestPostService_MoreNil(t *testing.T) {
parentComment := &Comment{ parentComment := &Comment{
Replies: Replies{ Replies: Replies{
MoreComments: nil, More: nil,
}, },
} }
@@ -766,7 +766,7 @@ func TestPostService_MoreNil(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Nil(t, resp) require.Nil(t, resp)
parentComment.Replies.MoreComments = &More{ parentComment.Replies.More = &More{
Children: []string{}, Children: []string{},
} }
+22 -14
View File
@@ -58,18 +58,19 @@ type listing struct {
} }
type things struct { type things struct {
Comments []*Comment Comments []*Comment
MoreComments *More More []*More
Users []*User Users []*User
Posts []*Post Posts []*Post
Subreddits []*Subreddit Subreddits []*Subreddit
ModActions []*ModAction ModActions []*ModAction
// todo: add the other kinds of things // todo: add the other kinds of things
} }
// init initializes or clears the listing. // init initializes or clears the listing.
func (t *things) init() { func (t *things) init() {
t.Comments = make([]*Comment, 0) t.Comments = make([]*Comment, 0)
t.More = make([]*More, 0)
t.Users = make([]*User, 0) t.Users = make([]*User, 0)
t.Posts = make([]*Post, 0) t.Posts = make([]*Post, 0)
t.Subreddits = make([]*Subreddit, 0) t.Subreddits = make([]*Subreddit, 0)
@@ -95,7 +96,7 @@ func (t *things) UnmarshalJSON(b []byte) error {
case kindMore: case kindMore:
v := new(More) v := new(More)
if err := json.Unmarshal(thing.Data, v); err == nil { if err := json.Unmarshal(thing.Data, v); err == nil {
t.MoreComments = v t.More = append(t.More, v)
} }
case kindAccount: case kindAccount:
v := new(User) v := new(User)
@@ -176,15 +177,15 @@ type Comment struct {
} }
func (c *Comment) hasMore() bool { func (c *Comment) hasMore() bool {
return c.Replies.MoreComments != nil && len(c.Replies.MoreComments.Children) > 0 return c.Replies.More != nil && len(c.Replies.More.Children) > 0
} }
// Replies holds replies to a comment. // Replies holds replies to a comment.
// It contains both comments and "more" comments, which are entrypoints to other // It contains both comments and "more" comments, which are entrypoints to other
// comments that were left out. // comments that were left out.
type Replies struct { type Replies struct {
Comments []*Comment `json:"comments,omitempty"` Comments []*Comment `json:"comments,omitempty"`
MoreComments *More `json:"more,omitempty"` More *More `json:"more,omitempty"`
} }
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
@@ -202,7 +203,7 @@ func (r *Replies) UnmarshalJSON(data []byte) error {
} }
r.Comments = root.Data.Things.Comments r.Comments = root.Data.Things.Comments
r.MoreComments = root.Data.Things.MoreComments r.More = root.getFirstMoreComments()
return nil return nil
} }
@@ -298,8 +299,15 @@ func (rl *rootListing) getComments() *Comments {
} }
} }
func (rl *rootListing) getMoreComments() *More { func (rl *rootListing) getMoreComments() []*More {
return rl.Data.Things.MoreComments return rl.Data.Things.More
}
func (rl *rootListing) getFirstMoreComments() *More {
if len(rl.Data.Things.More) == 0 {
return nil
}
return rl.Data.Things.More[0]
} }
func (rl *rootListing) getUsers() *Users { func (rl *rootListing) getUsers() *Users {
@@ -390,7 +398,7 @@ func (pc *PostAndComments) UnmarshalJSON(data []byte) error {
post := l[0].getPosts().Posts[0] post := l[0].getPosts().Posts[0]
comments := l[1].getComments().Comments comments := l[1].getComments().Comments
moreComments := l[1].getMoreComments() moreComments := l[1].getFirstMoreComments()
pc.Post = post pc.Post = post
pc.Comments = comments pc.Comments = comments