Generate proto file from golang struct

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.