Skip to content

Conversation

Serabe
Copy link
Contributor

@Serabe Serabe commented Jan 10, 2017

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!

@awalterschulze
Copy link
Member

Really cool project.
I am wondering if I understand one of your future ideas correctly.
But is it possible to generate the gogoprotobuf.proto file from the gotypes, like kubernetes has done, but has not exposed as a separate project?
Would it be possible then to translate thrift definitions to proto definitions, by generated the go code from thrift and then generated the proto file from the gotypes?

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?

@awalterschulze
Copy link
Member

To fix the tests you could rather use gogofast_out is possible?

@awalterschulze
Copy link
Member

@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
Copy link
Member

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.
@Serabe Serabe force-pushed the feature/drop_type_declaration branch from 20ebf70 to fed5238 Compare January 18, 2017 08:26
@Serabe
Copy link
Contributor Author

Serabe commented Jan 18, 2017

Fixed and rebased to clean the history

@Serabe
Copy link
Contributor Author

Serabe commented Jan 18, 2017

Travis is not picking up the PR, can you manually start the build? Thank you!

@awalterschulze
Copy link
Member

There are still some gofast_out commands left
They need to be replaced with

protoc-min-version --version="3.0.0" --gogo_out=. ...

or

protoc-min-version --version="3.0.0" --gogofast_out=. ...

@awalterschulze
Copy link
Member

I just restarted the build

@Serabe
Copy link
Contributor Author

Serabe commented Jan 18, 2017

Fixed, thank you!

@Serabe
Copy link
Contributor Author

Serabe commented Jan 18, 2017

Build is passing, let me know if you want me to do a rebase.

Thank you!


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)
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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) {
Copy link
Member

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

Copy link
Contributor Author

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) {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If + GoFmt. Check here

@@ -0,0 +1,4 @@
regenerate:
go install github.com/gogo/protobuf/protoc-gen-gogo
go install github.com/gogo/protobuf/protoc-gen-gofast
Copy link
Member

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

return true
}

func (d *Dropped) GetName() string {
Copy link
Member

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";
Copy link
Member

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

@awalterschulze
Copy link
Member

Since everything is working I am going to start becoming a little bit more nit picky.

@Serabe
Copy link
Contributor Author

Serabe commented Jan 18, 2017

Great! I keep things in two different commits to ease the review.

Thank you!

@awalterschulze
Copy link
Member

awalterschulze commented Jan 18, 2017

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.

@Serabe
Copy link
Contributor Author

Serabe commented Jan 18, 2017

I:

  • Renamed thetest.proto to drop.proto and enumdrop.proto. Generated files were removed and regenerated.
  • Remove gofast install instruction from Makefiles.
  • Provided a gist with the patch for protoc-gen-gogo/generator/generator.go.

I'm currently refactoring generator.go so drop options and getters are fully independent.

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
@Serabe
Copy link
Contributor Author

Serabe commented Jan 19, 2017

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
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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>
Copy link
Member

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>
Copy link
Member

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

@awalterschulze
Copy link
Member

Looks really good :)

@smarterclayton
Copy link
Contributor

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.

@awalterschulze
Copy link
Member

awalterschulze commented Jan 19, 2017 via email

* `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.
@Serabe
Copy link
Contributor Author

Serabe commented Jan 20, 2017

@awalterschulze I readded the comment, renamed the options and added the file-scoped versions with tests. Is this ok?

@awalterschulze
Copy link
Member

awalterschulze commented Jan 20, 2017 via email

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>
Copy link
Member

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>
Copy link
Member

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;
Copy link
Member

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

@Serabe
Copy link
Contributor Author

Serabe commented Jan 20, 2017

@awalterschulze done! Thank you!

@awalterschulze
Copy link
Member

Oh last thing. Do you want to add yourself to the CONTRIBUTORS or AUTHORS file as appropriate?

@Serabe
Copy link
Contributor Author

Serabe commented Jan 20, 2017

Done, thank you!

@awalterschulze awalterschulze merged commit 265e960 into gogo:master Jan 20, 2017
@awalterschulze
Copy link
Member

Thanks for one of the best features in a long time. This is really great work :)

@Serabe
Copy link
Contributor Author

Serabe commented Jan 20, 2017 via email

@Serabe Serabe deleted the feature/drop_type_declaration branch January 23, 2017 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants