diff --git a/multi.go b/multi.go index 02b1233..4f2939e 100644 --- a/multi.go +++ b/multi.go @@ -51,19 +51,18 @@ type SubredditNames []string // UnmarshalJSON implements the json.Unmarshaler interface. func (n *SubredditNames) UnmarshalJSON(data []byte) error { - var subreddits []map[string]string + type subreddit struct { + Name string `json:"name"` + } + var subreddits []subreddit err := json.Unmarshal(data, &subreddits) if err != nil { return err } - for _, subreddit := range subreddits { - name, ok := subreddit["name"] - if !ok { - continue - } - *n = append(*n, name) + for _, sr := range subreddits { + *n = append(*n, sr.Name) } return nil @@ -71,12 +70,13 @@ func (n *SubredditNames) UnmarshalJSON(data []byte) error { // MarshalJSON implements the json.Marshaler interface. func (n *SubredditNames) MarshalJSON() ([]byte, error) { - var subreddits []map[string]string + type subreddit struct { + Name string `json:"name"` + } + var subreddits []subreddit - for _, sr := range *n { - subreddits = append(subreddits, map[string]string{ - "name": sr, - }) + for _, name := range *n { + subreddits = append(subreddits, subreddit{name}) } return json.Marshal(subreddits) @@ -195,7 +195,7 @@ func (s *MultiService) Of(ctx context.Context, username string) ([]Multi, *Respo // Copy copies a multireddit. func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest) (*Multi, *Response, error) { if copyRequest == nil { - return nil, nil, errors.New("copyRequest cannot be nil") + return nil, nil, errors.New("copyRequest: cannot be nil") } path := "api/multi/copy" @@ -217,7 +217,7 @@ func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest) // Create creates a multireddit. func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) { if createRequest == nil { - return nil, nil, errors.New("createRequest cannot be nil") + return nil, nil, errors.New("createRequest: cannot be nil") } path := "api/multi" @@ -240,7 +240,7 @@ func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrU // If the multireddit does not exist, it will be created. func (s *MultiService) Update(ctx context.Context, multiPath string, updateRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) { if updateRequest == nil { - return nil, nil, errors.New("updateRequest cannot be nil") + return nil, nil, errors.New("updateRequest: cannot be nil") } path := fmt.Sprintf("api/multi/%s", multiPath) diff --git a/post_test.go b/post_test.go index a55673b..e7bb315 100644 --- a/post_test.go +++ b/post_test.go @@ -17,7 +17,7 @@ var expectedPostAndComments = &PostAndComments{ Created: &Timestamp{time.Date(2020, 7, 18, 10, 26, 7, 0, time.UTC)}, Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, - Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/"), + Permalink: "https://www.reddit.com/r/test/comments/testpost/test/", URL: "https://www.reddit.com/r/test/comments/testpost/test/", Title: "Test", @@ -44,7 +44,7 @@ var expectedPostAndComments = &PostAndComments{ Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, ParentID: "t3_testpost", - Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/testc1/"), + Permalink: "https://www.reddit.com/r/test/comments/testpost/test/testc1/", Body: "Hi", Author: "testuser", @@ -71,7 +71,7 @@ var expectedPostAndComments = &PostAndComments{ Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, ParentID: "t1_testc1", - Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/testc2/"), + Permalink: "https://www.reddit.com/r/test/comments/testpost/test/testc2/", Body: "Hello", Author: "testuser", diff --git a/things.go b/things.go index 48d20de..740ee09 100644 --- a/things.go +++ b/things.go @@ -32,7 +32,8 @@ func (p *Permalink) UnmarshalJSON(data []byte) error { if err != nil { return err } - *p = Permalink("https://www.reddit.com" + v) + v = "https://www.reddit.com" + v + *p = Permalink(v) return nil } @@ -89,16 +90,20 @@ func (t *Things) init() { func (t *Things) UnmarshalJSON(b []byte) error { t.init() - var children []map[string]interface{} - if err := json.Unmarshal(b, &children); err != nil { + type thing struct { + Kind string `json:"kind"` + Data interface{} `json:"data"` + } + + var things []thing + if err := json.Unmarshal(b, &things); err != nil { return err } - for _, child := range children { - data := child["data"] - byteValue, _ := json.Marshal(data) + for _, thing := range things { + byteValue, _ := json.Marshal(thing.Data) - switch child["kind"] { + switch thing.Kind { case kindComment: v := new(Comment) if err := json.Unmarshal(byteValue, v); err == nil {