From ee4f005b6cfe99af58d36cb369794223b793be3c Mon Sep 17 00:00:00 2001 From: Greg Date: Sat, 4 Aug 2018 16:03:00 -0400 Subject: [PATCH] Allow users to pass options to persist.New(). Support for bools. --- cmd/pgen/main.go | 6 +++++- cmd/pgen/template.go | 8 ++------ test/basic/main.go | 2 ++ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/pgen/main.go b/cmd/pgen/main.go index 6777c7f..7b96dd6 100644 --- a/cmd/pgen/main.go +++ b/cmd/pgen/main.go @@ -23,7 +23,7 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor { if !reg.MatchString(id.Name) { return v } - if len(x.Args) != 2 { + if len(x.Args) < 2 { log.Fatal("Wrong number of arguments in persistT call.") } var tp string @@ -36,6 +36,10 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor { case *ast.CompositeLit: tp = types.TypeString(info.TypeOf(arg),types.RelativeTo(pkg)) case *ast.Ident: + if arg.Name == "true" || arg.Name == "false" { + tp = "bool" + break + } if arg.Obj.Kind != ast.Var { return v } inner := pkg.Scope().Innermost(arg.Pos()) _,obj := inner.LookupParent(arg.Obj.Name,arg.Pos()) diff --git a/cmd/pgen/template.go b/cmd/pgen/template.go index 27ac21f..048cac0 100644 --- a/cmd/pgen/template.go +++ b/cmd/pgen/template.go @@ -10,13 +10,9 @@ import ( type Var_N persist.Var -func New(name string, xs ..._T) *Var_N { - var x _T +func New(name string, x _T, opt ...persist.Option) *Var_N { gob.Register(x) - if len(xs) > 0 { - x = xs[0] - } - ptr := persist.New(name, x) + ptr := persist.New(name, x, opt...) ret := (*Var_N)(unsafe.Pointer(ptr)) return ret } diff --git a/test/basic/main.go b/test/basic/main.go index 4550890..de32520 100644 --- a/test/basic/main.go +++ b/test/basic/main.go @@ -36,6 +36,8 @@ func main() { y6 = y6p.Get() y7p := persistIntArray("y7",[]int{1,2,3}) _ = y7p + y8p := persistBool("y8",true) + _ = y8p z := func(interface{}) { _ = persistFloat("y7",1.0)