From 4378c9a40edcd2db5eda9f73ea3fba9fdede5024 Mon Sep 17 00:00:00 2001 From: Peng Gao Date: Wed, 31 Aug 2016 12:21:05 +0800 Subject: [PATCH] Fix type finding bugs and add zero vaule for func 1. In the main loop to find typeName, it will continue find type if a type has been already found, which will make empty string overwite it latter, so if it is found first time, assuming this is the type we want, and break out the loop. 2. Add support for zero value for func type. 3. If the type not a builtin type (*ast.Ident), assuming it is a composite structure, like *ast.StructType. Signed-off-by: Peng Gao --- cachemap/main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cachemap/main.go b/cachemap/main.go index 44643b0..fc63fd7 100644 --- a/cachemap/main.go +++ b/cachemap/main.go @@ -37,6 +37,9 @@ func findInGenDecl(genDecl *ast.GenDecl, valueName string) string { indent, ok := valueSpec.Type.(*ast.Ident) if ok { return indent.Name + } else { + // For other types like StructType + return valueName } } } @@ -71,7 +74,8 @@ func zeroValue(s string) string { if s[0] == '*' || // Pointer strings.Index(s, "map") == 0 || // map strings.Index(s, "chan") == 0 || // chan - strings.Index(s, "[]") == 0 { // slice + strings.Index(s, "[]") == 0 || // slice + strings.Index(s, "func") == 0 { // func return "nil" } return s + "{}" @@ -125,11 +129,15 @@ func main() { } packageName := "main" typeName := "" +FIND: for name, pkg := range pkgs { packageName = name for _, f := range pkg.Files { for _, decl := range f.Decls { typeName = findInDecl(decl, *valueType) + if typeName != "" { + break FIND + } } } }