Skip to content

Commit ddf2292

Browse files
authored
protoc-gen-go: only disambiguate predefined idents for local package names (#724)
Permit creating a generated package named e.g., "string". Apply disambiguation to prevent creating a local import name that conflicts with predefined identifiers; importing package "string" will be done as: import string1 "string"
1 parent 31e0d06 commit ddf2292

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

protoc-gen-go/generator/generator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func (g *Generator) GoPackageName(importPath GoImportPath) GoPackageName {
535535
return name
536536
}
537537
name := cleanPackageName(baseName(string(importPath)))
538-
for i, orig := 1, name; g.usedPackageNames[name]; i++ {
538+
for i, orig := 1, name; g.usedPackageNames[name] || isGoPredeclaredIdentifier[string(name)]; i++ {
539539
name = orig + GoPackageName(strconv.Itoa(i))
540540
}
541541
g.packageNames[importPath] = name
@@ -567,8 +567,7 @@ func RegisterUniquePackageName(pkg string, f *FileDescriptor) string {
567567
return string(name)
568568
}
569569

570-
var isGoKeywordOrPredeclaredIdentifier = map[string]bool{
571-
// Keywords
570+
var isGoKeyword = map[string]bool{
572571
"break": true,
573572
"case": true,
574573
"chan": true,
@@ -594,8 +593,9 @@ var isGoKeywordOrPredeclaredIdentifier = map[string]bool{
594593
"switch": true,
595594
"type": true,
596595
"var": true,
596+
}
597597

598-
// Predeclared Identifiers
598+
var isGoPredeclaredIdentifier = map[string]bool{
599599
"append": true,
600600
"bool": true,
601601
"byte": true,
@@ -640,7 +640,7 @@ var isGoKeywordOrPredeclaredIdentifier = map[string]bool{
640640
func cleanPackageName(name string) GoPackageName {
641641
name = strings.Map(badToUnderscore, name)
642642
// Identifier must not be keyword or predeclared identifier: insert _.
643-
if isGoKeywordOrPredeclaredIdentifier[name] {
643+
if isGoKeyword[name] {
644644
name = "_" + name
645645
}
646646
// Identifier must not begin with digit: insert _.

protoc-gen-go/generator/name_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestGoPackageOption(t *testing.T) {
6868
{"foo", "", "foo", true},
6969
{"github.com/golang/bar", "github.com/golang/bar", "bar", true},
7070
{"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true},
71-
{"github.com/golang/string", "github.com/golang/string", "_string", true},
71+
{"github.com/golang/string", "github.com/golang/string", "string", true},
7272
}
7373
for _, tc := range tests {
7474
d := &FileDescriptor{
@@ -86,6 +86,25 @@ func TestGoPackageOption(t *testing.T) {
8686
}
8787
}
8888

89+
func TestPackageNames(t *testing.T) {
90+
g := New()
91+
g.packageNames = make(map[GoImportPath]GoPackageName)
92+
g.usedPackageNames = make(map[GoPackageName]bool)
93+
for _, test := range []struct {
94+
importPath GoImportPath
95+
want GoPackageName
96+
}{
97+
{"github.com/golang/foo", "foo"},
98+
{"github.com/golang/second/package/named/foo", "foo1"},
99+
{"github.com/golang/third/package/named/foo", "foo2"},
100+
{"github.com/golang/conflicts/with/predeclared/ident/string", "string1"},
101+
} {
102+
if got := g.GoPackageName(test.importPath); got != test.want {
103+
t.Errorf("GoPackageName(%v) = %v, want %v", test.importPath, got, test.want)
104+
}
105+
}
106+
}
107+
89108
func TestUnescape(t *testing.T) {
90109
tests := []struct {
91110
in string

0 commit comments

Comments
 (0)