简体   繁体   English

协议缓冲区将 oneof 消息拆分到不同的文件

[英]Protocol buffer split oneof message to a different file

I use nanopb and a .proto file that looks like this (simplified):我使用 nanopb 和一个看起来像这样(简化)的 .proto 文件:

syntax = "proto3";

message Type1
{
    bool a = 1;
}

message Type2
{
    int32 b = 1;
}

message Message
{
    int32 id = 1;
    oneof oneMessage {
        Type1 messageType1 = 2;
        Type2 messageType2 = 3;
    }
}

I now need to split the oneMessage to a different .proto file and import it.我现在需要将oneMessage拆分为不同的 .proto 文件并导入它。 I can do something like: file a.proto:我可以这样做:文件 a.proto:

syntax = "proto3";
import "b.proto"
message Message
{
    int32 id = 1;
    oneofTypeMessage aMessage = 2;
}

file b.proto文件 b.proto

syntax = "proto3";

message Type1
{
    bool a = 1;
}

message Type2
{
    int32 b = 1;
}

message oneofTypeMessage
{
    oneof oneMessage {
        Type1 messageType1 = 2;
        Type2 messageType2 = 3;
    }
}

But this means I can't get to my message types with message.messageType1 , triggering lots of changes in the existing source code.但这意味着我无法使用message.messageType1获取我的消息类型,从而触发现有源代码中的大量更改。 How can I split the oneof part to another file without changing the way I access to it?如何在不改变访问方式的情况下将 oneof 部分拆分为另一个文件? I've tried declaring Message in both files but it is not permitted.我已经尝试在两个文件中声明Message但这是不允许的。

No. oneMessage is not a message type , and to import something from another file, it must be a message type.不, oneMessage不是消息类型,要从另一个文件导入某些内容,它必须是消息类型。 Adding a new oneofTypeMessage would add an additional node in the graph, and would not be data-compatible with the original data.添加一个新的oneofTypeMessage将在图中添加一个额外的节点,并且不会与原始数据数据兼容。

You can move Type1 and Type2 , but the oneof must stay as-was;您可以移动Type1Type2 ,但oneof必须保持原样; so: this is valid:所以:这是有效的:

file a.proto :文件a.proto

syntax = "proto3";
import "b.proto"
message Message
{
    int32 id = 1;
    oneof oneMessage {
        Type1 messageType1 = 2;
        Type2 messageType2 = 3;
    }
}

file b.proto :文件b.proto

syntax = "proto3";

message Type1
{
    bool a = 1;
}

message Type2
{
    int32 b = 1;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM