Skip to content

Commit

Permalink
Merge pull request #92 from hexdigest/fix-87
Browse files Browse the repository at this point in the history
Fixes #87
  • Loading branch information
hexdigest authored Aug 25, 2024
2 parents 3f2f531 + 7d704b6 commit 4b3364a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
5 changes: 4 additions & 1 deletion cmd_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,8 @@ package {{.Package.Name}}
{{if (not .Options.HeaderVars.DisableGoGenerate)}}
//{{"go:generate"}} gowrap gen -p {{.SourcePackage.PkgPath}} -i {{.Options.InterfaceName}} -t {{.Options.HeaderVars.Template}} -o {{.Options.HeaderVars.OutputFileName}}{{.Options.HeaderVars.VarsArgs}} -l "{{.Options.LocalPrefix}}"
{{end}}
import(
{{range $import := .Options.Imports}}{{$import}}
{{end}}
)
`
17 changes: 11 additions & 6 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"text/template"

"github.com/pkg/errors"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"

Expand Down Expand Up @@ -199,16 +201,19 @@ func NewGenerator(options Options) (*Generator, error) {
return nil, errors.Wrap(err, "failed to parse source package")
}

interfaceType := srcPackage.Name + "." + options.InterfaceName
var interfaceType string
if srcPackage.PkgPath == dstPackage.PkgPath {
interfaceType = options.InterfaceName
srcPackageAST.Name = ""
} else {
if options.SourcePackageAlias != "" {
srcPackageAST.Name = options.SourcePackageAlias
} else {
srcPackageAST.Name = "_source" + cases.Title(language.Und, cases.NoLower).String(srcPackageAST.Name)
}

options.Imports = append(options.Imports, `"`+srcPackage.PkgPath+`"`)
interfaceType = srcPackageAST.Name + "." + options.InterfaceName
options.Imports = append(options.Imports, srcPackageAST.Name+` "`+srcPackage.PkgPath+`"`)
}

output, err := findTarget(processInput{
Expand Down Expand Up @@ -251,12 +256,12 @@ func NewGenerator(options Options) (*Generator, error) {

func makeImports(imports []*ast.ImportSpec) []string {
result := make([]string, len(imports))
for _, i := range imports {
for i, im := range imports {
var name string
if i.Name != nil {
name = i.Name.Name
if im.Name != nil {
name = im.Name.Name
}
result = append(result, name+" "+i.Path.Value)
result[i] = name + " " + im.Path.Value
}

return result
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gojuno/minimock/v3 v3.0.10
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
golang.org/x/text v0.3.7
golang.org/x/tools v0.1.12
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down

0 comments on commit 4b3364a

Please sign in to comment.