Return bool when type casting, include trophies in thing struct

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-09-01 19:30:05 -04:00
parent 0e96211c64
commit 5bb7a155de
7 changed files with 81 additions and 63 deletions
+3 -9
View File
@@ -293,7 +293,7 @@ func (s *AccountService) UpdateSettings(ctx context.Context, settings *Settings)
} }
// Trophies returns a list of your trophies. // Trophies returns a list of your trophies.
func (s *AccountService) Trophies(ctx context.Context) ([]Trophy, *Response, error) { func (s *AccountService) Trophies(ctx context.Context) ([]*Trophy, *Response, error) {
path := "api/v1/me/trophies" path := "api/v1/me/trophies"
req, err := s.client.NewRequest(http.MethodGet, path, nil) req, err := s.client.NewRequest(http.MethodGet, path, nil)
@@ -301,19 +301,13 @@ func (s *AccountService) Trophies(ctx context.Context) ([]Trophy, *Response, err
return nil, nil, err return nil, nil, err
} }
root := new(rootTrophyListing) root := new(thing)
resp, err := s.client.Do(ctx, req, root) resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
var trophies []Trophy trophies, _ := root.TrophyList()
for _, trophy := range root.Data.Trophies {
if trophy.Data != nil {
trophies = append(trophies, *trophy.Data)
}
}
return trophies, resp, nil return trophies, resp, nil
} }
+8 -4
View File
@@ -129,7 +129,8 @@ func (s *MultiService) Get(ctx context.Context, multiPath string) (*Multi, *Resp
return nil, resp, err return nil, resp, err
} }
return root.Multi(), resp, nil multi, _ := root.Multi()
return multi, resp, nil
} }
// Mine returns your multireddits. // Mine returns your multireddits.
@@ -192,7 +193,8 @@ func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest)
return nil, resp, err return nil, resp, err
} }
return root.Multi(), resp, nil multi, _ := root.Multi()
return multi, resp, nil
} }
// Create a multireddit. // Create a multireddit.
@@ -214,7 +216,8 @@ func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrU
return nil, resp, err return nil, resp, err
} }
return root.Multi(), resp, nil multi, _ := root.Multi()
return multi, resp, nil
} }
// Update a multireddit. // Update a multireddit.
@@ -237,7 +240,8 @@ func (s *MultiService) Update(ctx context.Context, multiPath string, updateReque
return nil, resp, err return nil, resp, err
} }
return root.Multi(), resp, nil multi, _ := root.Multi()
return multi, resp, nil
} }
// Delete a multireddit. // Delete a multireddit.
+3 -3
View File
@@ -190,7 +190,7 @@ func (c *Client) redirect(req *http.Request, via []*http.Request) error {
} }
// The readonly Reddit url needs .json at the end of its path to return responses in JSON instead of HTML. // The readonly Reddit url needs .json at the end of its path to return responses in JSON instead of HTML.
func (c *Client) appendJSONExtensionToRequestPath(req *http.Request) { func (c *Client) appendJSONExtensionToRequestURLPath(req *http.Request) {
readonlyURL, err := url.Parse(defaultBaseURLReadonly) readonlyURL, err := url.Parse(defaultBaseURLReadonly)
if err != nil { if err != nil {
return return
@@ -238,7 +238,7 @@ func (c *Client) NewRequest(method string, path string, body interface{}) (*http
return nil, err return nil, err
} }
c.appendJSONExtensionToRequestPath(req) c.appendJSONExtensionToRequestURLPath(req)
req.Header.Add(headerContentType, mediaTypeJSON) req.Header.Add(headerContentType, mediaTypeJSON)
req.Header.Add(headerAccept, mediaTypeJSON) req.Header.Add(headerAccept, mediaTypeJSON)
@@ -259,7 +259,7 @@ func (c *Client) NewRequestWithForm(method string, path string, form url.Values)
return nil, err return nil, err
} }
c.appendJSONExtensionToRequestPath(req) c.appendJSONExtensionToRequestURLPath(req)
req.Header.Add(headerContentType, mediaTypeForm) req.Header.Add(headerContentType, mediaTypeForm)
req.Header.Add(headerAccept, mediaTypeJSON) req.Header.Add(headerAccept, mediaTypeJSON)
+2 -1
View File
@@ -134,7 +134,8 @@ func (s *SubredditService) Get(ctx context.Context, name string) (*Subreddit, *R
return nil, resp, err return nil, resp, err
} }
return root.Subreddit(), resp, nil sr, _ := root.Subreddit()
return sr, resp, nil
} }
// Popular returns popular subreddits. // Popular returns popular subreddits.
+58 -22
View File
@@ -11,7 +11,7 @@ const (
kindPost = "t3" kindPost = "t3"
kindMessage = "t4" kindMessage = "t4"
kindSubreddit = "t5" kindSubreddit = "t5"
kindAward = "t6" kindTrophy = "t6"
kindListing = "Listing" kindListing = "Listing"
kindKarmaList = "KarmaList" kindKarmaList = "KarmaList"
kindTrophyList = "TrophyList" kindTrophyList = "TrophyList"
@@ -59,6 +59,10 @@ func (t *thing) UnmarshalJSON(b []byte) error {
v = new(ModAction) v = new(ModAction)
case kindMulti: case kindMulti:
v = new(Multi) v = new(Multi)
case kindTrophy:
v = new(Trophy)
case kindTrophyList:
v = new(trophyList)
default: default:
return fmt.Errorf("unrecognized kind: %q", t.Kind) return fmt.Errorf("unrecognized kind: %q", t.Kind)
} }
@@ -72,39 +76,49 @@ func (t *thing) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func (t *thing) Comment() *Comment { func (t *thing) Comment() (v *Comment, ok bool) {
v, _ := t.Data.(*Comment) v, ok = t.Data.(*Comment)
return v return
} }
func (t *thing) More() *More { func (t *thing) More() (v *More, ok bool) {
v, _ := t.Data.(*More) v, ok = t.Data.(*More)
return v return
} }
func (t *thing) User() *User { func (t *thing) User() (v *User, ok bool) {
v, _ := t.Data.(*User) v, ok = t.Data.(*User)
return v return
} }
func (t *thing) Post() *Post { func (t *thing) Post() (v *Post, ok bool) {
v, _ := t.Data.(*Post) v, ok = t.Data.(*Post)
return v return
} }
func (t *thing) Subreddit() *Subreddit { func (t *thing) Subreddit() (v *Subreddit, ok bool) {
v, _ := t.Data.(*Subreddit) v, ok = t.Data.(*Subreddit)
return v return
} }
func (t *thing) ModAction() *ModAction { func (t *thing) ModAction() (v *ModAction, ok bool) {
v, _ := t.Data.(*ModAction) v, ok = t.Data.(*ModAction)
return v return
} }
func (t *thing) Multi() *Multi { func (t *thing) Multi() (v *Multi, ok bool) {
v, _ := t.Data.(*Multi) v, ok = t.Data.(*Multi)
return v return
}
func (t *thing) Trophy() (v *Trophy, ok bool) {
v, ok = t.Data.(*Trophy)
return
}
func (t *thing) TrophyList() ([]*Trophy, bool) {
v, ok := t.Data.(*trophyList)
return *v, ok
} }
type anchor interface { type anchor interface {
@@ -205,6 +219,28 @@ func (t *things) add(things ...thing) {
} }
} }
type trophyList []*Trophy
// UnmarshalJSON implements the json.Unmarshaler interface.
func (l *trophyList) UnmarshalJSON(b []byte) error {
root := new(struct {
Trophies []thing `json:"trophies"`
})
err := json.Unmarshal(b, root)
if err != nil {
return err
}
for _, thing := range root.Trophies {
if trophy, ok := thing.Trophy(); ok {
*l = append(*l, trophy)
}
}
return nil
}
// Comment is a comment posted by a user. // Comment is a comment posted by a user.
type Comment struct { type Comment struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
+6 -23
View File
@@ -51,18 +51,6 @@ type Blocked struct {
Created *Timestamp `json:"date,omitempty"` Created *Timestamp `json:"date,omitempty"`
} }
type rootTrophyListing struct {
Kind string `json:"kind,omitempty"`
Data struct {
Trophies []rootTrophy `json:"trophies"`
} `json:"data"`
}
type rootTrophy struct {
Kind string `json:"kind,omitempty"`
Data *Trophy `json:"data,omitempty"`
}
// Trophy is a Reddit award. // Trophy is a Reddit award.
type Trophy struct { type Trophy struct {
ID string `json:"id"` ID string `json:"id"`
@@ -84,7 +72,8 @@ func (s *UserService) Get(ctx context.Context, username string) (*User, *Respons
return nil, resp, err return nil, resp, err
} }
return root.User(), resp, nil user, _ := root.User()
return user, resp, nil
} }
// GetMultipleByID returns multiple users from their full IDs. // GetMultipleByID returns multiple users from their full IDs.
@@ -482,12 +471,12 @@ func (s *UserService) UnblockByID(ctx context.Context, id string) (*Response, er
} }
// Trophies returns a list of your trophies. // Trophies returns a list of your trophies.
func (s *UserService) Trophies(ctx context.Context) ([]Trophy, *Response, error) { func (s *UserService) Trophies(ctx context.Context) ([]*Trophy, *Response, error) {
return s.TrophiesOf(ctx, s.client.Username) return s.TrophiesOf(ctx, s.client.Username)
} }
// TrophiesOf returns a list of the specified user's trophies. // TrophiesOf returns a list of the specified user's trophies.
func (s *UserService) TrophiesOf(ctx context.Context, username string) ([]Trophy, *Response, error) { func (s *UserService) TrophiesOf(ctx context.Context, username string) ([]*Trophy, *Response, error) {
path := fmt.Sprintf("api/v1/user/%s/trophies", username) path := fmt.Sprintf("api/v1/user/%s/trophies", username)
req, err := s.client.NewRequest(http.MethodGet, path, nil) req, err := s.client.NewRequest(http.MethodGet, path, nil)
@@ -495,19 +484,13 @@ func (s *UserService) TrophiesOf(ctx context.Context, username string) ([]Trophy
return nil, nil, err return nil, nil, err
} }
root := new(rootTrophyListing) root := new(thing)
resp, err := s.client.Do(ctx, req, root) resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
var trophies []Trophy trophies, _ := root.TrophyList()
for _, trophy := range root.Data.Trophies {
if trophy.Data != nil {
trophies = append(trophies, *trophy.Data)
}
}
return trophies, resp, nil return trophies, resp, nil
} }
+1 -1
View File
@@ -116,7 +116,7 @@ var expectedBlocked = &Blocked{
Created: &Timestamp{time.Date(2020, 6, 16, 16, 49, 50, 0, time.UTC)}, Created: &Timestamp{time.Date(2020, 6, 16, 16, 49, 50, 0, time.UTC)},
} }
var expectedTrophies = []Trophy{ var expectedTrophies = []*Trophy{
{ {
ID: "", ID: "",
Name: "Three-Year Club", Name: "Three-Year Club",