有没有办法在 protobuf (proto2) 中创建类型别名?

Is there a way to create a type alias in protobuf (proto2)?

是否可以为 protobuf 的标量类型创建别名?

例如,我想用 Sequence 代替 string,即使它们是等效的二进制文件。

我的直接目标是使文档(使用 protoc-gen-doc 生成)更易于理解。

理想情况下,此类型将以支持类型检查的语言表示,但这不是必需的。

好吧,这个答案会有点乏味但是:

不,我不知道此类功能已经存在或正在计划中。

您可以通过制作仅包含单个字段的子消息来模拟它,但这会改变二进制表示形式。

虽然你不是第一个问这个问题的人:

[更新:2017 年 8 月。适用于对 protoc-gen-bug 的完整 Go 重写,目前 1.0.0-rc]

我没有类型别名的答案,但确实有一个答案:

My immediate goal is to make documentation (generated with protoc-gen-doc) more readily understandable.

我还想指出,protoc-gen-doc 已完全用 Go 重写,现在使用 Docker 代替 apt-get

我已经创建了一些 htmlmarkdown 演示,演示如何在您的评论中使用内联富格式,例如添加链接、代码片段、表格、粗体、斜体等。

并说明如何使用 TravisCI[=29自动生成并发布到 Github 页面(gh-pages 分支) =]

仍有一些小错误需要解决(2017 年 8 月)才能投入生产。

查看演示的 + 描述:

.

例如有 markdown 内联评论像这样:

/**
* The **SLEEP** format is designed by the [Dat Project](https://datproject.org) 
* to allow for sparse replication, meaning you can efficiently download
* only the metadata and data required to resolve a single byte region 
* of a single file, which makes Dat suitable for a wide variety of streaming,
* _real-time_ and large dataset use cases.
*
* To take advantage of this, Dat includes a _network protocol_. It is message
* based and stateless, making it possible to implement on a variety of network
* transport protocols including UDP and TCP.
*
* Both metadata and content registers in **SLEEP** share the exact same
* replication protocol.
*
* Individual messages are encoded using Protocol Buffers and there are ten
* message types in total.
*
* ### Wire Protocol
*
* Over the wire messages are packed in the following lightweight
* container format:
*
* ```
* <varint - length of rest of message>
*   <varint - header>
*   <message>
* ```
*
* The `header` value is a single `varint` that has two pieces of information,
* the integer type that declares a 4-bit message type (used below), and a
* channel identifier, 0 for metadata and 1 for content.
*
* To generate this varint, you bitshift the 4-bit type integer onto the end of
* the channel identifier, e.g. channel << 4 | <4-bit-type>.
*
* ### Using in NodeJS
*
* The `protocol-bufers` package offers an intuitive javascript API, so you need
* not worry about low-level protocol concepts.
* 
* This example demonstrates how you create a feed and start streaming:
*
* ```javascript
*   var protocol = require('hypercore-protocol')
*   var stream = protocol()
*   
*   // open a feed specified by a 32 byte key
*   var feed = stream.feed(Buffer('deadbeefdeadbeefdeadbeefdeadbeef'))
*   
*   feed.request({block: 42})
*   feed.on('data', function (message) {
*     console.log(message) // contains message.index and message.value
*   })
*   
*   stream.pipe(anotherStream).pipe(stream)
* ```
*/

.

将导致 类似于此的输出:


HypercoreSpecV1_md.proto

The SLEEP format is designed by the Dat Project to allow for sparse replication, meaning you can efficiently download only the metadata and data required to resolve a single byte region of a single file, which makes Dat suitable for a wide variety of streaming, real-time and large dataset use cases.

To take advantage of this, Dat includes a network protocol. It is message based and stateless, making it possible to implement on a variety of network transport protocols including UDP and TCP.

Both metadata and content registers in SLEEP share the exact same replication protocol.

Individual messages are encoded using Protocol Buffers and there are ten message types in total.

Wire Protocol

Over the wire messages are packed in the following lightweight container format:

<varint - length of rest of message>
  <varint - header>
  <message>

The header value is a single varint that has two pieces of information, the integer type that declares a 4-bit message type (used below), and a channel identifier, 0 for metadata and 1 for content.

To generate this varint, you bitshift the 4-bit type integer onto the end of the channel identifier, e.g. channel << 4 | <4-bit-type>.

Using in NodeJS

The protocol-bufers package offers an intuitive javascript API, so you need not worry about low-level protocol concepts.

This example demonstrates how you create a feed and start streaming:

var protocol = require('hypercore-protocol')
var stream = protocol()

// open a feed specified by a 32 byte key
var feed = stream.feed(Buffer('deadbeefdeadbeefdeadbeefdeadbeef'))

feed.request({block: 42})
feed.on('data', function (message) {
  console.log(message) // contains message.index and message.value
})

stream.pipe(anotherStream).pipe(stream)

(注意: hypercore protocol is one of the core specifications of the Dat Project 模块生态系统,用于创建去中心化的点对点应用程序设计。我使用了他们的.proto 文件来演示概念)