Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 303742c

Browse files
committed
Merge pull request kubernetes#24723 from smarterclayton/strip_noop_imports
Protobuf generation should strip side-effect imports
2 parents 43b644e + 99430cf commit 303742c

File tree

12 files changed

+40
-27
lines changed

12 files changed

+40
-27
lines changed

cmd/libs/go2idl/go-to-protobuf/protobuf/parser.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@ func RewriteGeneratedGogoProtobufFile(name string, extractFn ExtractFunc, header
8181
// remove types that are already declared
8282
decls := []ast.Decl{}
8383
for _, d := range file.Decls {
84-
if !dropExistingTypeDeclarations(d, extractFn) {
85-
decls = append(decls, d)
84+
if dropExistingTypeDeclarations(d, extractFn) {
85+
continue
86+
}
87+
if dropEmptyImportDeclarations(d) {
88+
continue
8689
}
90+
decls = append(decls, d)
8791
}
8892
file.Decls = decls
8993

@@ -93,6 +97,8 @@ func RewriteGeneratedGogoProtobufFile(name string, extractFn ExtractFunc, header
9397
})
9498
}
9599

100+
// dropExistingTypeDeclarations removes any type declaration for which extractFn returns true. The function
101+
// returns true if the entire declaration should be dropped.
96102
func dropExistingTypeDeclarations(decl ast.Decl, extractFn ExtractFunc) bool {
97103
switch t := decl.(type) {
98104
case *ast.GenDecl:
@@ -117,6 +123,33 @@ func dropExistingTypeDeclarations(decl ast.Decl, extractFn ExtractFunc) bool {
117123
return false
118124
}
119125

126+
// dropEmptyImportDeclarations strips any generated but no-op imports from the generated code
127+
// to prevent generation from being able to define side-effects. The function returns true
128+
// if the entire declaration should be dropped.
129+
func dropEmptyImportDeclarations(decl ast.Decl) bool {
130+
switch t := decl.(type) {
131+
case *ast.GenDecl:
132+
if t.Tok != token.IMPORT {
133+
return false
134+
}
135+
specs := []ast.Spec{}
136+
for _, s := range t.Specs {
137+
switch spec := s.(type) {
138+
case *ast.ImportSpec:
139+
if spec.Name != nil && spec.Name.Name == "_" {
140+
continue
141+
}
142+
specs = append(specs, spec)
143+
}
144+
}
145+
if len(specs) == 0 {
146+
return true
147+
}
148+
t.Specs = specs
149+
}
150+
return false
151+
}
152+
120153
func RewriteTypesWithProtobufStructTags(name string, structTags map[string]map[string]string) error {
121154
return rewriteFile(name, []byte{}, func(fset *token.FileSet, file *ast.File) error {
122155
allErrs := []error{}

federation/apis/federation/v1alpha1/generated.pb.go

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/resource/generated.pb.go

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/unversioned/generated.pb.go

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/v1/generated.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/apps/v1alpha1/generated.pb.go

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/autoscaling/v1/generated.pb.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/batch/v1/generated.pb.go

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/extensions/v1beta1/generated.pb.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/runtime/generated.pb.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)