Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
c4b192b6
Unverified
Commit
c4b192b6
authored
Apr 24, 2016
by
Clayton Coleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Protobuf generation should strip empty-imports
The imports are generated because the packages are in the search tree, but nothing in the generated code needs them. For now, strip them.
parent
3a4f179c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
2 deletions
+35
-2
parser.go
cmd/libs/go2idl/go-to-protobuf/protobuf/parser.go
+35
-2
No files found.
cmd/libs/go2idl/go-to-protobuf/protobuf/parser.go
View file @
c4b192b6
...
...
@@ -81,9 +81,13 @@ func RewriteGeneratedGogoProtobufFile(name string, extractFn ExtractFunc, header
// remove types that are already declared
decls
:=
[]
ast
.
Decl
{}
for
_
,
d
:=
range
file
.
Decls
{
if
!
dropExistingTypeDeclarations
(
d
,
extractFn
)
{
decls
=
append
(
decls
,
d
)
if
dropExistingTypeDeclarations
(
d
,
extractFn
)
{
continue
}
if
dropEmptyImportDeclarations
(
d
)
{
continue
}
decls
=
append
(
decls
,
d
)
}
file
.
Decls
=
decls
...
...
@@ -93,6 +97,8 @@ func RewriteGeneratedGogoProtobufFile(name string, extractFn ExtractFunc, header
})
}
// dropExistingTypeDeclarations removes any type declaration for which extractFn returns true. The function
// returns true if the entire declaration should be dropped.
func
dropExistingTypeDeclarations
(
decl
ast
.
Decl
,
extractFn
ExtractFunc
)
bool
{
switch
t
:=
decl
.
(
type
)
{
case
*
ast
.
GenDecl
:
...
...
@@ -117,6 +123,33 @@ func dropExistingTypeDeclarations(decl ast.Decl, extractFn ExtractFunc) bool {
return
false
}
// dropEmptyImportDeclarations strips any generated but no-op imports from the generated code
// to prevent generation from being able to define side-effects. The function returns true
// if the entire declaration should be dropped.
func
dropEmptyImportDeclarations
(
decl
ast
.
Decl
)
bool
{
switch
t
:=
decl
.
(
type
)
{
case
*
ast
.
GenDecl
:
if
t
.
Tok
!=
token
.
IMPORT
{
return
false
}
specs
:=
[]
ast
.
Spec
{}
for
_
,
s
:=
range
t
.
Specs
{
switch
spec
:=
s
.
(
type
)
{
case
*
ast
.
ImportSpec
:
if
spec
.
Name
!=
nil
&&
spec
.
Name
.
Name
==
"_"
{
continue
}
specs
=
append
(
specs
,
spec
)
}
}
if
len
(
specs
)
==
0
{
return
true
}
t
.
Specs
=
specs
}
return
false
}
func
RewriteTypesWithProtobufStructTags
(
name
string
,
structTags
map
[
string
]
map
[
string
]
string
)
error
{
return
rewriteFile
(
name
,
[]
byte
{},
func
(
fset
*
token
.
FileSet
,
file
*
ast
.
File
)
error
{
allErrs
:=
[]
error
{}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment