Support zerovalue and fix tests

Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
This commit is contained in:
Peng Gao 2016-08-27 19:35:01 +08:00
parent 987311e3b4
commit 5b06290188
3 changed files with 84 additions and 14 deletions

View File

@ -198,21 +198,22 @@ func TestFlush(t *testing.T) {
} }
func TestOnEvicted(t *testing.T) { func TestOnEvicted(t *testing.T) {
tc := New_tpl(Attr_tpl{ works := false
var tc *Cache_tpl
tc = New_tpl(Attr_tpl{
DefaultExpiration: DefaultExpiration, DefaultExpiration: DefaultExpiration,
DefaultCleanupInterval: 0, DefaultCleanupInterval: 0,
OnEvicted: func(k string, v ValueType_tpl) {
if k == "foo" && v == 3 {
works = true
}
tc.Set("bar", 4, DefaultExpiration)
},
}) })
tc.Set("foo", 3, DefaultExpiration) tc.Set("foo", 3, DefaultExpiration)
if tc.onEvicted != nil { if tc.onEvicted == nil {
t.Fatal("tc.onEvicted is not nil") t.Fatal("tc.onEvicted is nil")
} }
works := false
tc.OnEvicted(func(k string, v ValueType_tpl) {
if k == "foo" && v == 3 {
works = true
}
tc.Set("bar", 4, DefaultExpiration)
})
tc.Delete("foo") tc.Delete("foo")
x, _ := tc.Get("bar") x, _ := tc.Get("bar")
if !works { if !works {

View File

@ -182,8 +182,7 @@ func (c *cache) delete(k string) ({{.ValueType}}, bool) {
delete(c.items, k) delete(c.items, k)
return v.Object, true return v.Object, true
} }
//TODO: zeroValue return {{ .ZeroValue }}, false
return 0, false
} }
func (c *cache) deleteFast(k string) { func (c *cache) deleteFast(k string) {

View File

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"go/ast"
"go/parser" "go/parser"
"go/token" "go/token"
"os" "os"
@ -25,6 +26,61 @@ func packageDir() string {
return path.Dir(filename) return path.Dir(filename)
} }
// find value of ident 'grammar' in GenDecl.
func findInGenDecl(genDecl *ast.GenDecl, grammarName string) string {
for _, spec := range genDecl.Specs {
valueSpec, ok := spec.(*ast.TypeSpec)
if ok {
// type ident
ident, ok := valueSpec.Type.(*ast.Ident)
if ok {
return ident.Name
}
}
}
return ""
}
func findInDecl(decl ast.Decl, grammarName string) string {
genDecl, ok := decl.(*ast.GenDecl)
if ok {
g := findInGenDecl(genDecl, grammarName)
if g != "" {
return g
}
}
return ""
}
// zeroValue returns literal zero value.
func zeroValue(s string) string {
// TODO: support func type.
switch s {
case "string":
return "\"\""
case "int", "uint", "int64", "uint64", "uint32", "int32", "int16",
"uint16", "int8", "uint8", "byte", "rune", "float64", "float32",
"complex64", "complex32", "uintptr":
return "0"
case "slice":
return "nil"
default:
if s[0] == '*' { // Pointer
return "nil"
}
return s + "{}"
}
}
// TODO: support more builtin types
func builtin(s string) bool {
switch s {
case "string":
return true
}
return false
}
func main() { func main() {
keyType := flag.String("k", "", "key type") keyType := flag.String("k", "", "key type")
valueType := flag.String("v", "", "value type") valueType := flag.String("v", "", "value type")
@ -40,10 +96,23 @@ func main() {
if err != nil { if err != nil {
fatal(err) fatal(err)
} }
var packageName string packageName := "main"
for name := range pkgs { typeName := ""
for name, pkg := range pkgs {
packageName = name packageName = name
for _, f := range pkg.Files {
for _, decl := range f.Decls {
typeName = findInDecl(decl, *valueType)
}
}
} }
if typeName == "" && !builtin(*valueType) {
fatal(fmt.Errorf("found no definition of %s in files\n", *valueType))
}
if typeName == "" {
typeName = *valueType
}
zeroTypeValue := zeroValue(typeName)
f, err := os.OpenFile(fmt.Sprintf("%s2%s_cachemap.go", *keyType, *valueType), os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644) f, err := os.OpenFile(fmt.Sprintf("%s2%s_cachemap.go", *keyType, *valueType), os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil { if err != nil {
fatal(err) fatal(err)
@ -59,6 +128,7 @@ func main() {
"ValueType": *valueType, "ValueType": *valueType,
"PackageName": packageName, "PackageName": packageName,
"Cache": fmt.Sprintf("String2%sCache", *valueType), "Cache": fmt.Sprintf("String2%sCache", *valueType),
"ZeroValue": zeroTypeValue,
}, },
) )
if err != nil { if err != nil {