2024-11-18 15:23:13 +00:00
|
|
|
// Copyright 2024 Patial Tech. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2024-11-17 16:58:29 +00:00
|
|
|
package uid
|
|
|
|
|
|
|
|
import "github.com/sqids/sqids-go"
|
|
|
|
|
|
|
|
// use your own random version of alphabets
|
|
|
|
// there is online util at the bottomt of this page: https://sqids.org/go
|
|
|
|
var opts sqids.Options = sqids.Options{
|
|
|
|
Alphabet: "fsvjrnGWiTk2Lt1l5MzEhFH73CIg60ByexQPYpX9Ro8ZKawAVUdNuDJbmqScO4",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode a slice of IDs into one unique ID
|
|
|
|
func Encode(ids []uint64) (string, error) {
|
|
|
|
s, err := sqids.New()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Encode(ids) // "86Rf07"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode an ID back to slice of IDs
|
|
|
|
func Decode(id string) ([]uint64, error) {
|
|
|
|
s, err := sqids.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Decode(id), nil
|
|
|
|
}
|