Skip to content

Commit

Permalink
optimize code imports in generate code for external package code
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Dec 23, 2022
1 parent e8ca1d7 commit 22bc41d
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions core/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,30 @@ func (d *IfaceDescriptor) SetInnerInOuts(inOuts []reflect.Type) {
}
}
// to set fields pkg name alias map
pkgNames := utils.NewStrSet()
pkgNames := make(map[string]string)
for _, t := range extSts {
pkgPath := t.PkgPath()
if pkgPath == "" {
continue
}
// had import so no need process
if _, exist := d.Imports[pkgPath]; exist {
continue
}
// process alias if needed
pkgs := strings.Split(pkgPath, "/")
pkgName := pkgs[len(pkgs)-1]
isAlias := false
for err := pkgNames.Add(pkgName); err != nil; err = pkgNames.Add(pkgName) {
isAlias = true
pkgName = pkgName + "_m"
}
if !isAlias {
pkgName = ""
if pkg, exist := pkgNames[pkgName]; !exist {
pkgNames[pkgName] = pkgPath
d.Imports[pkgPath] = ""
} else {
for exist && pkg != pkgPath {
pkgName = pkgName + "_m"
pkg, exist = pkgNames[pkgName]
}
pkgNames[pkgName] = pkgPath
d.Imports[pkgPath] = pkgName
}
d.Imports[pkgPath] = pkgName
}
d.setFiledImports()
}
Expand Down

0 comments on commit 22bc41d

Please sign in to comment.