update interface

This commit is contained in:
Asim Aslam 2019-06-06 11:46:13 +01:00
parent deee4dcd6a
commit d59b693fa6
3 changed files with 33 additions and 36 deletions

View File

@ -1,44 +1,41 @@
package init package init
type defaultInit struct { type defaultOptions struct {
opts *Options opts *Values
} }
type stringKey struct{} type stringKey struct{}
func (i *defaultInit) Init(opts ...Option) error { func (d *defaultOptions) Init(opts ...Option) error {
if i.opts == nil { if d.opts == nil {
i.opts = new(Options) d.opts = new(Values)
} }
for _, o := range opts { for _, o := range opts {
if err := i.opts.SetOption(o); err != nil { if err := d.opts.Option(o); err != nil {
return err return err
} }
} }
return nil return nil
} }
func (i *defaultInit) Options() *Options { func (d *defaultOptions) Options() Options {
if i.opts == nil { return d
i.opts = new(Options)
}
return i.opts
} }
func (i *defaultInit) Value(k interface{}) (interface{}, bool) { func (d *defaultOptions) Value(k interface{}) (interface{}, bool) {
if i.opts == nil { if d.opts == nil {
i.opts = new(Options) d.opts = new(Values)
} }
return i.opts.Value(k) return d.opts.Get(k)
} }
func (i *defaultInit) String() string { func (d *defaultOptions) String() string {
if i.opts == nil { if d.opts == nil {
i.opts = new(Options) d.opts = new(Values)
} }
n, ok := i.opts.Value(stringKey{}) n, ok := d.opts.Get(stringKey{})
if ok { if ok {
return n.(string) return n.(string)
} }
return "defaultInit" return "Values"
} }

View File

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

View File

@ -4,17 +4,17 @@ import (
"sync" "sync"
) )
// Options holds the set of option values and protects them // Values holds the set of option values and protects them
type Options struct { type Values struct {
sync.RWMutex sync.RWMutex
values map[interface{}]interface{} values map[interface{}]interface{}
} }
// Option gives access to options // Option gives access to options
type Option func(o *Options) error type Option func(o *Values) error
// Get a value from options // Get a value from options
func (o *Options) Value(k interface{}) (interface{}, bool) { func (o *Values) Get(k interface{}) (interface{}, bool) {
o.RLock() o.RLock()
defer o.RUnlock() defer o.RUnlock()
v, ok := o.values[k] v, ok := o.values[k]
@ -22,7 +22,7 @@ func (o *Options) Value(k interface{}) (interface{}, bool) {
} }
// Set a value in the options // Set a value in the options
func (o *Options) SetValue(k, v interface{}) error { func (o *Values) Set(k, v interface{}) error {
o.Lock() o.Lock()
defer o.Unlock() defer o.Unlock()
if o.values == nil { if o.values == nil {
@ -33,14 +33,14 @@ func (o *Options) SetValue(k, v interface{}) error {
} }
// SetOption executes an option // SetOption executes an option
func (o *Options) SetOption(op Option) error { func (o *Values) Option(op Option) error {
return op(o) return op(o)
} }
// WithValue allows you to set any value within the options // WithValue allows you to set any value within the options
func WithValue(k, v interface{}) Option { func WithValue(k, v interface{}) Option {
return func(o *Options) error { return func(o *Values) error {
return o.SetValue(k, v) return o.Set(k, v)
} }
} }