Skip to content

Commit

Permalink
Improve & reduce golang parsing utility.
Browse files Browse the repository at this point in the history
This removes the listing parsing functions for specific types of listings,
because it can all be done with one function even when the listings are
monotyped. Mixed listings are very common endpoints on Reddit.
  • Loading branch information
turnage committed Oct 29, 2015
1 parent 601aa8e commit 8b9bd66
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 121 deletions.
54 changes: 11 additions & 43 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,6 @@ func ParseComment(raw json.RawMessage) (*Comment, error) {
return comment, nil
}

// ParseLinkListing parses a JSON message expected to be a listing of Links
// (Link is the Reddit type for what may also be called posts or submissions).
// Links do not have a comments field except when fetched from their own
// comments page; the Links returned from this function have no comments
// included.
func ParseLinkListing(raw json.RawMessage) ([]*Link, error) {
thing, err := handleThing(raw)
if err != nil {
return nil, err
}

buffer, ok := thing.(*listingBuffer)
if !ok {
return nil, fmt.Errorf("JSON message was not a listing")
}

return buffer.links, nil
}

// ParseMessageListing parses a JSON message expected to be a listing of
// Messages (e.g. an inbox JSON digest). Reddit will provide comments as
// Messages if viewed from the inbox--use GetWasComment() to identify the
// original type.
func ParseMessageListing(raw json.RawMessage) ([]*Message, error) {
thing, err := handleThing(raw)
if err != nil {
return nil, err
}

buffer, ok := thing.(*listingBuffer)
if !ok {
return nil, fmt.Errorf("JSON message was not a listing")
}

return buffer.messages, nil
}

// ParseThread parses a JSON message expected to represent a Link comment page.
func ParseThread(raw json.RawMessage) (*Link, error) {
// The JSON message should be a top level array, holding the link in the
Expand Down Expand Up @@ -126,20 +89,25 @@ func ParseThread(raw json.RawMessage) (*Link, error) {
return link, nil
}

// ParseComboListing parses a JSON message expected to be a listing with a mix
// of comments and posts (e.g. a user landing page).
func ParseComboListing(raw json.RawMessage) ([]*Link, []*Comment, error) {
// ParseListing parses a JSON message expected to be a listing with any mix of
// Links, Comments, or Messages.
func ParseListing(raw json.RawMessage) (
[]*Link,
[]*Comment,
[]*Message,
error,
) {
thing, err := handleThing(raw)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}

buffer, ok := thing.(*listingBuffer)
if !ok {
return nil, nil, fmt.Errorf("JSON message was nonlisting")
return nil, nil, nil, fmt.Errorf("JSON message was nonlisting")
}

return buffer.links, buffer.comments, nil
return buffer.links, buffer.comments, buffer.messages, nil
}

// handleThing unmarshals Things and parses them.
Expand Down
87 changes: 11 additions & 76 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,80 +60,6 @@ func TestParseComment(t *testing.T) {
}
}

func TestParseLinkListing(t *testing.T) {
listing, err := ParseLinkListing([]byte(
`{
"kind": "Listing",
"data": {
"children": [
{
"kind": "t3",
"data": {
"title": "1"
}
},
{
"kind": "t3",
"data": {
"title": "2"
}
},
{
"kind": "t3",
"data": {
"title": "3"
}
}
]
}
}`))
if err != nil {
t.Fatal(err)
}

if len(listing) != 3 {
t.Fatalf("got %d links; wanted 3", len(listing))
}

if listing[2].GetTitle() != "3" {
t.Errorf("got %s as title; wanted 3", listing[2].GetTitle())
}
}

func TestParseMessageListing(t *testing.T) {
listing, err := ParseMessageListing([]byte(
`{
"kind": "Listing",
"data": {
"children": [
{
"kind": "t4",
"data": {
"body": "1"
}
},
{
"kind": "t4",
"data": {
"body": "2"
}
}
]
}
}`))
if err != nil {
t.Fatal(err)
}

if len(listing) != 2 {
t.Fatalf("got %d messages; wanted 2", len(listing))
}

if listing[1].GetBody() != "2" {
t.Errorf("got %s as body; wanted 2", listing[1].GetBody())
}
}

func TestParseThread(t *testing.T) {
link, err := ParseThread([]byte(
`[
Expand Down Expand Up @@ -183,8 +109,8 @@ func TestParseThread(t *testing.T) {
}
}

func TestParseComboListing(t *testing.T) {
links, comments, err := ParseComboListing([]byte(
func TestParseListing(t *testing.T) {
links, comments, messages, err := ParseListing([]byte(
`{
"kind": "Listing",
"data": {
Expand All @@ -200,6 +126,12 @@ func TestParseComboListing(t *testing.T) {
"data": {
"body": "1"
}
},
{
"kind": "t4",
"data": {
"body": "1"
}
}
]
}
Expand All @@ -215,4 +147,7 @@ func TestParseComboListing(t *testing.T) {
if len(comments) != 1 {
t.Errorf("got %d comments; wanted 1", len(comments))
}
if len(messages) != 1 {
t.Errorf("got %d messages; wanted 1", len(messages))
}
}
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ See ````protoc --help```` for further guidance.
No breaking changes will be made to redditproto messages as of version 1.0. Any
message changes will be noted here in the readme.

* 0.9 -> August 25th, 2015
* 1.0 -> October 21st, 2015
* 0.9.0 -> August 25th, 2015
* 1.0.0 -> October 21st, 2015
* 2.0.0 -> Octover 29th, 2015 (No message changes; improve Go utilities).

# Gophers

Expand Down

0 comments on commit 8b9bd66

Please sign in to comment.