cluade code review changes
This commit is contained in:
36
uid/sqid.go
36
uid/sqid.go
@@ -5,15 +5,45 @@
|
||||
|
||||
package uid
|
||||
|
||||
import "github.com/sqids/sqids-go"
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/sqids/sqids-go"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
SquiOptions() sqids.Options
|
||||
}
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
sqidInst *sqids.Sqids
|
||||
sqidOpts *sqids.Options
|
||||
)
|
||||
|
||||
func getSqids(svc Service) (*sqids.Sqids, error) {
|
||||
opts := svc.SquiOptions()
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if sqidInst != nil && sqidOpts != nil && opts.Alphabet == sqidOpts.Alphabet && opts.MinLength == sqidOpts.MinLength {
|
||||
return sqidInst, nil
|
||||
}
|
||||
|
||||
s, err := sqids.New(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqidInst = s
|
||||
sqidOpts = &opts
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Encode a slice of IDs into one unique ID
|
||||
func Encode(svc Service, ids ...uint64) (string, error) {
|
||||
s, err := sqids.New(svc.SquiOptions())
|
||||
s, err := getSqids(svc)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -23,7 +53,7 @@ func Encode(svc Service, ids ...uint64) (string, error) {
|
||||
|
||||
// Decode an ID back to slice of IDs
|
||||
func Decode(svc Service, id string) ([]uint64, error) {
|
||||
s, err := sqids.New(svc.SquiOptions())
|
||||
s, err := getSqids(svc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user