simplify title setter with anon func

This commit is contained in:
Umputun
2019-02-02 22:29:02 -06:00
parent 3314223120
commit 38fc7e500c
3 changed files with 13 additions and 9 deletions
+10 -7
View File
@@ -76,14 +76,17 @@ func (s *DataStore) Create(comment store.Comment) (commentID string, err error)
return "", ErrRestrictedWordsFound
}
// keep input title and set to extracted if missing
if s.TitleExtractor != nil && comment.PostTitle == "" {
if title, err := s.TitleExtractor.Get(comment.Locator.URL); err == nil {
comment.PostTitle = title
} else {
log.Printf("[WARN] failed to set title, %v", err)
func() { // keep input title and set to extracted if missing
if s.TitleExtractor == nil || comment.PostTitle != "" {
return
}
}
title, e := s.TitleExtractor.Get(comment.Locator.URL)
if e != nil {
log.Printf("[WARN] failed to set title, %v", e)
return
}
comment.PostTitle = title
}()
return s.Interface.Create(comment)
}
+1 -1
View File
@@ -53,7 +53,7 @@ func (t *TitleExtractor) Get(url string) (string, error) {
return title, nil
})
// on error save result (empty strung) to cache too but
// on error save result (empty string) to cache too and return "" title
if err != nil {
_, _ = t.cache.Get(url, func() (lcw.Value, error) { return "", nil })
return "", err
+2 -1
View File
@@ -1,6 +1,7 @@
package service
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
@@ -84,7 +85,7 @@ func TestTitle_GetConcurrent(t *testing.T) {
for i := 0; i < 100; i++ {
i := i
g.Go(func() {
g.Go(func(_ context.Context) {
title, err := ex.Get(ts.URL + "/good/" + strconv.Itoa(i))
require.Nil(t, err)
assert.Equal(t, "blah 123 "+"/good/"+strconv.Itoa(i), title)