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
+12 -7
View File
@@ -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 {