Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Generator interface #61

Closed
wants to merge 4 commits into from
Closed

Conversation

klnusbaum
Copy link

@klnusbaum klnusbaum commented Oct 20, 2017

In certain cases (namely testing) a person may want to deterministically generate UUIDs. There currently aren't many facilities in this UUID library for doing this.

I propose introducing a new interface called Generator. A Generator has all of the standard uuid creation functions on it, but has the advantage of being able to be mocked. Developers wishing to write more testable code can write code that instead of directly using the creation function, instead uses some instance of the Generator interface. For the code they actually want to run, they can inject the Generator returned by the NewGenerator function. When they want to test, they can inject a mocked Generator of their choice (using mockgen, mockery, or their own custom implementation) and deterministically control what UUIDs are generated.

For instance, consider the following generator that might be used for testing. In this case, the author only cares about deterministically generating a certain sequence of V4 UUIDS.

type MyTestGen struct {
  Sequence []satori.UUID
  Iter            int
}

func (_ MyTestGen) NewV1() satori.UUID{return  satori.Nil}
func (_ MyTestGen) NewV2(domain byte) satori.UUID{return  satori.Nil}
func (_ MyTestGen) NewV3(ns UUID, name string) satori.UUID{return  satori.Nil}
func (m MyTestGen) NewV4() satori.UUID{
  toReturn := m.Sequence[m.Iter]
  m.Iter += 1
  return toReturn
}
func (_ MyTestGen) NewV5(ns UUID, name string) satori.UUID{return  satori.Nil}

@coveralls
Copy link

coveralls commented Oct 20, 2017

Coverage Status

Coverage increased (+0.1%) to 96.667% when pulling 9a63855 on klnusbaum:generator into 5bf94b6 on satori:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 96.667% when pulling c459c6c on klnusbaum:generator into 5bf94b6 on satori:master.

1 similar comment
@coveralls
Copy link

coveralls commented Oct 20, 2017

Coverage Status

Coverage increased (+0.1%) to 96.667% when pulling c459c6c on klnusbaum:generator into 5bf94b6 on satori:master.

@coveralls
Copy link

coveralls commented Oct 20, 2017

Coverage Status

Coverage increased (+0.1%) to 96.667% when pulling db9933c on klnusbaum:generator into 5bf94b6 on satori:master.

generator.go Outdated
// version 2 (as specified in DCE 1.1).
package uuid

// Generator can create any type od UUID.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: of

generator.go Outdated
// Generator can create any type od UUID.
type Generator interface {
NewV1() UUID
NewV2(domaing byte) UUID
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: domain

@coveralls
Copy link

coveralls commented Oct 21, 2017

Coverage Status

Coverage increased (+0.1%) to 96.667% when pulling 2b4c18d on klnusbaum:generator into 5bf94b6 on satori:master.

@satori
Copy link
Owner

satori commented Jan 2, 2018

This PR was superseded by #62

@satori satori closed this Jan 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants