Skip to content

Commit

Permalink
Add Generator interface
Browse files Browse the repository at this point in the history
  • Loading branch information
klnusbaum committed Oct 20, 2017
1 parent 5bf94b6 commit 9a63855
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
21 changes: 21 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uuid

type Generator interface {
NewV1() UUID
NewV2(domaing byte) UUID
NewV3(ns UUID, name string) UUID
NewV4() UUID
NewV5(ns UUID, name string) UUID
}

type standard struct{}

func (_ standard) NewV1() UUID { return NewV1() }
func (_ standard) NewV2(domain byte) UUID { return NewV2(domain) }
func (_ standard) NewV3(ns UUID, name string) UUID { return NewV3(ns, name) }
func (_ standard) NewV4() UUID { return NewV4() }
func (_ standard) NewV5(ns UUID, name string) UUID { return NewV5(ns, name) }

func NewGenerator() Generator {
return standard{}
}
39 changes: 39 additions & 0 deletions generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uuid

import (
"testing"
)

func TestNewGenerator(t *testing.T) {
generator := NewGenerator()

u1 := generator.NewV1()
if u1.Version() != 1 {
t.Errorf("UUIDv1 generated with incorrect version: %d", u1.Version())
}

u2 := generator.NewV2(DomainPerson)

if u2.Version() != 2 {
t.Errorf("UUIDv2 generated with incorrect version: %d", u2.Version())
}

u3 := generator.NewV3(NamespaceDNS, "www.example.com")

if u3.Version() != 3 {
t.Errorf("UUIDv3 generated with incorrect version: %d", u3.Version())
}

u4 := generator.NewV4()

if u4.Version() != 4 {
t.Errorf("UUIDv4 generated with incorrect version: %d", u4.Version())
}

u5 := generator.NewV5(NamespaceDNS, "www.example.com")

if u5.Version() != 5 {
t.Errorf("UUIDv3 generated with incorrect version: %d", u5.Version())
}

}

0 comments on commit 9a63855

Please sign in to comment.