I have a golang struct which contains references to some other structs. Is there an automated way to generate the .proto file from the structs ?
For example:
type A struct {
a int
b B
}
type B struct {
c []C
}
type C struct {
x int
}
should generate:
message A, B, C
etc. proto3 is preferred.
https://github.com/kubernetes/kubernetes/tree/master/cmd/libs/go2idl seems to have something related but is undocumented. Any options ?
You can generate the proto files, the marshal/unmarshal and the rest of protobuf stuff for your Go types, the RPC client and server interface and the RPC server implementation for your packages. That is, the whole process. You can generate proto files only using the command line tool provided with proteus.
You can use any text editor to create a . proto file. If you'd like to have syntax highlighting there are also editors that would give you that. I use IntelliJ but a quick Google search found an Eclipse plugin which appears to be free: https://code.google.com/p/protobuf-dt/.
proto file are simple: you add a message for each data structure you want to serialize, then specify a name and a type for each field in the message. In our example, the . proto file that defines the messages is addressbook. proto .
This article is all about the go_package option in the Protocol Buffers (Protobuf). The go_package option defines the import path of the package which will contain all the generated code for this file. The Go package name will be the last path component of the import path — Google doc.
I'm find the package,Generate .proto files from Go source code: proteus (https://github.com/src-d/proteus)
Proteus /proʊtiəs/ is a tool to generate protocol buffers version 3 compatible .proto files from your Go structs, types and functions.
The motivation behind this library is to use Go as a source of truth for your models instead of the other way around and then generating Go code from a .proto file, which does not generate idiomatic code.
Generate protobuf messages
//proteus:generate
type User struct {
Model
Username string
}
type Model struct {
ID int
CreatedAt time.Time
}
This example will generate the following protobuf message.
message User {
int32 id = 1;
google.protobuf.Timestamp created_at = 2;
string username = 3;
}
Install
go get -v gopkg.in/src-d/proteus.v1/...
Requirements
There are two requirements for the full process.
protoc binary installed on your path
go get -u github.com/gogo/protobuf/...
Usage
You can generate the proto files, the marshal/unmarshal and the rest of protobuf stuff for your Go types, the RPC client and server interface and the RPC server implementation for your packages. That is, the whole process.
proteus -f /path/to/protos/folder \
-p my/go/package \
-p my/other/go/package
You can generate proto files only using the command line tool provided with proteus.
proteus proto -f /path/to/output/folder \
-p my/go/package \
-p my/other/go/package
--verbose
You can also only generate gRPC server implementations for your packages.
proteus rpc -p my/go/package \
-p my/other/go/package
NOTE: Of course, if the defaults don't suit your needs, until proteus is extensible via plugins, you can hack together your own generator command using the provided components. Check out the godoc documentation of the package.
If anyone just need to generate pure protobuf messages without any gogo, mogo, blogo syntax you can use https://github.com/anjmao/go2proto which I wrote recently. It's super simple and just generates proto messages from given go source package containing structs. Also it supports go modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With