douqi3064 2016-08-02 15:23
浏览 80
已采纳

从golang结构生成原型文件

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 ?

  • 写回答

2条回答 默认 最新

  • dpn4073 2017-08-11 00:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效