options
Use Case
You have a constructor-type func which accepts optional funcs as parameters. Rob Pike and Dave Cheney have discussed this in 2014 and 2016, respectively.
func SpecificThingOptionFunc func(st *SpecificThing) error
func SetHost(host string) error {
return func(st *SpecificThing) error {
st.host = host
return nil
}
}
type SpecificThing struct {
host string
}
func NewSpecificThing(options ...SpecificThingOptionFunc) *SpecificThing {
st := &SpecificThing{}
for o := range options {
o(st)
}
return st
}
func main() {
st := NewSpecificThing(SetHost("example.com"))
// ...
}
If a program has many specific things, it may have some common things, using struct cmoposition.
type CommonThing struct {
loggingLevel int
}
type SpecificThing struct {
CommonThing
host string
}
Continue the options func pattern, CommonThing
can also have option funcs.
func CommonThingOptionFunc func(ct *CommonThing) error
func SetLoggingLevel(loggingLevel int) error {
return func(ct *CommonThing) error {
ct.loggingLevel = loggingLevel
return nil
}
}
The problem comes when the SpecificThing
constructor wants to apply option to its CommontThing
. There are a couple of common patterns that can address this.
Multiple sets of options
A constructor may accept arrays of options of different types by dropping the vardiac syntax:
func NewSpecificThing(ct CommonThing, ctOptions []CommonThingOptionFunc, stOptions []SpecificThingOptionFunc) {
ct := CommonThing {}
for o := range ctOptions {
o(ct)
}
st := &SpecificThing {
CommonThing: ct,
}
for o := range stOptions {
o(st)
}
return st
}
This rapidly becomes inellegant and unweildy as the number of embedded structures with options grows. For example, if CommonThing
also embeds another struct, and that struct embeds one, etc., the argument list rapidly grows out of control.
Dependency Injection
A constructor may require that all of its constituant parts may already exist at the time of construction:
func NewSpecificThing(ct CommonThing, options ...SpecificThingOptionFunc) {
st := &SpecificThing {
CommonThing: ct,
}
for o := range options {
o(st)
}
return st
}
This pattern should scale more elegantly. However, this looks a little like dependency injection, which is not appropriate for every project.
Motivation
The goals of this project are to allow a single options func to be applicable across many embedded structs.
Usage
options github.com/object88/hoarding:Options,Suboptions github.com/object88/hoarding/internal:Options