Skip to content

Commit

Permalink
fix: fixing for define imported package's local name with flag problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ErfanMomeniii authored and Erfan Momeni committed Jul 13, 2023
1 parent e14f5b1 commit 2cf432d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac

packagesName := createPackageMap(sortedPaths)

definedImports := make(map[string]string, len(im))
if *imports != "" {
for _, kv := range strings.Split(*imports, ",") {
eq := strings.Index(kv, "=")
k, v := kv[:eq], kv[eq+1:]
if k != "." {
definedImports[v] = k
}
}
}

g.packageMap = make(map[string]string, len(im))
localNames := make(map[string]bool, len(im))
for _, pth := range sortedPaths {
Expand All @@ -327,9 +338,14 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
// Local names for an imported package can usually be the basename of the import path.
// A couple of situations don't permit that, such as duplicate local names
// (e.g. importing "html/template" and "text/template"), or where the basename is
// a keyword (e.g. "foo/case").
// a keyword (e.g. "foo/case") or when defining a name for that by using the -imports flag.
// try base0, base1, ...
pkgName := base

if _, ok := definedImports[base]; ok {
pkgName = definedImports[base]
}

i := 0
for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() {
pkgName = base + strconv.Itoa(i)
Expand Down

0 comments on commit 2cf432d

Please sign in to comment.