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 <peng.gao.dut@gmail.com>
This commit is contained in:
parent
ebc1ab826e
commit
4378c9a40e
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue