ID generator returns a pointer to platform.ID now

pull/10616/head
Leonardo Di Donato 2018-07-25 13:02:05 +02:00 committed by Leonardo Di Donato
parent a5465f6416
commit de66503de8
2 changed files with 10 additions and 3 deletions

View File

@ -12,10 +12,12 @@ func init() {
rand.Seed(time.Now().UnixNano())
}
// IDGenerator holds the ID generator.
type IDGenerator struct {
Generator *snowflake.Generator
}
// NewIDGenerator creates a new ID generator.
func NewIDGenerator() *IDGenerator {
return &IDGenerator{
// Maximum machine id is 1023
@ -23,6 +25,8 @@ func NewIDGenerator() *IDGenerator {
}
}
func (g *IDGenerator) ID() platform.ID {
return platform.ID(g.Generator.Next())
// ID returns a pointer to a generated ID.
func (g *IDGenerator) ID() *platform.ID {
id := platform.ID(g.Generator.Next())
return &id
}

View File

@ -10,7 +10,10 @@ import (
func TestIDLength(t *testing.T) {
gen := NewIDGenerator()
id := gen.ID()
if len(id.Encode()) != 16 {
if id == nil {
t.Fail()
}
if len(id.Encode()) != platform.IDLength {
t.Fail()
}
}