geddit icon indicating copy to clipboard operation
geddit copied to clipboard

Reddit Comments

Open jzelinskie opened this issue 10 years ago • 4 comments

We're currently using a slapped together implementation to fetch comments from reddit.com. I'd like to completely model the whole thing out using static types and just encoding/json.Unmarshal() the whole thing. A lot of the complexity around the type hierarchy needed to parse the JSON can be hidden by struct embedding.

However, the "replies" field on comments either returns a list of objects or empty string if there are none. Sadly, I think this will require some kind of run-time type assertion.

jzelinskie avatar May 19 '14 23:05 jzelinskie

Is it not possible to make use of omitempty?

rendom avatar Aug 16 '14 02:08 rendom

Nope, I tried here. The problem is that the empty "replies" should be an [] not a "". It's basically a mismatch between a loosely-typed language (JavaScript) and a strongly typed language (Go).

jzelinskie avatar Nov 12 '14 16:11 jzelinskie

You just need to type alias []Comment and then implement the Unmarshaller interface. See example here: https://play.golang.org/p/Gs9DxuKqBx

gmbuell avatar Aug 30 '16 16:08 gmbuell

Yep. That'd definitely work.

jzelinskie avatar Aug 30 '16 20:08 jzelinskie