Skip to content

Commit

Permalink
Add passing wg to processors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexferrari88 committed Oct 7, 2022
1 parent 4f67050 commit c27c486
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/gohn/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Item struct {
// ItemProcessor is used by ItemsService.Get and ItemsService.FetchAllKids
// to process items after they are retrieved.
// The package itemprocessor provides some common implementations.
type ItemProcessor func(*Item) error
type ItemProcessor func(*Item, *sync.WaitGroup) error

// Get returns an Item given an ID.
func (s *ItemsService) Get(ctx context.Context, id int) (*Item, error) {
Expand Down Expand Up @@ -137,7 +137,7 @@ L:
return
}
if fn != nil {
err = fn(it)
err = fn(it, &wg)
if err != nil {
// TODO: add better error handling
wg.Done()
Expand Down
15 changes: 8 additions & 7 deletions pkg/gohn/stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ func (s *Story) SetCommentsPosition() {
var n int
var preorder func(int, int)
preorder = func(id int, order int) {
comment := s.CommentsByIdMap[id]
comment.Position = &order
s.CommentsByIdMap[id] = comment
if comment.Kids != nil {
for _, kid := range *comment.Kids {
preorder(kid, n+1)
n++
if comment, ok := s.CommentsByIdMap[id]; ok {
comment.Position = &order
s.CommentsByIdMap[id] = comment
if comment.Kids != nil {
for _, kid := range *comment.Kids {
preorder(kid, n+1)
n++
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/processors/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"fmt"
"strings"
"sync"

"github.com/alexferrari88/gohn/pkg/gohn"
)
Expand All @@ -11,10 +12,12 @@ import (
// The argument title is a boolean that indicates if the filter
// should be applied to the title and not the text.
func FilterOutWords(words []string, title bool) gohn.ItemProcessor {
return func(item *gohn.Item) error {
return func(item *gohn.Item, wg *sync.WaitGroup) error {
if item == nil {
return nil
}
wg.Add(1)
defer wg.Done()
for _, word := range words {
if title && item.Title != nil {
if strings.Contains(strings.ToLower(*item.Title), strings.ToLower(word)) {
Expand All @@ -35,10 +38,12 @@ func FilterOutWords(words []string, title bool) gohn.ItemProcessor {

// FilterOutDeleted filters deleted items
func FilterOutDeleted() gohn.ItemProcessor {
return func(item *gohn.Item) error {
return func(item *gohn.Item, wg *sync.WaitGroup) error {
if item == nil {
return nil
}
wg.Add(1)
defer wg.Done()
if item.Deleted != nil && *item.Deleted {
return fmt.Errorf("Deleted item found")
}
Expand All @@ -48,13 +53,15 @@ func FilterOutDeleted() gohn.ItemProcessor {

// FilterOutUsers filters items that are not from the given user
func FilterOutUsers(users []string) gohn.ItemProcessor {
return func(item *gohn.Item) error {
return func(item *gohn.Item, wg *sync.WaitGroup) error {
if item == nil {
return nil
}
if item.By == nil {
return nil
}
wg.Add(1)
defer wg.Done()
for _, user := range users {
if *item.By == user {
return fmt.Errorf("User found")
Expand Down
5 changes: 4 additions & 1 deletion pkg/processors/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import (
"html"
"sync"

"github.com/alexferrari88/gohn/pkg/gohn"
)

// UnescapeHTML unescapes HTML entities in the text of the item
func UnescapeHTML() gohn.ItemProcessor {
return func(item *gohn.Item) error {
return func(item *gohn.Item, wg *sync.WaitGroup) error {
if item == nil {
return nil
}
if item.Text == nil {
return nil
}
wg.Add(1)
defer wg.Done()
*item.Text = html.UnescapeString(*item.Text)
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/processors/modifiers_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package processors

import (
"sync"
"testing"

"github.com/alexferrari88/gohn/pkg/gohn"
)

func TestUnescapeHTML(t *testing.T) {
expectedText := `This is an <a href="https://www.example.com">example</a>`
var wg sync.WaitGroup
id := 1
i := &gohn.Item{ID: &id, Text: &expectedText}

f := UnescapeHTML()
err := f(i)
err := f(i, &wg)

if err != nil {
t.Fatalf("unexpected error unescaping HTML: %v", err)
Expand Down

0 comments on commit c27c486

Please sign in to comment.