From dad9def22c5d16c1e797055aee8c0d980c14c071 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Fri, 31 Jul 2020 08:24:28 -0400 Subject: [PATCH] Use json.RawMessage ([]byte) as the type for thing data Signed-off-by: Vartan Benohanian --- things.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/things.go b/things.go index 740ee09..40a6223 100644 --- a/things.go +++ b/things.go @@ -91,8 +91,8 @@ func (t *Things) UnmarshalJSON(b []byte) error { t.init() type thing struct { - Kind string `json:"kind"` - Data interface{} `json:"data"` + Kind string `json:"kind"` + Data json.RawMessage `json:"data"` } var things []thing @@ -101,39 +101,37 @@ func (t *Things) UnmarshalJSON(b []byte) error { } for _, thing := range things { - byteValue, _ := json.Marshal(thing.Data) - switch thing.Kind { case kindComment: v := new(Comment) - if err := json.Unmarshal(byteValue, v); err == nil { + if err := json.Unmarshal(thing.Data, v); err == nil { t.Comments = append(t.Comments, v) } case kindMore: v := new(More) - if err := json.Unmarshal(byteValue, v); err == nil { + if err := json.Unmarshal(thing.Data, v); err == nil { t.MoreComments = v } case kindAccount: v := new(User) - if err := json.Unmarshal(byteValue, v); err == nil { + if err := json.Unmarshal(thing.Data, v); err == nil { t.Users = append(t.Users, v) } case kindLink: v := new(Post) - if err := json.Unmarshal(byteValue, v); err == nil { + if err := json.Unmarshal(thing.Data, v); err == nil { t.Posts = append(t.Posts, v) } case kindMessage: case kindSubreddit: v := new(Subreddit) - if err := json.Unmarshal(byteValue, v); err == nil { + if err := json.Unmarshal(thing.Data, v); err == nil { t.Subreddits = append(t.Subreddits, v) } case kindAward: case kindModAction: v := new(ModAction) - if err := json.Unmarshal(byteValue, v); err == nil { + if err := json.Unmarshal(thing.Data, v); err == nil { t.ModActions = append(t.ModActions, v) } }