From d59b693fa63024a3660c1ed59d7265657323d933 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Thu, 6 Jun 2019 11:46:13 +0100 Subject: [PATCH] update interface --- init/default.go | 37 +++++++++++++++++-------------------- init/init.go | 16 ++++++++-------- init/options.go | 16 ++++++++-------- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/init/default.go b/init/default.go index fa937af0..97d26e35 100644 --- a/init/default.go +++ b/init/default.go @@ -1,44 +1,41 @@ package init -type defaultInit struct { - opts *Options +type defaultOptions struct { + opts *Values } type stringKey struct{} -func (i *defaultInit) Init(opts ...Option) error { - if i.opts == nil { - i.opts = new(Options) +func (d *defaultOptions) Init(opts ...Option) error { + if d.opts == nil { + d.opts = new(Values) } for _, o := range opts { - if err := i.opts.SetOption(o); err != nil { + if err := d.opts.Option(o); err != nil { return err } } return nil } -func (i *defaultInit) Options() *Options { - if i.opts == nil { - i.opts = new(Options) - } - return i.opts +func (d *defaultOptions) Options() Options { + return d } -func (i *defaultInit) Value(k interface{}) (interface{}, bool) { - if i.opts == nil { - i.opts = new(Options) +func (d *defaultOptions) Value(k interface{}) (interface{}, bool) { + if d.opts == nil { + d.opts = new(Values) } - return i.opts.Value(k) + return d.opts.Get(k) } -func (i *defaultInit) String() string { - if i.opts == nil { - i.opts = new(Options) +func (d *defaultOptions) String() string { + if d.opts == nil { + d.opts = new(Values) } - n, ok := i.opts.Value(stringKey{}) + n, ok := d.opts.Get(stringKey{}) if ok { return n.(string) } - return "defaultInit" + return "Values" } diff --git a/init/init.go b/init/init.go index 3a4e9fd5..a9db1ebc 100644 --- a/init/init.go +++ b/init/init.go @@ -1,21 +1,21 @@ // Package init is an interface for initialising options package init -// Init is used for initialisation -type Init interface { +// Options is used for initialisation +type Options interface { // Initialise options Init(...Option) error // Options returns the current options - Options() *Options + Options() Options // Value returns an option value Value(k interface{}) (interface{}, bool) // The name for who these options exist String() string } -// NewInit returns a new initialiser -func NewInit(opts ...Option) Init { - i := new(defaultInit) - i.Init(opts...) - return i +// NewOptions returns a new initialiser +func NewOptions(opts ...Option) Options { + o := new(defaultOptions) + o.Init(opts...) + return o } diff --git a/init/options.go b/init/options.go index 4cdb9409..ad99a056 100644 --- a/init/options.go +++ b/init/options.go @@ -4,17 +4,17 @@ import ( "sync" ) -// Options holds the set of option values and protects them -type Options struct { +// Values holds the set of option values and protects them +type Values struct { sync.RWMutex values map[interface{}]interface{} } // Option gives access to options -type Option func(o *Options) error +type Option func(o *Values) error // Get a value from options -func (o *Options) Value(k interface{}) (interface{}, bool) { +func (o *Values) Get(k interface{}) (interface{}, bool) { o.RLock() defer o.RUnlock() v, ok := o.values[k] @@ -22,7 +22,7 @@ func (o *Options) Value(k interface{}) (interface{}, bool) { } // Set a value in the options -func (o *Options) SetValue(k, v interface{}) error { +func (o *Values) Set(k, v interface{}) error { o.Lock() defer o.Unlock() if o.values == nil { @@ -33,14 +33,14 @@ func (o *Options) SetValue(k, v interface{}) error { } // SetOption executes an option -func (o *Options) SetOption(op Option) error { +func (o *Values) Option(op Option) error { return op(o) } // WithValue allows you to set any value within the options func WithValue(k, v interface{}) Option { - return func(o *Options) error { - return o.SetValue(k, v) + return func(o *Values) error { + return o.Set(k, v) } }