-
Notifications
You must be signed in to change notification settings - Fork 46
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
cmd/collectproto: a new tool for collecting protobuf #659
Conversation
A new command line tool for collecting all protobuf declarations was added. From the `README.md`: Collect, combine and produce a single protobuf file with all declarations. This program reads provided protobuf declarations and: - removes all non standard plugin information (ie gogoproto) - removes all package declaration - removes all but one syntax declaration - removes all import declarations, inlines all weave protobuf imports Combined result is written to stdout. Example usage: ``` $ go run cmd/collectproto/collectproto.go cmd/bnsd/app/codec.proto | wc -l 930 ```
This comment has been minimized.
This comment has been minimized.
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.
Ambitious.
I agree on the desired result.
I will review it tomorrow when the gov / batch stuff is not blocking.
But some solid tests cases is great and feel free to continue on this path. I have not seen tools that do such well, and go is more stable than shell scripts.
|
||
Collect, combine and produce a single protobuf file with all declarations. | ||
|
||
This program reads provided protobuf declarations and: |
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.
At first I thought you found this program. Then I realized you wrote it.
Above and beyond the call of duty!
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.
At first I only typed package main
. The rest just happened 😉
Protobuf is having a trivial syntax to parse. After implementing the cutting of the plugin information I have added more and more. This code is not using regular expressions and is still below 200 lines of a very verbose code! 💪
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 regexp? How dare you...
We no longer use `GOPATH` and `dep` for dependency management. Using Go modules requires changes to how protobuf files are declared and included. - `protofmt` command no longer works as it does not accept `-I` flag - `protodoc` is disabled for now as it requires a single protobuf. This is waiting for #659 - some packages cannot be compiled unless weave top level `codec.proto` is included. Compiler is printing a warning that this file is not used, but it does not compile without it. Weird. fix #568
First comments when trying it out (precurser to code review for most scripts):
message Coin {
option (gogoproto.goproto_stringer) = false; I will look at the code soon, but I think those are good places to start to fix |
Trying to compile the output (with gogoproto... which should be more forgiving that other languages): go run cmd/collectproto/collectproto.go x/cash/codec.proto > foo.proto
protoc --gogoproto_out=. foo.proto I get Top of the output file looks like: syntax proto3;
// definitions from github.com/iov-one/weave/x/cash/codec.proto It should look like: syntax = "proto3";
package bnsd; (I guess the package name can be taken from the last directory name in the path of the input file?) It would be nice for a simple test script there, which tries to generate output from bnsd and compile it (must not be done in the CI, but at least demo the above works) Note that after I fixed the issue with
|
I have fixed I am not sure what to do about the import scope removal. This tool is not using proper parser and lexer but just some tricks to ignore the content. To add functionality to remove package namespace (because it is all in one file) it would take much more time. It would be also a reason to change the current approach to using a tokenizer and lexer. |
cmd/collectproto/collectproto.go
Outdated
@@ -18,7 +18,7 @@ func main() { | |||
|
|||
// Syntax declaration is never rewritten. Initialize combined file with | |||
// one as it is required. | |||
fmt.Fprintln(&out, "syntax proto3;") | |||
fmt.Fprintln(&out, "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.
Can you please test this with protoc??
It is syntax = "proto3";
With regards to the namespacing, there is a quite simple trick with no parser:
Returns:
I will make a push to this branch |
Okay, I pushed a simple fix for the syntax issue.
Got a bunch more errors...
Others may be due to the fact we use the same name (Configuration) in multiple files: Unless you have a clever idea to fix those issues, I am not sure it is worth continuing this road |
What about https://github.com/tallstoat/pbparser ? This (go library) can parse the protobuf files. One idea I had was to mangle all names to be
I would say it is not worth more than a few hours of work. I spent some time and actually combining these is going to be very hard. The readability cleanup is nice though. So let's just simplify the ambitions and we can use this as a nice alternative to flaky shell scripts. |
I thought this through and I would back off from the idea of merging files. I think we should focus on minimal functionality here, without trying to do too much.
This is much smaller scope. Almost exactly what #618 was about. What do you think? |
Yes I agree with the last comment. Just add
So it will make a clean version of our protobuf files. And we can use that in some script (find, etc) but avoid crazy sed/python/perl mangling |
Continued in #666 🤘 |
A new command line tool for collecting all protobuf declarations was
added.
fix #618
From the
README.md
:Collect, combine and produce a single protobuf file with all declarations.
This program reads provided protobuf declarations and:
The combined result is written to stdout.
Example usage:
Please let me know if the output is as expected.