Create Permalink type, tweak structs
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
@@ -75,6 +75,8 @@ func (s *ListingsServiceOp) GetPosts(ctx context.Context, ids ...string) ([]Post
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetPost returns a post with its comments.
|
// GetPost returns a post with its comments.
|
||||||
|
// The id here is the ID36 of the post, not its full id.
|
||||||
|
// Example: instead of t3_abc123, use abc123.
|
||||||
func (s *ListingsServiceOp) GetPost(ctx context.Context, id string) (*PostAndComments, *Response, error) {
|
func (s *ListingsServiceOp) GetPost(ctx context.Context, id string) (*PostAndComments, *Response, error) {
|
||||||
path := fmt.Sprintf("comments/%s", id)
|
path := fmt.Sprintf("comments/%s", id)
|
||||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
||||||
|
|||||||
@@ -83,6 +83,20 @@ func (t Timespan) String() string {
|
|||||||
return timespans[t]
|
return timespans[t]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Permalink is the link to a post or comment.
|
||||||
|
type Permalink string
|
||||||
|
|
||||||
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||||
|
func (p *Permalink) UnmarshalJSON(data []byte) error {
|
||||||
|
var v string
|
||||||
|
err := json.Unmarshal(data, &v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*p = Permalink("https://www.reddit.com" + v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type root struct {
|
type root struct {
|
||||||
Kind string `json:"kind,omitempty"`
|
Kind string `json:"kind,omitempty"`
|
||||||
Data interface{} `json:"data,omitempty"`
|
Data interface{} `json:"data,omitempty"`
|
||||||
@@ -130,8 +144,25 @@ type subredditRoot struct {
|
|||||||
Data *Subreddit `json:"data,omitempty"`
|
Data *Subreddit `json:"data,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Things) init() {
|
||||||
|
if t.Comments == nil {
|
||||||
|
t.Comments = make([]Comment, 0)
|
||||||
|
}
|
||||||
|
if t.Users == nil {
|
||||||
|
t.Users = make([]User, 0)
|
||||||
|
}
|
||||||
|
if t.Posts == nil {
|
||||||
|
t.Posts = make([]Post, 0)
|
||||||
|
}
|
||||||
|
if t.Subreddits == nil {
|
||||||
|
t.Subreddits = make([]Subreddit, 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()
|
||||||
|
|
||||||
var children []map[string]interface{}
|
var children []map[string]interface{}
|
||||||
if err := json.Unmarshal(b, &children); err != nil {
|
if err := json.Unmarshal(b, &children); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -177,7 +208,7 @@ type Comment struct {
|
|||||||
Edited *Timestamp `json:"edited,omitempty"`
|
Edited *Timestamp `json:"edited,omitempty"`
|
||||||
|
|
||||||
ParentID string `json:"parent_id,omitempty"`
|
ParentID string `json:"parent_id,omitempty"`
|
||||||
Permalink string `json:"permalink,omitempty"`
|
Permalink Permalink `json:"permalink,omitempty"`
|
||||||
|
|
||||||
Body string `json:"body,omitempty"`
|
Body string `json:"body,omitempty"`
|
||||||
Author string `json:"author,omitempty"`
|
Author string `json:"author,omitempty"`
|
||||||
@@ -200,7 +231,7 @@ type Comment struct {
|
|||||||
|
|
||||||
// These don't appear when submitting a comment
|
// These don't appear when submitting a comment
|
||||||
PostTitle string `json:"link_title,omitempty"`
|
PostTitle string `json:"link_title,omitempty"`
|
||||||
PostPermalink string `json:"link_permalink,omitempty"`
|
PostPermalink Permalink `json:"link_permalink,omitempty"`
|
||||||
PostAuthor string `json:"link_author,omitempty"`
|
PostAuthor string `json:"link_author,omitempty"`
|
||||||
PostNumComments int `json:"num_comments"`
|
PostNumComments int `json:"num_comments"`
|
||||||
|
|
||||||
@@ -215,7 +246,7 @@ type Comment struct {
|
|||||||
Replies Replies `json:"replies"`
|
Replies Replies `json:"replies"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replies are replies to a comment
|
// Replies are replies to a comment.
|
||||||
type Replies []Comment
|
type Replies []Comment
|
||||||
|
|
||||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||||
@@ -242,7 +273,7 @@ type Post struct {
|
|||||||
Created *Timestamp `json:"created_utc,omitempty"`
|
Created *Timestamp `json:"created_utc,omitempty"`
|
||||||
Edited *Timestamp `json:"edited,omitempty"`
|
Edited *Timestamp `json:"edited,omitempty"`
|
||||||
|
|
||||||
Permalink string `json:"permalink,omitempty"`
|
Permalink Permalink `json:"permalink,omitempty"`
|
||||||
URL string `json:"url,omitempty"`
|
URL string `json:"url,omitempty"`
|
||||||
|
|
||||||
Title string `json:"title,omitempty"`
|
Title string `json:"title,omitempty"`
|
||||||
@@ -347,36 +378,36 @@ func (rl *rootListing) getSubreddits() *Subreddits {
|
|||||||
|
|
||||||
// Comments is a list of comments
|
// Comments is a list of comments
|
||||||
type Comments struct {
|
type Comments struct {
|
||||||
Comments []Comment `json:"comments,omitempty"`
|
Comments []Comment `json:"comments"`
|
||||||
After string `json:"after"`
|
After string `json:"after"`
|
||||||
Before string `json:"before"`
|
Before string `json:"before"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Users is a list of users
|
// Users is a list of users
|
||||||
type Users struct {
|
type Users struct {
|
||||||
Users []User `json:"users,omitempty"`
|
Users []User `json:"users"`
|
||||||
After string `json:"after"`
|
After string `json:"after"`
|
||||||
Before string `json:"before"`
|
Before string `json:"before"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subreddits is a list of subreddits
|
// Subreddits is a list of subreddits
|
||||||
type Subreddits struct {
|
type Subreddits struct {
|
||||||
Subreddits []Subreddit `json:"subreddits,omitempty"`
|
Subreddits []Subreddit `json:"subreddits"`
|
||||||
After string `json:"after"`
|
After string `json:"after"`
|
||||||
Before string `json:"before"`
|
Before string `json:"before"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Posts is a list of posts.
|
// Posts is a list of posts.
|
||||||
type Posts struct {
|
type Posts struct {
|
||||||
Posts []Post `json:"submissions,omitempty"`
|
Posts []Post `json:"posts"`
|
||||||
After string `json:"after"`
|
After string `json:"after"`
|
||||||
Before string `json:"before"`
|
Before string `json:"before"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostAndComments is a post and its comments
|
// PostAndComments is a post and its comments
|
||||||
type PostAndComments struct {
|
type PostAndComments struct {
|
||||||
Post Post `json:"post,omitempty"`
|
Post Post `json:"post"`
|
||||||
Comments []Comment `json:"comments,omitempty"`
|
Comments []Comment `json:"comments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||||
|
|||||||
Reference in New Issue
Block a user