将多个字段设置为Oneof的一个元素

发布于 2025-02-05 00:53:38 字数 296 浏览 0 评论 0原文

我想定义一条消息,该消息可以具有2个字段(字段A和字段B)XOR另一个字段(仅字段C)。我看到我可以使用关键字oneof设置XOR,但仅在两个字段之间。 我该如何表达我的需求?

理想情况下,我想要类似( 工作)之类的东西

syntax = "proto3";

message M {
  oneof name {
    {
      string a = 1;
      string b = 2;
    }
    string c = 3;
  }
}

I want to define a message that can have 2 fields (field A AND field B) XOR one other field (field C alone). I saw I can use the keyword oneof to set the XOR, but only between two fields.
how can I express my needs?

Ideally I want something like (not working)

syntax = "proto3";

message M {
  oneof name {
    {
      string a = 1;
      string b = 2;
    }
    string c = 3;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-02-12 00:53:38

我知道的唯一方法是将两个字段Ab放入单独的订阅中,然后您可以将其放入Oneof中:

syntax = "proto3";
    
message A {
    string a = 1;
    string b = 2;
}
    
message M {
    oneof name {
        A a = 1;
        string c = 3;
    }
}

另外可以将所有字段放入m的情况下,而无需Oneof。在评论中描述逻辑,并在应用程序代码中手动检查。

Only way I know of is to put the two fields a and b into separate submessage, which you can then put inside the oneof:

syntax = "proto3";
    
message A {
    string a = 1;
    string b = 2;
}
    
message M {
    oneof name {
        A a = 1;
        string c = 3;
    }
}

Alternatively you can put all fields into M without oneof. Describe the logic in the comments and check it manually in application code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文