[英]Cli to generate protocol buffers
In my test.proto file I have as below, ie no field inside message HTML, how do I set a value for the _html field of message Page?在我的 test.proto 文件中,我有如下内容,即消息 HTML 中没有字段,如何为消息页面的 _html 字段设置值?
message HTML{
enum Type {
NO_TYPE = 0;
HTML5 = 1;
XHTML = 2;
}
}
message Page{
optional HTML _html = 1;
}
How can I set the values?如何设置这些值?
echo _html : **???** | protoc --encode=Page test.proto > binary.data
https://medium.com/@at_ishikawa/cli-to-generate-protocol-buffers-c2cfdf633dce https://medium.com/@at_ishikawa/cli-to-generate-protocol-buffers-c2cfdf633dce
How can I set the values?如何设置这些值?
you can't when it is defined like you have shown.你不能像你展示的那样定义它。 The message HTML
only contains a definition of an enum but no actual fields.消息HTML
仅包含枚举的定义,但不包含实际字段。 You can either add a field in the message HTML
, or define the type in Page
as HTML.Type
instead of just HTML
:您可以在消息HTML
添加一个字段,或者将Page
的类型定义为HTML.Type
而不仅仅是HTML
:
test.proto:测试.proto:
message HTML{
enum Type {
NO_TYPE = 0;
HTML5 = 1;
XHTML = 2;
}
}
message Page{
optional HTML.Type _html = 1;
}
and then create a file test.txt:然后创建一个文件 test.txt:
_html: HTML5
you can create a binary file like this:你可以像这样创建一个二进制文件:
protoc --experimental_allow_proto3_optional --encode Page test.proto < test.txt > binary.data
and decode it :并解码它:
protoc --experimental_allow_proto3_optional --decode Page test.proto < binary.data
The last command prints:最后一条命令打印:
_html: HTML5
Apart from the enum definition, the HTML
message is an Empty
message , which even exists as a predefined message :除了枚举定义之外, HTML
消息是一个Empty
message ,它甚至作为预定义消息存在:
// A generic empty message that you can re-use to avoid defining duplicated // 可以重复使用的通用空消息,以避免定义重复
// empty messages in your APIs. // API 中的空消息。 A typical example is to use it as the request一个典型的例子是将其用作请求
// or the response type of an API method. // 或 API 方法的响应类型。
// The JSON representation forEmpty
is empty JSON object{}
. //Empty
的 JSON 表示是空的 JSON 对象{}
。
message Empty {}消息空{}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.