Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion v2/generator/import_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ func NewImportTrackerForPackage(local string, typesToAdd ...*types.Type) *namer.
tracker := namer.NewDefaultImportTracker(types.Name{Package: local})
tracker.IsInvalidType = func(*types.Type) bool { return false }
tracker.LocalName = func(name types.Name) string { return goTrackerLocalName(&tracker, local, name) }
tracker.PrintImport = func(path, name string) string { return name + " \"" + path + "\"" }
tracker.PrintImport = func(path, name string) string {
dirs := strings.Split(path, namer.GoSeperator)
if len(dirs) > 0 && name == dirs[len(dirs)-1] {
return "\"" + path + "\""
}
return name + " \"" + path + "\""
}

tracker.AddTypes(typesToAdd...)
return &tracker
Expand Down
23 changes: 20 additions & 3 deletions v2/generator/import_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ func TestNewImportTracker(t *testing.T) {
{
name: "builtin",
inputTypes: []*types.Type{
{Name: types.Name{Package: "context"}},
{Name: types.Name{Package: "net/http"}},
{Name: types.Name{Package: "x/net/http"}},
},
expectedImports: []string{
`http "net/http"`,
`"context"`,
`"net/http"`,
`nethttp "x/net/http"`,
},
},
{
Expand All @@ -51,8 +55,21 @@ func TestNewImportTracker(t *testing.T) {
{Name: types.Name{Package: "foo/bar/pkg1"}},
},
expectedImports: []string{
`pkg1 "foo/bar/pkg1"`,
`pkg2 "foo/bar/pkg2"`,
`"foo/bar/pkg1"`,
`"foo/bar/pkg2"`,
},
},
{
name: "duplicate",
inputTypes: []*types.Type{
{Name: types.Name{Package: "foo/bar/pkg2/v1"}},
{Name: types.Name{Package: "foo/bar/pkg3/v1"}},
{Name: types.Name{Package: "foo/bar/pkg1/v1"}},
},
expectedImports: []string{
`pkg1v1 "foo/bar/pkg1/v1"`,
`"foo/bar/pkg2/v1"`,
`pkg3v1 "foo/bar/pkg3/v1"`,
},
},
{
Expand Down