-
Notifications
You must be signed in to change notification settings - Fork 810
Drop types declaration option #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop types declaration option #250
Conversation
Really cool project. Really cool idea about omitting the generated types. I'll just have to think about this concept a little. Also can't you use the plugins: populate, equal and testgen to generate those tests, that you manually wrote? |
To fix the tests you could rather use gogofast_out is possible? |
@smarterclayton do think this feature will help kubertnetes? |
regenerate: | ||
go install github.com/gogo/protobuf/protoc-gen-gogo | ||
go install github.com/gogo/protobuf/protoc-gen-gofast | ||
protoc --gofast_out=. --proto_path=../../../../../:../../protobuf/:. thetest.proto |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
protoc-min-version --version="3.0.0" --gogo_out=. --proto_path=../../../../../:../../protobuf/:. thetest.proto
Add two options, one for messages and one for enums, to drop type declarations. In messages, it also drops getters and setters. In enums, it also drops the generated constants.
20ebf70
to
fed5238
Compare
Fixed and rebased to clean the history |
Travis is not picking up the PR, can you manually start the build? Thank you! |
There are still some gofast_out commands left
or
|
I just restarted the build |
Fixed, thank you! |
Build is passing, let me know if you want me to do a rebase. Thank you! |
gogoproto/helper.go
Outdated
|
||
func HasGoGetters(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool { | ||
return proto.GetBoolExtension(message.Options, E_GoprotoGetters, proto.GetBoolExtension(file.Options, E_GoprotoGettersAll, true)) | ||
return proto.GetBoolExtension(message.Options, E_GoprotoGetters, proto.GetBoolExtension(file.Options, E_GoprotoGettersAll, true)) && !IsDropTypeDeclaration(message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
I mean if someone doesn't want gogetters they can turn them off, but otherwise there is a good chance they will still work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason is that in protoc-gen-gogo/generator/generator.go
the information needed to create the getters is not worked out unless the struct is created. I can try and refactor that part if you are ok with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think being able to turn the getters on or off will useful.
But its unfortunate that this has to complicate the code.
Please go ahead and refactor this, thank you very much.
g.PrintComments(message.path) | ||
g.P("type ", ccTypeName, " struct {") | ||
g.In() | ||
if !gogoproto.IsDropTypeDeclaration(message.DescriptorProto) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
github is really struggling with this diff, I assume you only added this if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If + GoFmt. Check here
|
||
g.P(name, " ", ccTypeName, " = ", e.Number) | ||
g.file.addExport(enum, constOrVarSymbol{name, "const", ccTypeName}) | ||
if !gogoproto.IsEnumDropTypeDeclaration(enum.EnumDescriptorProto) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
github is really struggling with this diff, I assume you only added this if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If + GoFmt. Check here
test/drop_type_declaration/Makefile
Outdated
@@ -0,0 +1,4 @@ | |||
regenerate: | |||
go install github.com/gogo/protobuf/protoc-gen-gogo | |||
go install github.com/gogo/protobuf/protoc-gen-gofast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to install gofast anymore
test/drop_type_declaration/models.go
Outdated
return true | ||
} | ||
|
||
func (d *Dropped) GetName() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated getters would probably be fine here
@@ -0,0 +1,27 @@ | |||
syntax = "proto3"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we call this file something else and not thetest.proto as well?
How about drop.proto
Since everything is working I am going to start becoming a little bit more nit picky. |
Great! I keep things in two different commits to ease the review. Thank you! |
I am looking at everything everytime, so don't worry about the professionalism of the commits too much. As long as the end result looks good I'm happy. |
I:
I'm currently refactoring Thank you! |
This commit extract a few variable initializations that was being done at the same time of generating the type for a given message. This allow for the Go Getters option to be independent from the drop type declaration one
I've exrtracted some variable declaration out of the part of the code that generated the type definition for a given message. I think I have extracted the minimal amount of code needed for go getters to be independent from drop type declaration. I think this completes your first review. Thank you! |
} | ||
|
||
for i, field := range message.Field { | ||
// Allocate the getter and the field at the same time so name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this comment deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in next push. I intended to fix it, but I rather do that if I have time in the future in a different PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok thats fine, then just bring back the comment :)
This is coming all the way from golang/protobuf so we want to fix it there and then pull that fix in here.
extensions.md
Outdated
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/plugin/marshalto"> unsafe_marshaler</a> </td><td> Message </td><td> bool </td><td> if true, a Marshal and MarshalTo method is generated for the specific message. The generated code uses the unsafe package. </td><td> false</td></tr> | ||
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/plugin/unmarshal">unsafe_unmarshaler</a></td><td> Message </td><td> bool </td><td> if true, an Unmarshal method is generated for the specific message. The generated code uses the unsafe package. </td><td> false</td></tr> | ||
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/plugin/marshalto">stable_marshaler</a></td><td> Message </td><td> bool </td><td> if true, a Marshal and MarshalTo method is generated for the specific message, but unlike marshaler the output is guaranteed to be deterministic, at the sacrifice of some speed</td><td> false </td></tr> | ||
<tr><td>drop_type_declaration (beta)</td><td> Message </td><td> bool </td><td> if true, type declaration of messages are excluded from output. </td><td> false </td></tr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about 'typedecl' with default true
extensions.md
Outdated
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/gogoproto"> castkey </a> (beta) </td><td> Field </td><td> string </td><td> Changes the generated fieldtype for a map key. All generated code assumes that this type is castable to the protocol buffer field type. Only supported on maps. </td><td> goprotobuf field type </td></tr> | ||
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/gogoproto"> castvalue </a> (beta) </td><td> Field </td><td> string </td><td> Changes the generated fieldtype for a map value. All generated code assumes that this type is castable to the protocol buffer field type. Only supported on maps. </td><td> goprotobuf field type </td></tr> | ||
<tr><td>enum_customname (beta)</td><td> Enum </td><td> string </td><td>Sets the type name of an enum. If goproto_enum_prefix is enabled, this value will be used as a prefix when generating enum values.</td><td>goprotobuf enum type name. Helps with golint issues.</td></tr> | ||
<tr><td>enum_drop_type_declaration (beta)</td><td> Enum </td><td> bool </td><td> if true, type declaration of enums are excluded from output. </td><td> false </td></tr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about 'enumdecl' with default true
Looks really good :) |
Yes this would help us - we currently perform a Go AST traversal of the generated types to remove it and this would remove the need for us having to do that. |
Thats great to hear.
I just remembered, can you also add Fileoptions, _all versions of the
extensions?
…On Thu, 19 Jan 2017, 18:38 Clayton Coleman, ***@***.***> wrote:
Yes this would help us - we currently perform a Go AST traversal of the
generated types to remove it and this would remove the need for us having
to do that.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#250 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvsLSbWNU0PO2TmL-1rgnbEwpr_DS3Tks5rT59wgaJpZM4LfMht>
.
|
* `drop_type_declaration` becomes `typedecl` * `enum_drop_type_declaration` becomes `enumdecl` Default values are now true and for removing the declaration the value needs to be `false`. Options `typedecl_all` and `enumdecl_all` are added.
@awalterschulze I readded the comment, renamed the options and added the file-scoped versions with tests. Is this ok? |
I am pretty sure it will be, but I'll check when I get home :)
…On Fri, 20 Jan 2017 at 12:16 Sergio Arbeo ***@***.***> wrote:
@awalterschulze <https://github.com/awalterschulze> I readded the
comment, renamed the options and added the file-scoped versions with tests.
Is this ok?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#250 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvsLaKmWdvEW3AC22ioKGKRS512KssZks5rUJeKgaJpZM4LfMht>
.
|
extensions.md
Outdated
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/plugin/marshalto"> unsafe_marshaler</a> </td><td> Message </td><td> bool </td><td> if true, a Marshal and MarshalTo method is generated for the specific message. The generated code uses the unsafe package. </td><td> false</td></tr> | ||
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/plugin/unmarshal">unsafe_unmarshaler</a></td><td> Message </td><td> bool </td><td> if true, an Unmarshal method is generated for the specific message. The generated code uses the unsafe package. </td><td> false</td></tr> | ||
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/plugin/marshalto">stable_marshaler</a></td><td> Message </td><td> bool </td><td> if true, a Marshal and MarshalTo method is generated for the specific message, but unlike marshaler the output is guaranteed to be deterministic, at the sacrifice of some speed</td><td> false </td></tr> | ||
<tr><td>typedecl (beta)</td><td> Message </td><td> bool </td><td> if false, type declaration of the message are excluded from output. </td><td> true </td></tr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if false, type declaration of the message is excluded from the generated output
extensions.md
Outdated
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/gogoproto"> castkey </a> (beta) </td><td> Field </td><td> string </td><td> Changes the generated fieldtype for a map key. All generated code assumes that this type is castable to the protocol buffer field type. Only supported on maps. </td><td> goprotobuf field type </td></tr> | ||
<tr><td><a href="http://godoc.org/github.com/gogo/protobuf/gogoproto"> castvalue </a> (beta) </td><td> Field </td><td> string </td><td> Changes the generated fieldtype for a map value. All generated code assumes that this type is castable to the protocol buffer field type. Only supported on maps. </td><td> goprotobuf field type </td></tr> | ||
<tr><td>enum_customname (beta)</td><td> Enum </td><td> string </td><td>Sets the type name of an enum. If goproto_enum_prefix is enabled, this value will be used as a prefix when generating enum values.</td><td>goprotobuf enum type name. Helps with golint issues.</td></tr> | ||
<tr><td>enumdecl (beta)</td><td> Enum </td><td> bool </td><td> if false, type declaration of the enum are excluded from output. </td><td> true </td></tr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if false, type declaration of the enum is excluded from the generated output
option (gogoproto.testgen_all) = true; | ||
option (gogoproto.populate_all) = true; | ||
option (gogoproto.benchgen_all) = true; | ||
option (gogoproto.unmarshaler_all) = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly this probably won't work as well when the marshaler and unmarshaler are not generated. Perhaps we should make a note of this in the docs, that it is not supported without code generation
@awalterschulze done! Thank you! |
Oh last thing. Do you want to add yourself to the CONTRIBUTORS or AUTHORS file as appropriate? |
Done, thank you! |
Thanks for one of the best features in a long time. This is really great work :) |
Thanks!
…On 20 January 2017 at 17:23:07, Walter Schulze ***@***.***) wrote:
Thanks for one of the best features in a long time. This is really great
work :)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#250 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AACNfYNrSUaZFF1uyuI4BnbY4xM1A3nKks5rUN9rgaJpZM4LfMht>
.
|
At src-d, we are using our Go code as source of truth for the rest of the platform. For this, we started writing proteus, a tool that creates proto files from our Go source.
Due to this, we needed to remove type declarations coming from both protobuf messages and enums. These options seem useful for other people and projects (like kubernetes/gengo), so we are opening this PR in case you find this feature helpful.
Thank you!