Add Multi to thing struct

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-08-31 12:51:45 -04:00
parent 57ee41b2cf
commit b42aa44c44
5 changed files with 36 additions and 39 deletions

View file

@ -19,11 +19,6 @@ type MultiService struct {
client *Client
}
type multiRoot struct {
Kind string `json:"kind,omitempty"`
Data *Multi `json:"data,omitempty"`
}
// Multi is a multireddit, i.e. a customizable group of subreddits.
// Users can create multis for custom navigation, instead of browsing
// one subreddit or all subreddits at a time.
@ -75,8 +70,8 @@ func (n *SubredditNames) MarshalJSON() ([]byte, error) {
type subreddit struct {
Name string `json:"name"`
}
var subreddits []subreddit
subreddits := make([]subreddit, 0)
for _, name := range *n {
subreddits = append(subreddits, subreddit{name})
}
@ -128,17 +123,17 @@ func (s *MultiService) Get(ctx context.Context, multiPath string) (*Multi, *Resp
return nil, nil, err
}
root := new(multiRoot)
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Data, resp, nil
return root.Multi(), resp, nil
}
// Mine returns your multireddits.
func (s *MultiService) Mine(ctx context.Context) ([]Multi, *Response, error) {
func (s *MultiService) Mine(ctx context.Context) ([]*Multi, *Response, error) {
path := "api/multi/mine"
req, err := s.client.NewRequest(http.MethodGet, path, nil)
@ -146,23 +141,18 @@ func (s *MultiService) Mine(ctx context.Context) ([]Multi, *Response, error) {
return nil, nil, err
}
var root []multiRoot
resp, err := s.client.Do(ctx, req, &root)
root := new(things)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
multis := make([]Multi, 0)
for _, multi := range root {
multis = append(multis, *multi.Data)
}
return multis, resp, nil
return root.Multis, resp, nil
}
// Of returns the user's public multireddits.
// Or, if the user is you, all of your multireddits.
func (s *MultiService) Of(ctx context.Context, username string) ([]Multi, *Response, error) {
func (s *MultiService) Of(ctx context.Context, username string) ([]*Multi, *Response, error) {
path := fmt.Sprintf("api/multi/user/%s", username)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
@ -170,18 +160,13 @@ func (s *MultiService) Of(ctx context.Context, username string) ([]Multi, *Respo
return nil, nil, err
}
var root []multiRoot
resp, err := s.client.Do(ctx, req, &root)
root := new(things)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
multis := make([]Multi, 0)
for _, multi := range root {
multis = append(multis, *multi.Data)
}
return multis, resp, nil
return root.Multis, resp, nil
}
// Copy a multireddit.
@ -201,13 +186,13 @@ func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest)
return nil, nil, err
}
root := new(multiRoot)
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Data, resp, nil
return root.Multi(), resp, nil
}
// Create a multireddit.
@ -223,13 +208,13 @@ func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrU
return nil, nil, err
}
root := new(multiRoot)
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Data, resp, nil
return root.Multi(), resp, nil
}
// Update a multireddit.
@ -246,13 +231,13 @@ func (s *MultiService) Update(ctx context.Context, multiPath string, updateReque
return nil, nil, err
}
root := new(multiRoot)
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Data, resp, nil
return root.Multi(), resp, nil
}
// Delete a multireddit.

View file

@ -74,7 +74,7 @@ func TestMultiService_Mine(t *testing.T) {
multis, _, err := client.Multi.Mine(ctx)
require.NoError(t, err)
require.Equal(t, []Multi{*expectedMulti, *expectedMulti2}, multis)
require.Equal(t, []*Multi{expectedMulti, expectedMulti2}, multis)
}
func TestMultiService_Of(t *testing.T) {
@ -91,7 +91,7 @@ func TestMultiService_Of(t *testing.T) {
multis, _, err := client.Multi.Of(ctx, "test")
require.NoError(t, err)
require.Equal(t, []Multi{*expectedMulti, *expectedMulti2}, multis)
require.Equal(t, []*Multi{expectedMulti, expectedMulti2}, multis)
}
func TestMultiService_Copy(t *testing.T) {

View file

@ -20,7 +20,7 @@ type PostService struct {
client *Client
}
type submittedLinkRoot struct {
type rootSubmittedPost struct {
JSON struct {
Data *Submitted `json:"data,omitempty"`
} `json:"json"`
@ -125,7 +125,7 @@ func (s *PostService) submit(ctx context.Context, v interface{}) (*Submitted, *R
return nil, nil, err
}
root := new(submittedLinkRoot)
root := new(rootSubmittedPost)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err

View file

@ -341,7 +341,7 @@ func (c *Client) id(ctx context.Context) (string, *Response, error) {
return "", resp, err
}
c.redditID = fmt.Sprintf("%s_%s", kindAccount, self.ID)
c.redditID = fmt.Sprintf("%s_%s", kindUser, self.ID)
return c.redditID, resp, nil
}

View file

@ -7,7 +7,7 @@ import (
const (
kindComment = "t1"
kindAccount = "t2"
kindUser = "t2"
kindPost = "t3"
kindMessage = "t4"
kindSubreddit = "t5"
@ -18,6 +18,7 @@ const (
kindUserList = "UserList"
kindMore = "more"
kindModAction = "modaction"
kindMulti = "LabeledMulti"
)
// thing is an entity on Reddit.
@ -48,7 +49,7 @@ func (t *thing) UnmarshalJSON(b []byte) error {
v = new(Comment)
case kindMore:
v = new(More)
case kindAccount:
case kindUser:
v = new(User)
case kindPost:
v = new(Post)
@ -56,6 +57,8 @@ func (t *thing) UnmarshalJSON(b []byte) error {
v = new(Subreddit)
case kindModAction:
v = new(ModAction)
case kindMulti:
v = new(Multi)
default:
return fmt.Errorf("unrecognized kind: %q", t.Kind)
}
@ -99,6 +102,11 @@ func (t *thing) ModAction() *ModAction {
return v
}
func (t *thing) Multi() *Multi {
v, _ := t.Data.(*Multi)
return v
}
type anchor interface {
After() string
Before() string
@ -149,6 +157,7 @@ type things struct {
Posts []*Post
Subreddits []*Subreddit
ModActions []*ModAction
Multis []*Multi
}
// init initializes or clears the listing.
@ -159,6 +168,7 @@ func (t *things) init() {
t.Posts = make([]*Post, 0)
t.Subreddits = make([]*Subreddit, 0)
t.ModActions = make([]*ModAction, 0)
t.Multis = make([]*Multi, 0)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
@ -189,6 +199,8 @@ func (t *things) add(things ...thing) {
t.Subreddits = append(t.Subreddits, v)
case *ModAction:
t.ModActions = append(t.ModActions, v)
case *Multi:
t.Multis = append(t.Multis, v)
}
}
}