rano/util/uid/sqid.go
2024-11-18 20:53:13 +05:30

34 lines
800 B
Go

// 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.
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
}