从golang结构生成原型文件 [英] Generate proto file from golang struct

查看:62
本文介绍了从golang结构生成原型文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个golang结构,其中包含对其他一些结构的引用.有没有一种自动的方法可以从结构生成.proto文件?

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 ?

例如:

type A struct {
 a int
 b B
}

type B struct {
 c []C
}

type C struct {
 x int
}

应生成:

消息A,B,C 等.首选proto3.

message A, B, C etc. proto3 is preferred.

https://github.com/kubernetes/kubernetes/tree/master/cmd/libs/go2idl 似乎有相关之处,但未记录在案.有什么选择吗?

https://github.com/kubernetes/kubernetes/tree/master/cmd/libs/go2idl seems to have something related but is undocumented. Any options ?

推荐答案

我找到了该包,可以从Go源代码生成.proto文件: proteus(https://github.com/src-d/proteus)

I'm find the package,Generate .proto files from Go source code: proteus (https://github.com/src-d/proteus)

Proteus/proʊtiəs/是从Go的结构,类型和函数中生成与协议缓冲区版本3兼容的.proto文件的工具.

Proteus /proʊtiəs/ is a tool to generate protocol buffers version 3 compatible .proto files from your Go structs, types and functions.

该库背后的动机是将Go用作模型的真实来源,而不是相反,然后从.proto文件生成Go代码,该文件不会生成惯用代码.

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.

生成protobuf消息

  //proteus:generate
  type User struct {
        Model
        Username string
  }

  type Model struct {
        ID int
        CreatedAt time.Time
  }

此示例将生成以下protobuf消息.

This example will generate the following protobuf message.

  message User {
          int32 id = 1;
          google.protobuf.Timestamp created_at = 2;
          string username = 3;
  }

安装

 go get -v gopkg.in/src-d/proteus.v1/...

要求

整个过程有两个要求.

 protoc binary installed on your path
 go get -u github.com/gogo/protobuf/...

用法

您可以为Go类型,包的RPC客户端和服务器接口以及RPC服务器实现生成proto文件,元数据/解组和其余protobuf东西.也就是说,整个过程.

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

您只能使用proteus随附的命令行工具来生成proto文件.

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

您还只能为您的软件包生成gRPC服务器实现.

You can also only generate gRPC server implementations for your packages.

  proteus rpc -p my/go/package \
    -p my/other/go/package

注意:当然,如果默认设置不符合您的需求,则在可以通过插件扩展proteus之前,您可以使用提供的组件来修改自己的生成器命令.请查看软件包的godoc文档.

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.

这篇关于从golang结构生成原型文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆