Simplified type handling.

This commit is contained in:
Greg 2018-08-04 16:39:39 -04:00
parent ee4f005b6c
commit 1c3bab0dcc

View File

@ -15,6 +15,7 @@ type visitor struct{}
func trimType(n *string) { func trimType(n *string) {
*n = pkgreg.ReplaceAllString(*n,"") *n = pkgreg.ReplaceAllString(*n,"")
*n = treg.ReplaceAllString(*n,"")
} }
func (v *visitor) Visit(node ast.Node) ast.Visitor { func (v *visitor) Visit(node ast.Node) ast.Visitor {
@ -36,14 +37,7 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
case *ast.CompositeLit: case *ast.CompositeLit:
tp = types.TypeString(info.TypeOf(arg),types.RelativeTo(pkg)) tp = types.TypeString(info.TypeOf(arg),types.RelativeTo(pkg))
case *ast.Ident: case *ast.Ident:
if arg.Name == "true" || arg.Name == "false" { tp = types.TypeString(info.TypeOf(arg),types.RelativeTo(pkg))
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())
tp = types.TypeString(obj.Type(),types.RelativeTo(pkg))
trimType(&tp) trimType(&tp)
for _, s := range impreg.FindAllStringSubmatchIndex(tp, -1) { for _, s := range impreg.FindAllStringSubmatchIndex(tp, -1) {
pkgname := impreg.ExpandString(make([]byte,0), "$1", tp, s) pkgname := impreg.ExpandString(make([]byte,0), "$1", tp, s)
@ -67,6 +61,7 @@ var ts map[string]tps
var reg *regexp.Regexp var reg *regexp.Regexp
var pkgreg *regexp.Regexp var pkgreg *regexp.Regexp
var impreg *regexp.Regexp var impreg *regexp.Regexp
var treg *regexp.Regexp
var imps map[string]string var imps map[string]string
var needImps map[string]bool var needImps map[string]bool
var info *types.Info var info *types.Info
@ -104,6 +99,7 @@ func init() {
pkgreg = regexp.MustCompile(`[a-zA-Z_]+[a-zA-Z_0-9\.]*/|"`) pkgreg = regexp.MustCompile(`[a-zA-Z_]+[a-zA-Z_0-9\.]*/|"`)
impreg = regexp.MustCompile(`([a-zA-Z_]+[a-zA-Z_0-9]+)\.[a-zA-Z_]+[a-zA-Z_0-9]+`) impreg = regexp.MustCompile(`([a-zA-Z_]+[a-zA-Z_0-9]+)\.[a-zA-Z_]+[a-zA-Z_0-9]+`)
treg = regexp.MustCompile(`untyped `)
imps = make(map[string]string) imps = make(map[string]string)
needImps = make(map[string]bool) needImps = make(map[string]bool)