-
Notifications
You must be signed in to change notification settings - Fork 114
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
Generator options #98
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ import ( | |
// UUID epoch (October 15, 1582) and Unix epoch (January 1, 1970). | ||
const epochStart = 122192928000000000 | ||
|
||
type epochFunc func() time.Time | ||
type EpochFunc func() time.Time | ||
|
||
// HWAddrFunc is the function type used to provide hardware (MAC) addresses. | ||
type HWAddrFunc func() (net.HardwareAddr, error) | ||
|
@@ -126,7 +126,7 @@ type Gen struct { | |
|
||
rand io.Reader | ||
|
||
epochFunc epochFunc | ||
epochFunc EpochFunc | ||
hwAddrFunc HWAddrFunc | ||
lastTime uint64 | ||
clockSequence uint16 | ||
|
@@ -137,13 +137,43 @@ type Gen struct { | |
v7ClockSequence uint16 | ||
} | ||
|
||
type GenOption func(*Gen) | ||
|
||
// interface check -- build will fail if *Gen doesn't satisfy Generator | ||
var _ Generator = (*Gen)(nil) | ||
|
||
// NewGen returns a new instance of Gen with some default values set. Most | ||
// people should use this. | ||
func NewGen() *Gen { | ||
return NewGenWithHWAF(defaultHWAddrFunc) | ||
// NewGen returns a new instance of Gen with default values if no options are | ||
// passed. | ||
func NewGen(opts ...GenOption) *Gen { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As written this would be a breaking change for the module, and so I don't think we can accept this if it changes Secondarily, I really dislike functional options and would prefer we do not adopt them for this use case. They are really hard to discover via documentation, where-as a configuration struct could be a lot more approachable / discoverable and still also support adding new options without it being a breaking change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why it would be a breaking change if you can call it as before
Just to make sure I understand this, you would prefer to have it like this: type GenConfig struct {
EpochFunc EpochFunc
HWAddrFunc HWAddrFunc
RandReader io.Reader
}
func NewGen() *Gen {
return NewGenWithConfig(GenConfig{})
}
func NewGenWithConfig(config GenConfig) *Gen {
if config.EpochFunc == nil {
config.EpochFunc = time.Now
}
if config.HWAddrFunc == nil {
config.HWAddrFunc = defaultHWAddrFunc
}
if config.RandReader == nil {
config.RandReader = rand.Reader
}
return &Gen{
epochFunc: config.EpochFunc,
hwAddrFunc: config.HWAddrFunc,
rand: config.RandReader,
}
} ? I find it more specific when using options, as you just use the one that you want to adjust, with config it seems as if someone has to set all of the properties. |
||
gen := &Gen{ | ||
epochFunc: time.Now, | ||
hwAddrFunc: defaultHWAddrFunc, | ||
rand: rand.Reader, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(gen) | ||
} | ||
|
||
return gen | ||
} | ||
|
||
func WithHWAddrFunc(hwaf HWAddrFunc) GenOption { | ||
return func(gen *Gen) { | ||
gen.hwAddrFunc = hwaf | ||
} | ||
} | ||
|
||
func WithEpochFunc(epochf EpochFunc) GenOption { | ||
return func(gen *Gen) { | ||
gen.epochFunc = epochf | ||
} | ||
} | ||
|
||
func WithRandomReader(reader io.Reader) GenOption { | ||
return func(gen *Gen) { | ||
gen.rand = reader | ||
} | ||
} | ||
|
||
// NewGenWithHWAF builds a new UUID generator with the HWAddrFunc provided. Most | ||
|
@@ -158,11 +188,7 @@ func NewGen() *Gen { | |
// MAC address being used, you'll need to create a new generator using this | ||
// function. | ||
func NewGenWithHWAF(hwaf HWAddrFunc) *Gen { | ||
return &Gen{ | ||
epochFunc: time.Now, | ||
hwAddrFunc: hwaf, | ||
rand: rand.Reader, | ||
} | ||
return NewGen(WithHWAddrFunc(hwaf)) | ||
} | ||
|
||
// NewV1 returns a UUID based on the current timestamp and MAC address. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that this is being exported, could you please add a GoDoc-compatible comment that explains what this is:
// EpochFunc is ...