From 20c89c6e56a6135e1e0bbe143536a3b3515c9e9d Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 30 Aug 2018 09:23:21 -0400 Subject: [PATCH] Functional options to persist.Init(). --- generate/template.go | 4 +-- persist.go | 75 +++++++++++++++++++++++++------------------- test/tidy/main.go | 9 +++--- 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/generate/template.go b/generate/template.go index a8f1173..17101a8 100644 --- a/generate/template.go +++ b/generate/template.go @@ -12,9 +12,9 @@ import ( type Var_N persist.Var -func New(name string, x _T, opt ...persist.Option) *Var_N { +func New(name string, x _T, ps ...bool) *Var_N { gob.Register(x) - ptr := persist.New(name, x, opt...) + ptr := persist.New(name, x, ps...) ret := (*Var_N)(unsafe.Pointer(ptr)) return ret } diff --git a/persist.go b/persist.go index f1d8ef4..cbea4a2 100644 --- a/persist.go +++ b/persist.go @@ -25,16 +25,14 @@ type Var struct { X interface{} Time time.Time name string - opt Option + permanent bool } type encVar struct { X interface{} } -type Option struct { - Permanent bool -} +type Option func(*Config) type Config struct { configured bool @@ -43,42 +41,47 @@ type Config struct { Tidy TidyConfig } -func Init(x ...Config) { +func MaxVars(i int) Option { + return func(c *Config) { + c.MaxVars = i + } +} + +func DBPath(s string) Option { + return func(c *Config) { + c.DBPath = s + } +} + +func TidyInterval(d time.Duration) Option { + return func(c *Config) { + c.Tidy.Interval = d + } +} + +func Init(confs ...Option) { mux.Lock() - c := Config { + tc := TidyConfig { + Interval: 10 * time.Second, + } + conf = Config { // default config configured: true, - MaxVars: 64, // default config + MaxVars: 64, DBPath: "db", - } - conf = c - if len(x) > 0 { - c = x[0] - } - if c.MaxVars != 0 { - conf.MaxVars = c.MaxVars - } - if c.DBPath != "" { - conf.DBPath = c.DBPath + Tidy: tc, } - if c.Tidy.Interval == 0 { - conf.Tidy.Interval = 10 * time.Second - } else { - fmt.Println("Setting conf.Tidy.Interval to",c.Tidy.Interval) - conf.Tidy.Interval = c.Tidy.Interval + for _,f := range(confs) { + f(&conf) } - if c.Tidy.Func == nil { - conf.Tidy.Func = ExpireAfter(24 * time.Hour) - } else { - fmt.Println("Setting conf.Tidy.Func") - conf.Tidy.Func = c.Tidy.Func + if conf.Tidy.Func == nil { + conf.Tidy.Func = ExpireAfterFunc(24 * time.Hour) } - mux.Unlock() } -func New(name string, x interface{},opt ...Option) *Var { +func New(name string, x interface{},ps ...bool) *Var { start() mux.Lock() if vars == nil { @@ -89,8 +92,8 @@ func New(name string, x interface{},opt ...Option) *Var { ret = &Var{X: x, name: name} vars[name] = ret } - if len(opt) == 1 { - ret.opt = opt[0] + if len(ps) > 1 { + ret.permanent = ps[0] } mux.Unlock() err := ret.Load() @@ -450,7 +453,13 @@ type TidyConfig struct { Func TidyFunc } -func ExpireAfter(exp time.Duration) TidyFunc { +func ExpireAfter(exp time.Duration) Option { + return func(c *Config) { + c.Tidy.Func = ExpireAfterFunc(exp) + } +} + +func ExpireAfterFunc(exp time.Duration) TidyFunc { if exp > 0 { exp = -1 * exp } @@ -485,7 +494,7 @@ func tidyThread() { } for { for p := range loaded { - if p.opt.Permanent == false { + if p.permanent == false { lchan<- p.name loaded<- p // put it back in the queue } diff --git a/test/tidy/main.go b/test/tidy/main.go index 25a6d48..18dfab2 100644 --- a/test/tidy/main.go +++ b/test/tidy/main.go @@ -7,11 +7,10 @@ import ( ) func main() { - tc := persist.TidyConfig{ - Interval: 2 * time.Second, - Func: persist.ExpireAfter(-5 * time.Second), - } - persist.Init(persist.Config{Tidy: tc}) + persist.Init( + persist.TidyInterval(2 * time.Second), + persist.ExpireAfter(-5 * time.Second), + ) x := persistInt("x",0) persist.Commit()