52 lines
762 B
Go
52 lines
762 B
Go
package identity
|
|
|
|
import (
|
|
"github.com/bwmarrin/snowflake"
|
|
"github.com/google/uuid"
|
|
"github.com/rs/xid"
|
|
)
|
|
|
|
type Engine interface {
|
|
NextID() string
|
|
}
|
|
|
|
type (
|
|
XidEngine struct {
|
|
}
|
|
|
|
UUIDEngine struct {
|
|
}
|
|
|
|
SnowflakeEngine struct {
|
|
e *snowflake.Node
|
|
}
|
|
)
|
|
|
|
func (e *XidEngine) NextID() string {
|
|
return xid.New().String()
|
|
}
|
|
|
|
func (e *UUIDEngine) NextID() string {
|
|
return uuid.New().String()
|
|
}
|
|
|
|
func (e *SnowflakeEngine) NextID() string {
|
|
return e.e.Generate().String()
|
|
}
|
|
|
|
func NewXidEngine() *XidEngine {
|
|
return &XidEngine{}
|
|
}
|
|
|
|
func NewUUIDEngine() *UUIDEngine {
|
|
return &UUIDEngine{}
|
|
}
|
|
|
|
func NewSnowflakeEngine(nodeID int64) *SnowflakeEngine {
|
|
node, err := snowflake.NewNode(nodeID)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &SnowflakeEngine{e: node}
|
|
}
|