2016-08-11 16:42:28 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2016-08-27 19:35:01 +08:00
|
|
|
"go/ast"
|
2016-08-11 16:42:28 +08:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
2016-08-27 18:05:46 +08:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2016-08-11 16:42:28 +08:00
|
|
|
"runtime"
|
2016-08-29 18:58:22 +08:00
|
|
|
"strings"
|
2016-08-27 18:05:46 +08:00
|
|
|
"text/template"
|
2016-08-11 16:42:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func fatal(v ...interface{}) {
|
|
|
|
fmt.Fprintln(os.Stderr, v...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-08-27 18:05:46 +08:00
|
|
|
func packageDir() string {
|
|
|
|
_, filename, _, ok := runtime.Caller(0)
|
|
|
|
if !ok {
|
|
|
|
panic("No caller information")
|
|
|
|
}
|
|
|
|
return path.Dir(filename)
|
|
|
|
}
|
|
|
|
|
2016-08-29 18:58:22 +08:00
|
|
|
// TODO: parse type if type is also not builtin type.
|
|
|
|
// find literal value of type.
|
|
|
|
func findInGenDecl(genDecl *ast.GenDecl, valueName string) string {
|
2016-08-27 19:35:01 +08:00
|
|
|
for _, spec := range genDecl.Specs {
|
|
|
|
valueSpec, ok := spec.(*ast.TypeSpec)
|
|
|
|
if ok {
|
2016-08-29 18:58:22 +08:00
|
|
|
if ok && valueSpec.Name.Name == valueName {
|
|
|
|
indent, ok := valueSpec.Type.(*ast.Ident)
|
|
|
|
if ok {
|
|
|
|
return indent.Name
|
2016-08-31 12:21:05 +08:00
|
|
|
} else {
|
|
|
|
// For other types like StructType
|
|
|
|
return valueName
|
2016-08-29 18:58:22 +08:00
|
|
|
}
|
2016-08-27 19:35:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-08-29 18:58:22 +08:00
|
|
|
func findInDecl(decl ast.Decl, valueName string) string {
|
2016-08-27 19:35:01 +08:00
|
|
|
genDecl, ok := decl.(*ast.GenDecl)
|
|
|
|
if ok {
|
2016-08-29 18:58:22 +08:00
|
|
|
g := findInGenDecl(genDecl, valueName)
|
2016-08-27 19:35:01 +08:00
|
|
|
if g != "" {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// zeroValue returns literal zero value.
|
|
|
|
func zeroValue(s string) string {
|
|
|
|
// TODO: support func type.
|
|
|
|
switch s {
|
2016-08-29 18:58:22 +08:00
|
|
|
case "bool":
|
|
|
|
return "false"
|
2016-08-27 19:35:01 +08:00
|
|
|
case "string":
|
|
|
|
return "\"\""
|
|
|
|
case "int", "uint", "int64", "uint64", "uint32", "int32", "int16",
|
|
|
|
"uint16", "int8", "uint8", "byte", "rune", "float64", "float32",
|
|
|
|
"complex64", "complex32", "uintptr":
|
|
|
|
return "0"
|
|
|
|
default:
|
2016-08-29 18:58:22 +08:00
|
|
|
if s[0] == '*' || // Pointer
|
|
|
|
strings.Index(s, "map") == 0 || // map
|
|
|
|
strings.Index(s, "chan") == 0 || // chan
|
2016-08-31 12:21:05 +08:00
|
|
|
strings.Index(s, "[]") == 0 || // slice
|
|
|
|
strings.Index(s, "func") == 0 { // func
|
2016-08-27 19:35:01 +08:00
|
|
|
return "nil"
|
|
|
|
}
|
|
|
|
return s + "{}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 18:58:22 +08:00
|
|
|
var builtinTypes = []string{
|
|
|
|
"bool",
|
|
|
|
"string",
|
|
|
|
|
|
|
|
"int", "int8", "int16", "int32", "int64", // numbericType
|
|
|
|
"uint", "uint8", "uint16", "uint32", "uint64", "uintptr",
|
|
|
|
"float32", "float64",
|
|
|
|
"complex64", "complex128",
|
|
|
|
"byte",
|
|
|
|
"rune",
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNumberic(s string) bool {
|
|
|
|
for _, v := range builtinTypes[2:] { // 2 is beginning of numberic types in builtinTypes.
|
|
|
|
if v == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isBuiltin(s string) bool {
|
|
|
|
for _, v := range builtinTypes {
|
|
|
|
if v == s {
|
|
|
|
return true
|
|
|
|
}
|
2016-08-27 19:35:01 +08:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:42:28 +08:00
|
|
|
func main() {
|
|
|
|
keyType := flag.String("k", "", "key type")
|
|
|
|
valueType := flag.String("v", "", "value type")
|
|
|
|
flag.Parse()
|
|
|
|
if *keyType == "" {
|
|
|
|
fatal("key empty")
|
|
|
|
}
|
|
|
|
if *valueType == "" {
|
|
|
|
fatal("value empty")
|
|
|
|
}
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
pkgs, err := parser.ParseDir(fset, ".", nil, parser.ParseComments)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2016-08-27 19:35:01 +08:00
|
|
|
packageName := "main"
|
|
|
|
typeName := ""
|
2016-08-31 12:21:05 +08:00
|
|
|
FIND:
|
2016-08-27 19:35:01 +08:00
|
|
|
for name, pkg := range pkgs {
|
2016-08-11 16:42:28 +08:00
|
|
|
packageName = name
|
2016-08-27 19:35:01 +08:00
|
|
|
for _, f := range pkg.Files {
|
|
|
|
for _, decl := range f.Decls {
|
|
|
|
typeName = findInDecl(decl, *valueType)
|
2016-08-31 12:21:05 +08:00
|
|
|
if typeName != "" {
|
|
|
|
break FIND
|
|
|
|
}
|
2016-08-27 19:35:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 18:58:22 +08:00
|
|
|
if typeName == "" && !isBuiltin(*valueType) {
|
2016-08-27 19:35:01 +08:00
|
|
|
fatal(fmt.Errorf("found no definition of %s in files\n", *valueType))
|
|
|
|
}
|
|
|
|
if typeName == "" {
|
|
|
|
typeName = *valueType
|
2016-08-11 16:42:28 +08:00
|
|
|
}
|
2016-08-27 19:35:01 +08:00
|
|
|
zeroTypeValue := zeroValue(typeName)
|
2016-08-11 16:42:28 +08:00
|
|
|
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 {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2016-08-27 18:05:46 +08:00
|
|
|
tpl, err := template.New("cache.tmpl").ParseFiles(filepath.Join(packageDir(), "cache.tmpl"))
|
2016-08-11 16:42:28 +08:00
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2016-09-01 11:46:59 +08:00
|
|
|
if !isBuiltin(*valueType) {
|
2016-08-29 18:58:22 +08:00
|
|
|
*valueType = strings.Title(*valueType)
|
|
|
|
}
|
2016-08-11 16:42:28 +08:00
|
|
|
err = tpl.Execute(
|
|
|
|
f,
|
2016-08-29 18:58:22 +08:00
|
|
|
map[string]interface{}{
|
2016-08-11 16:42:28 +08:00
|
|
|
"ValueType": *valueType,
|
2016-08-29 18:58:22 +08:00
|
|
|
"RealType": typeName,
|
2016-08-11 16:42:28 +08:00
|
|
|
"PackageName": packageName,
|
|
|
|
"Cache": fmt.Sprintf("String2%sCache", *valueType),
|
2016-08-27 19:35:01 +08:00
|
|
|
"ZeroValue": zeroTypeValue,
|
2016-08-29 18:58:22 +08:00
|
|
|
"IsNumberic": isNumberic,
|
2016-08-11 16:42:28 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2016-09-01 11:46:59 +08:00
|
|
|
constFile, err := os.OpenFile(fmt.Sprintf("cachemap_const.go"), os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
defer constFile.Close()
|
|
|
|
constTpl, err := template.New("const.tmpl").ParseFiles(filepath.Join(packageDir(), "const.tmpl"))
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
err = constTpl.Execute(constFile,
|
|
|
|
map[string]interface{}{
|
|
|
|
"PackageName": packageName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2016-08-11 16:42:28 +08:00
|
|
|
}
|