Skip to content

Commit

Permalink
fix for transitive enum package import failures #585 (#626)
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Solis <[email protected]>

Signed-off-by: Eduardo Solis <[email protected]>
Co-authored-by: Elliot Jackson <[email protected]>
  • Loading branch information
eleduardo and Elliot Jackson authored Sep 21, 2022
1 parent 05f8c12 commit 2eca307
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
8 changes: 4 additions & 4 deletions templates/go/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"google.golang.org/protobuf/types/known/anypb"
{{ range $pkg, $path := enumPackages (externalEnums .) }}
{{ $pkg }} "{{ $path }}"
{{ range $pkg, $enum := enumPackages (externalEnums .) }}
{{ $pkg }} "{{ $enum.FilePath }}"
{{ end }}
)
Expand All @@ -40,8 +40,8 @@ var (
_ = anypb.Any{}
_ = sort.Sort
{{ range $pkg, $path := enumPackages (externalEnums .) }}
_ = {{ $pkg }}.{{ enumName (index (externalEnums $) 0) }}(0)
{{ range $pkg, $enum := enumPackages (externalEnums .) }}
_ = {{ $pkg }}.{{ $enum.Name }}(0)
{{ end }}
)
Expand Down
23 changes: 16 additions & 7 deletions templates/goshared/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,23 +343,32 @@ func (fns goSharedFuncs) enumName(enum pgs.Enum) string {
}
}

func (fns goSharedFuncs) enumPackages(enums []pgs.Enum) map[pgs.Name]pgs.FilePath {
out := make(map[pgs.Name]pgs.FilePath, len(enums))
type NormalizedEnum struct {
FilePath pgs.FilePath
Name string
}

func (fns goSharedFuncs) enumPackages(enums []pgs.Enum) map[pgs.Name]NormalizedEnum {
out := make(map[pgs.Name]NormalizedEnum, len(enums))

nameCollision := make(map[pgs.Name]int)

for _, en := range enums {

pkgName := fns.PackageName(en)

path, ok := out[pkgName]
if normalized, ok := out[pkgName]; ok {
if normalized.FilePath != fns.ImportPath(en) {
nameCollision[pkgName] = nameCollision[pkgName] + 1
pkgName = pkgName + pgs.Name(strconv.Itoa(nameCollision[pkgName]))
}
}

if ok && path != fns.ImportPath(en) {
nameCollision[pkgName] = nameCollision[pkgName] + 1
pkgName = pkgName + pgs.Name(strconv.Itoa(nameCollision[pkgName]))
out[pkgName] = NormalizedEnum{
Name: fns.enumName(en),
FilePath: fns.ImportPath(en),
}

out[pkgName] = fns.ImportPath(en)
}

return out
Expand Down

0 comments on commit 2eca307

Please sign in to comment.