move init to options

This commit is contained in:
Asim Aslam 2019-06-06 17:54:30 +01:00
parent c60b5a45bb
commit 64459c54a1
3 changed files with 21 additions and 23 deletions

View File

@ -1,21 +0,0 @@
// Package init is an interface for initialising options
package init
// Options is used for initialisation
type Options interface {
// Initialise options
Init(...Option) error
// Options returns the current options
Options() Options
// Value returns an option value
Value(k interface{}) (interface{}, bool)
// The name for who these options exist
String() string
}
// NewOptions returns a new initialiser
func NewOptions(opts ...Option) Options {
o := new(defaultOptions)
o.Init(opts...)
return o
}

View File

@ -1,4 +1,4 @@
package init package options
type defaultOptions struct { type defaultOptions struct {
opts *Values opts *Values

View File

@ -1,9 +1,21 @@
package init package options
import ( import (
"sync" "sync"
) )
// Options is used for initialisation
type Options interface {
// Initialise options
Init(...Option) error
// Options returns the current options
Options() Options
// Value returns an option value
Value(k interface{}) (interface{}, bool)
// The name for who these options exist
String() string
}
// Values holds the set of option values and protects them // Values holds the set of option values and protects them
type Values struct { type Values struct {
sync.RWMutex sync.RWMutex
@ -53,3 +65,10 @@ func WithOption(o Option) Option {
func String(s string) Option { func String(s string) Option {
return WithValue(stringKey{}, s) return WithValue(stringKey{}, s)
} }
// NewOptions returns a new initialiser
func NewOptions(opts ...Option) Options {
o := new(defaultOptions)
o.Init(opts...)
return o
}