2025-06-16 22:19:00 +05:30
|
|
|
// Copyright 2024 Patial Tech (Ankit Patial).
|
2025-06-16 22:26:47 +05:30
|
|
|
//
|
|
|
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
|
|
|
// See http://opensource.org/licenses/MIT
|
2025-06-16 22:19:00 +05:30
|
|
|
|
|
|
|
package uid
|
|
|
|
|
|
|
|
import "github.com/sqids/sqids-go"
|
|
|
|
|
|
|
|
type Service interface {
|
|
|
|
SquiOptions() sqids.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode a slice of IDs into one unique ID
|
|
|
|
func Encode(svc Service, ids ...uint64) (string, error) {
|
|
|
|
s, err := sqids.New(svc.SquiOptions())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Encode(ids) // "86Rf07"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode an ID back to slice of IDs
|
|
|
|
func Decode(svc Service, id string) ([]uint64, error) {
|
|
|
|
s, err := sqids.New(svc.SquiOptions())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Decode(id), nil
|
|
|
|
}
|