简体   繁体   English

Cli 生成协议缓冲区

[英]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 for Empty is empty JSON object {} . // Empty的 JSON 表示是空的 JSON 对象{}
message Empty {}消息空{}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用协议缓冲区编译器为我的服务生成java接口而不是抽象类? - how to generate java interfaces instead of abstract classes for my service using protocol buffers compiler? 协议缓冲区-仅生成OuterClass - Protocol buffers - only generating OuterClasses 在单个表达式中初始化协议缓冲区 - Intialise protocol buffers in single expression LabVIEW 是 gRPC 协议缓冲区支持的语言之一吗? - Is LabVIEW one of the supported languages for protocol buffers for gRPC? Google协议缓冲区-protoc-c和protoc之间的二进制兼容性 - Google protocol buffers - Binary compatibility between protoc-c and protoc 在哪里设置协议的路径以导入标准协议缓冲区 - Where to set the path to the protoc to import standards Protocol Buffers 如何在 Windows 上为 Python 使用协议缓冲区? - How can I use protocol buffers for Python on windows? 将协议缓冲区文件转储到stdout进行调试/调查? - Dumping protocol-buffers file to stdout to debug/investigate? 尝试构建运行 Protocol Buffers 的基于 Alpine 的 Docker 容器时出现“/bin/ash: protoc: not found” - "/bin/ash: protoc: not found" when attempting to build an Alpine-based Docker container running Protocol Buffers 来自Google协议缓冲区protoc命令“ --decode_raw”的“解析输入失败” - “Failed to parse input” from Google protocol buffers protoc command for ``--decode_raw``
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM