mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Implement human id generator
This commit is contained in:
33
web/pkg/id/hashids.go
Normal file
33
web/pkg/id/hashids.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package id
|
||||
|
||||
import (
|
||||
hd "github.com/speps/go-hashids"
|
||||
)
|
||||
|
||||
const (
|
||||
minLength = 4
|
||||
salt = "mVJIX8cDWQJ71oMw6xw9yYV9TA1rojDcKrhUaOqEfaE"
|
||||
alphabet = "abcdefghijklmnopqrstuvwxyz1234567890"
|
||||
)
|
||||
|
||||
type hashId struct {
|
||||
hid *hd.HashID
|
||||
}
|
||||
|
||||
func (h *hashId) Encode(x ...int) (string, error) {
|
||||
var d []int
|
||||
return h.hid.Encode(append(d, x...))
|
||||
}
|
||||
|
||||
func NewIdGenerator() (*hashId, error) {
|
||||
data := hd.NewData()
|
||||
data.MinLength = minLength
|
||||
data.Salt = salt
|
||||
data.Alphabet = alphabet
|
||||
hid, err := hd.NewWithData(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &hashId{hid}, nil
|
||||
}
|
15
web/pkg/id/hashids_test.go
Normal file
15
web/pkg/id/hashids_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package id
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
hid, err := NewIdGenerator()
|
||||
require.NoError(t, err)
|
||||
|
||||
hash, err := hid.Encode(1)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, hash)
|
||||
}
|
Reference in New Issue
Block a user