protocol-buffers - 如何包含具有相互依赖性的 .proto 文件

标签 protocol-buffers protoc proto

我有两个 .proto 文件,其中有两个相互依赖的包。

a.proto

syntax = "proto3";
import "b.proto";

package a;

message cert {
    string filename = 1;
    uint32 length = 2;
}

enum state {
    UP = 1;
    DOWN = 2;
}

message events {
    repeated b.event curevent = 1;
    uint32 val = 2;
}

b.proto

syntax = "proto3";
import "a.proto";

package b;

message event {
     a.cert certificate = 1;
     a.state curstate = 2;
}

当我尝试生成 cpp 文件时,出现以下错误

# protoc -I. --cpp_out=. b.原型(prototype) b.proto:文件递归导入自身:b.proto -> a.proto -> b.proto

如何实现?

注意:使用的协议(protocol)版本是libprotoc 3.3.0

最佳答案

proto 编译器不会让你包含循环依赖。您将必须组织您的代码,以便没有任何递归导入。上面示例代码的一种组织方式可能是:

一个.proto

syntax = "proto3";

package a;

message cert {
    string filename = 1;
    uint32 length = 2;
}

enum state {
    UNDEFINED = 0;
    UP = 1;
    DOWN = 2;
}

b.proto

syntax = "proto3";
import "a.proto";

package b;

message event {
    a.cert certificate = 1;
    a.state curstate = 2;
}

message events {
    repeated event curevent = 1;
    uint32 val = 2;
}

您的events 类型不使用a.proto 中的任何内容,还使用b.proto< 中的event 类型。将它移动到 b.proto 是有意义的。

关于protocol-buffers - 如何包含具有相互依赖性的 .proto 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45378889/

相关文章:

android - 如何使用GreenDao直接保存protocol buffer类

protocol-buffers - Protocol Buffer : How to use the --encode command on console

performance - R:通过引用传递数据帧

Google 在 Golang 中的 Protocol Buffer

c++ - Boost.Asio 与谷歌 Protocol Buffer

java - Avro 与 Protobuf 性能对比

java - 导入 "google/protobuf/timestamp.proto"未找到或有错误

plugins - 如何创建一个 protobuf go plugin 插件

python - 如何导入protobuf模块

python - 考虑字段中的零值的字节字符串反序列化