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

View file

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

View file

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

View file

@ -341,7 +341,7 @@ func (c *Client) id(ctx context.Context) (string, *Response, error) {
return "", resp, err 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 return c.redditID, resp, nil
} }

View file

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