From 03f16b14769644f0764122fa5129a3d7e3a7143b Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Fri, 20 Oct 2017 19:21:44 -0700 Subject: [PATCH] Rework id generator --- pkg/api/api.go | 1 - pkg/id/hashids.go | 35 +++++++---------------------------- pkg/id/hashids_test.go | 24 +++--------------------- 3 files changed, 10 insertions(+), 50 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 32b0123..4ba658a 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -17,7 +17,6 @@ const ( Vimeo = Provider("vimeo") ) - type LinkType string const ( diff --git a/pkg/id/hashids.go b/pkg/id/hashids.go index 16e6c86..44c1314 100644 --- a/pkg/id/hashids.go +++ b/pkg/id/hashids.go @@ -2,19 +2,14 @@ package id import ( "hash/fnv" + "time" "github.com/mxpv/podsync/pkg/api" - hd "github.com/speps/go-hashids" -) - -const ( - minLength = 4 - salt = "mVJIX8cDWQJ71oMw6xw9yYV9TA1rojDcKrhUaOqEfaE" - alphabet = "abcdefghijklmnopqrstuvwxyz1234567890" + "github.com/ventu-io/go-shortid" ) type hashId struct { - hid *hd.HashID + sid *shortid.Shortid } func hashString(s string) int { @@ -24,30 +19,14 @@ func hashString(s string) int { } func (h *hashId) Generate(feed *api.Feed) (string, error) { - // Don't create duplicate urls for same playlist/settings - // https://github.com/podsync/issues/issues/6 - numbers := []int{ - hashString(feed.UserId), - hashString(string(feed.Provider)), - hashString(string(feed.LinkType)), - hashString(feed.ItemId), - feed.PageSize, - hashString(string(feed.Quality)), - hashString(string(feed.Format)), - feed.FeatureLevel, - } - - return h.hid.Encode(numbers) + return h.sid.Generate() } func NewIdGenerator() (*hashId, error) { - data := hd.NewData() - data.MinLength = minLength - data.Salt = salt - data.Alphabet = alphabet - hid, err := hd.NewWithData(data) + sid, err := shortid.New(1, shortid.DefaultABC, uint64(time.Now().UnixNano())) if err != nil { return nil, err } - return &hashId{hid}, nil + + return &hashId{sid}, nil } diff --git a/pkg/id/hashids_test.go b/pkg/id/hashids_test.go index b702a92..e06e8fd 100644 --- a/pkg/id/hashids_test.go +++ b/pkg/id/hashids_test.go @@ -11,27 +11,9 @@ func TestEncode(t *testing.T) { hid, err := NewIdGenerator() require.NoError(t, err) - feed := &api.Feed{ - UserId: "1", - Provider: api.Youtube, - LinkType: api.Channel, - ItemId: "UC2yTVSttx7lxAOAzx1opjoA", - PageSize: 10, - Quality: api.HighQuality, - Format: api.AudioFormat, - } + feed := &api.Feed{} - hash1, err := hid.Generate(feed) + hash, err := hid.Generate(feed) require.NoError(t, err) - require.NotEmpty(t, hash1) - - // Ensure we have same hash for same feed/parameters - hash2, err := hid.Generate(feed) - require.NoError(t, err) - require.Equal(t, hash1, hash2) - - feed.UserId = "" - hash3, err := hid.Generate(feed) - require.NoError(t, err) - require.NotEqual(t, hash1, hash3) + require.NotEmpty(t, hash) }