Use anonymous structs in UnmarshalJSON implementations

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-30 13:01:18 -04:00
parent d65358b6cc
commit 4303d59c03
3 changed files with 30 additions and 25 deletions

View file

@ -51,19 +51,18 @@ type SubredditNames []string
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
func (n *SubredditNames) UnmarshalJSON(data []byte) error { 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) err := json.Unmarshal(data, &subreddits)
if err != nil { if err != nil {
return err return err
} }
for _, subreddit := range subreddits { for _, sr := range subreddits {
name, ok := subreddit["name"] *n = append(*n, sr.Name)
if !ok {
continue
}
*n = append(*n, name)
} }
return nil return nil
@ -71,12 +70,13 @@ func (n *SubredditNames) UnmarshalJSON(data []byte) error {
// MarshalJSON implements the json.Marshaler interface. // MarshalJSON implements the json.Marshaler interface.
func (n *SubredditNames) MarshalJSON() ([]byte, error) { 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 { for _, name := range *n {
subreddits = append(subreddits, map[string]string{ subreddits = append(subreddits, subreddit{name})
"name": sr,
})
} }
return json.Marshal(subreddits) return json.Marshal(subreddits)
@ -195,7 +195,7 @@ func (s *MultiService) Of(ctx context.Context, username string) ([]Multi, *Respo
// Copy copies a multireddit. // Copy copies a multireddit.
func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest) (*Multi, *Response, error) { func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest) (*Multi, *Response, error) {
if copyRequest == nil { 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" path := "api/multi/copy"
@ -217,7 +217,7 @@ func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest)
// Create creates a multireddit. // Create creates a multireddit.
func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) { func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) {
if createRequest == nil { if createRequest == nil {
return nil, nil, errors.New("createRequest cannot be nil") return nil, nil, errors.New("createRequest: cannot be nil")
} }
path := "api/multi" 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. // If the multireddit does not exist, it will be created.
func (s *MultiService) Update(ctx context.Context, multiPath string, updateRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) { func (s *MultiService) Update(ctx context.Context, multiPath string, updateRequest *MultiCreateOrUpdateRequest) (*Multi, *Response, error) {
if updateRequest == nil { 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) path := fmt.Sprintf("api/multi/%s", multiPath)

View file

@ -17,7 +17,7 @@ var expectedPostAndComments = &PostAndComments{
Created: &Timestamp{time.Date(2020, 7, 18, 10, 26, 7, 0, time.UTC)}, 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)}, 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/", URL: "https://www.reddit.com/r/test/comments/testpost/test/",
Title: "Test", Title: "Test",
@ -44,7 +44,7 @@ var expectedPostAndComments = &PostAndComments{
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
ParentID: "t3_testpost", 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", Body: "Hi",
Author: "testuser", Author: "testuser",
@ -71,7 +71,7 @@ var expectedPostAndComments = &PostAndComments{
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
ParentID: "t1_testc1", 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", Body: "Hello",
Author: "testuser", Author: "testuser",

View file

@ -32,7 +32,8 @@ func (p *Permalink) UnmarshalJSON(data []byte) error {
if err != nil { if err != nil {
return err return err
} }
*p = Permalink("https://www.reddit.com" + v) v = "https://www.reddit.com" + v
*p = Permalink(v)
return nil return nil
} }
@ -89,16 +90,20 @@ func (t *Things) init() {
func (t *Things) UnmarshalJSON(b []byte) error { func (t *Things) UnmarshalJSON(b []byte) error {
t.init() t.init()
var children []map[string]interface{} type thing struct {
if err := json.Unmarshal(b, &children); err != nil { Kind string `json:"kind"`
Data interface{} `json:"data"`
}
var things []thing
if err := json.Unmarshal(b, &things); err != nil {
return err return err
} }
for _, child := range children { for _, thing := range things {
data := child["data"] byteValue, _ := json.Marshal(thing.Data)
byteValue, _ := json.Marshal(data)
switch child["kind"] { switch thing.Kind {
case kindComment: case kindComment:
v := new(Comment) v := new(Comment)
if err := json.Unmarshal(byteValue, v); err == nil { if err := json.Unmarshal(byteValue, v); err == nil {