English 中文(简体)
采取什么适当方式使多吗?
原标题:what s the right way to do polymorphism with protocol buffers?

我试图长期地把ava瓦强等级的物体连成一nch,我想利用礼宾缓冲来这样做,因为其简便、性能和升级方便。 然而,它们没有为多变主义提供大量支持。 现在,我处理该问题的方式是,有一个“一个信息来统治所有”的解决办法,它有一个必要的地圈,使我能够通过思考来立即进行正确的类型,然后,我可以把所有其他可能类别的选任领域编成序号,其中只有一个(根据里面的价值)。 是否有更好的办法处理多吗?

问题回答

In proto3 the extend keyword has been replaced. From the docs: If you are already familiar with proto2 syntax, the Any type replaces extensions.

syntax = "proto3";

import "google/protobuf/any.proto";

message Foo {
  google.protobuf.Any bar = 1;
}

但需注意:<代码>Any基本上是一个tes。 大部分时间最好使用Oneof:

syntax = "proto3";

message A {
    string a = 1;
}

message B {
    string b = 1;
}

message Foo {
  oneof bar {
    A a = 1;
    B b = 2;
  }
}

采用多种形态的方法有几个。 我试图把所有这些内容都包括在内:

我的首选方法使用extensions:

message Animal
{
    extensions 100 to max;

    enum Type
    {
        Cat = 1;
        Dog = 2;
    }

    required Type type = 1;
}

message Cat
{
    extend Animal
    {
        required Cat animal = 100; // Unique Animal extension number
    }

    // These fields can use the full number range.
    optional bool declawed = 1;
}

message Dog
{
    extend Animal
    {
        required Dog animal = 101; // Unique Animal extension number
    }

    // These fields can use the full number range.
    optional uint32 bones_buried = 1;
}

若若能解决问题是正确的,是行之有效的,但顺便提一下(对我来说)。 但《议定书》是十分简单的,因此,你可以做这样的事情:

enum Type {
    FOO = 0;
    BAR = 1;
  }

message Foo {
  required Type type = 1;
}

message Bar {
  required Type type = 1;
  required string text = 2;
}

基本信息 律师传达Foo(当然从实用的方面)的信息。 Java的执行情况也很简单:

Bar bar = Bar.newBuilder().setType(Type.BAR).setText("example").build();
byte[] data = bar.toByteArray();

----

Foo foo = Foo.parseFrom(data);
if(foo.getType() == Type.BAR){
   Bar bar = Bar.parseFrom(data);
   System.out.println(bar.getText());
}

我知道,这不是一个棘手的解决办法,而是简单和合乎逻辑的。

Extensions and Nested Expansions,以稍微更清洁的方式这样做。

您是否考虑使用extensions? 您的田野可以确定使用类型,然后只装载适当的延伸。 如果你知道你的田地是相互排斥的,那么你就可以在单独的展期之间重新利用田地。

你们必须处理这一天空,因为礼宾缓冲器的设计是自我描述,超出单纯的价值观清单。 这在google技术网页上有所触及。

对我来说,“Łukasz Marciniak”的答案稍有改善。

律师 Foo, 简单写:

message Bar {
   optional Foo foo = 1;
   optional double aDouble = 2;
}
message Foo {
   optional string aString = 1;
}

因此,如果Foo只变换了Foo信息。





相关问题
Serializing a List of objects using Protobuf-net

I ve been looking to do some binary serialization to file and protobuf-net seems like a well-performing alternative. I m a bit stuck in getting started though. Since I want to decouple the definition ...

How to use protocol buffers?

Could someone please help and tell me how to use protocol buffers. Actually I want to exchange data through sockets between a program running on unix and anoother running on windows in order to run ...

Using Protocol buffer as general Data object?

We re introducing protocol buffers as the new transport for some back end RPC services. Because there s resistance to manually shuttling data between different forms of similar objects, I can forsee ...

Go integration with Protocol Buffers?

After a quick look at the documentation, I immediately started to think about integration with existing languages and applications and was wondering whether support would be provided for Protocol ...

Android and Protocol Buffers

I am writing an Android application that would both store data and communicate with a server using protocol buffers. However, the stock implementation of protocol buffers compiled with the LITE flag (...

热门标签