Drop ID package

This commit is contained in:
Maksym Pavlenko
2017-11-03 17:19:44 -07:00
parent 47fe3e41b5
commit d79cc87ea5
6 changed files with 32 additions and 127 deletions
+5 -8
View File
@@ -18,7 +18,6 @@ import (
"github.com/mxpv/podsync/pkg/config"
"github.com/mxpv/podsync/pkg/feeds"
"github.com/mxpv/podsync/pkg/handler"
"github.com/mxpv/podsync/pkg/id"
"github.com/mxpv/podsync/pkg/storage"
"github.com/mxpv/podsync/pkg/support"
"github.com/pkg/errors"
@@ -38,11 +37,6 @@ func main() {
panic(err)
}
hashIds, err := id.NewIdGenerator()
if err != nil {
panic(err)
}
redis, err := storage.NewRedisStorage(cfg.RedisURL)
if err != nil {
panic(err)
@@ -67,13 +61,16 @@ func main() {
panic(err)
}
feed := feeds.NewFeedService(
feeds.WithIdGen(hashIds),
feed, err := feeds.NewFeedService(
feeds.WithStorage(redis),
feeds.WithBuilder(api.ProviderYoutube, youtube),
feeds.WithBuilder(api.ProviderVimeo, vimeo),
)
if err != nil {
panic(err)
}
srv := http.Server{
Addr: fmt.Sprintf(":%d", 5001),
Handler: handler.New(feed, patreon, cfg),
+21 -24
View File
@@ -8,16 +8,13 @@ import (
"github.com/mxpv/podsync/pkg/api"
"github.com/mxpv/podsync/pkg/model"
"github.com/pkg/errors"
"github.com/ventu-io/go-shortid"
)
const (
maxPageSize = 150
)
type idService interface {
Generate(feed *model.Feed) (string, error)
}
type storageService interface {
CreateFeed(feed *model.Feed) error
GetFeed(hashId string) (*model.Feed, error)
@@ -27,13 +24,13 @@ type builder interface {
Build(feed *model.Feed) (podcast *itunes.Podcast, err error)
}
type service struct {
id idService
type Service struct {
sid *shortid.Shortid
storage storageService
builders map[api.Provider]builder
}
func (s *service) CreateFeed(req *api.CreateFeedRequest, identity *api.Identity) (string, error) {
func (s Service) CreateFeed(req *api.CreateFeedRequest, identity *api.Identity) (string, error) {
feed, err := parseURL(req.URL)
if err != nil {
return "", errors.Wrapf(err, "failed to create feed for URL: %s", req.URL)
@@ -64,7 +61,7 @@ func (s *service) CreateFeed(req *api.CreateFeedRequest, identity *api.Identity)
}
// Generate short id
hashId, err := s.id.Generate(feed)
hashId, err := s.sid.Generate()
if err != nil {
return "", errors.Wrap(err, "failed to generate id for feed")
}
@@ -79,7 +76,7 @@ func (s *service) CreateFeed(req *api.CreateFeedRequest, identity *api.Identity)
return hashId, nil
}
func (s *service) GetFeed(hashId string) (*itunes.Podcast, error) {
func (s Service) GetFeed(hashId string) (*itunes.Podcast, error) {
feed, err := s.storage.GetFeed(hashId)
if err != nil {
return nil, err
@@ -93,7 +90,7 @@ func (s *service) GetFeed(hashId string) (*itunes.Podcast, error) {
return builder.Build(feed)
}
func (s *service) GetMetadata(hashId string) (*api.Metadata, error) {
func (s Service) GetMetadata(hashId string) (*api.Metadata, error) {
feed, err := s.storage.GetFeed(hashId)
if err != nil {
return nil, err
@@ -106,36 +103,36 @@ func (s *service) GetMetadata(hashId string) (*api.Metadata, error) {
}, nil
}
type feedOption func(*service)
type feedOption func(*Service)
//noinspection GoExportedFuncWithUnexportedType
func WithStorage(storage storageService) feedOption {
return func(service *service) {
return func(service *Service) {
service.storage = storage
}
}
//noinspection GoExportedFuncWithUnexportedType
func WithIdGen(id idService) feedOption {
return func(service *service) {
service.id = id
}
}
//noinspection GoExportedFuncWithUnexportedType
func WithBuilder(provider api.Provider, builder builder) feedOption {
return func(service *service) {
return func(service *Service) {
service.builders[provider] = builder
}
}
func NewFeedService(opts ...feedOption) *service {
svc := &service{}
svc.builders = make(map[api.Provider]builder)
func NewFeedService(opts ...feedOption) (*Service, error) {
sid, err := shortid.New(1, shortid.DefaultABC, uint64(time.Now().UnixNano()))
if err != nil {
return nil, err
}
svc := &Service{
sid: sid,
builders: make(map[api.Provider]builder),
}
for _, fn := range opts {
fn(svc)
}
return svc
return svc, nil
}
-36
View File
@@ -10,42 +10,6 @@ import (
reflect "reflect"
)
// MockidService is a mock of idService interface
type MockidService struct {
ctrl *gomock.Controller
recorder *MockidServiceMockRecorder
}
// MockidServiceMockRecorder is the mock recorder for MockidService
type MockidServiceMockRecorder struct {
mock *MockidService
}
// NewMockidService creates a new mock instance
func NewMockidService(ctrl *gomock.Controller) *MockidService {
mock := &MockidService{ctrl: ctrl}
mock.recorder = &MockidServiceMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (_m *MockidService) EXPECT() *MockidServiceMockRecorder {
return _m.recorder
}
// Generate mocks base method
func (_m *MockidService) Generate(feed *model.Feed) (string, error) {
ret := _m.ctrl.Call(_m, "Generate", feed)
ret0, _ := ret[0].(string)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Generate indicates an expected call of Generate
func (_mr *MockidServiceMockRecorder) Generate(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCallWithMethodType(_mr.mock, "Generate", reflect.TypeOf((*MockidService)(nil).Generate), arg0)
}
// MockstorageService is a mock of storageService interface
type MockstorageService struct {
ctrl *gomock.Controller
+6 -8
View File
@@ -9,20 +9,18 @@ import (
"github.com/mxpv/podsync/pkg/api"
"github.com/mxpv/podsync/pkg/model"
"github.com/stretchr/testify/require"
"github.com/ventu-io/go-shortid"
)
func TestService_CreateFeed(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
id := NewMockidService(ctrl)
id.EXPECT().Generate(gomock.Any()).Times(1).Return("123", nil)
storage := NewMockstorageService(ctrl)
storage.EXPECT().CreateFeed(gomock.Any()).Times(1).Return(nil)
s := service{
id: id,
s := Service{
sid: shortid.GetDefault(),
storage: storage,
builders: map[api.Provider]builder{api.ProviderYoutube: nil},
}
@@ -36,7 +34,7 @@ func TestService_CreateFeed(t *testing.T) {
hashId, err := s.CreateFeed(req, &api.Identity{})
require.NoError(t, err)
require.Equal(t, "123", hashId)
require.NotEmpty(t, hashId)
}
func TestService_GetFeed(t *testing.T) {
@@ -51,7 +49,7 @@ func TestService_GetFeed(t *testing.T) {
bld := NewMockbuilder(ctrl)
bld.EXPECT().Build(feed).Return(nil, nil)
s := service{
s := Service{
storage: storage,
builders: map[api.Provider]builder{api.ProviderYoutube: bld},
}
@@ -67,7 +65,7 @@ func TestService_GetMetadata(t *testing.T) {
storage := NewMockstorageService(ctrl)
storage.EXPECT().GetFeed("123").Times(1).Return(&model.Feed{}, nil)
s := service{storage: storage}
s := Service{storage: storage}
_, err := s.GetMetadata("123")
require.NoError(t, err)
}
-32
View File
@@ -1,32 +0,0 @@
package id
import (
"hash/fnv"
"time"
"github.com/mxpv/podsync/pkg/model"
"github.com/ventu-io/go-shortid"
)
type hashId struct {
sid *shortid.Shortid
}
func hashString(s string) int {
h := fnv.New32a()
h.Write([]byte(s))
return int(h.Sum32())
}
func (h *hashId) Generate(feed *model.Feed) (string, error) {
return h.sid.Generate()
}
func NewIdGenerator() (*hashId, error) {
sid, err := shortid.New(1, shortid.DefaultABC, uint64(time.Now().UnixNano()))
if err != nil {
return nil, err
}
return &hashId{sid}, nil
}
-19
View File
@@ -1,19 +0,0 @@
package id
import (
"testing"
"github.com/mxpv/podsync/pkg/model"
"github.com/stretchr/testify/require"
)
func TestEncode(t *testing.T) {
hid, err := NewIdGenerator()
require.NoError(t, err)
feed := &model.Feed{}
hash, err := hid.Generate(feed)
require.NoError(t, err)
require.NotEmpty(t, hash)
}