Use json.RawMessage ([]byte) as the type for thing data

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-31 08:24:28 -04:00
parent 4303d59c03
commit dad9def22c

View file

@ -92,7 +92,7 @@ func (t *Things) UnmarshalJSON(b []byte) error {
type thing struct { type thing struct {
Kind string `json:"kind"` Kind string `json:"kind"`
Data interface{} `json:"data"` Data json.RawMessage `json:"data"`
} }
var things []thing var things []thing
@ -101,39 +101,37 @@ func (t *Things) UnmarshalJSON(b []byte) error {
} }
for _, thing := range things { for _, thing := range things {
byteValue, _ := json.Marshal(thing.Data)
switch thing.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(thing.Data, v); err == nil {
t.Comments = append(t.Comments, v) t.Comments = append(t.Comments, v)
} }
case kindMore: case kindMore:
v := new(More) v := new(More)
if err := json.Unmarshal(byteValue, v); err == nil { if err := json.Unmarshal(thing.Data, v); err == nil {
t.MoreComments = v t.MoreComments = v
} }
case kindAccount: case kindAccount:
v := new(User) 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) t.Users = append(t.Users, v)
} }
case kindLink: case kindLink:
v := new(Post) 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) t.Posts = append(t.Posts, v)
} }
case kindMessage: case kindMessage:
case kindSubreddit: case kindSubreddit:
v := new(Subreddit) 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) t.Subreddits = append(t.Subreddits, v)
} }
case kindAward: case kindAward:
case kindModAction: case kindModAction:
v := new(ModAction) 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) t.ModActions = append(t.ModActions, v)
} }
} }