Is there a best practice for this use case?

Here are some options.

// Duplicate every field (sounds like you're doing this)
message FooRequest {
  required string prompt = 1;
  required int64 timeout = 2;
  required Foo foo = 3;
}

message BarRequest {
  required string prompt = 1;
  required int64 timeout = 2;
  required Bar bar = 3;
}

// Client code--something like this anyway
Foo foo = Foo.newBuilder().build();
FooRequest fooRequest = FooRequest.newBuilder().setPrompt("").setTimeout(0).
setFoo(foo).build();
Bar bar = Bar.newBuilder().build();
BarRequest barRequest = BarRequest.newBuilder().setPrompt("").setTimeout(0).
setBar(bar).build();

// Share a base message and extend it
message BaseRequest {
  required string prompt = 1;
  required int64 timeout = 2;
  extensions 1000 to max; // reserved for extensions
}
message BazRequest {
  extends BaseRequest {
    optional BazRequest baz = 1000;
  }
  required Baz baz = 1;
}

// Client Code
Baz baz = Baz.newBuilder().build();
BazRequest bazRequest = BazRequest.newBuilder().setBaz(baz).build();
BaseRequest request =
BaseRequest.newBuilder().setPrompt("").setTimeout(0).setExtension(BazRequest.baz,
bazRequest).build();
// so given a BaseRequest, how do we know if it's is a Baz or something
else? -- lots of hasExtension() calls? that doesn't sound good and you have
to keep track of an ExtensionRegistry if you're using a generic
Reader/Writer.
// baz is a base? not so much
// baz has a base? sounds right
// this approach doesn't seem right

// Composition
message CommonRequest {
  required string prompt = 1;
  required int64 timeout = 2;
}
message QuxRequest {
  required CommonRequest common = 1;
  required Qux qux = 2;
}

// Client Code
Qux baz = Qux.newBuilder().build();
CommonRequest.Builder crb =
CommonRequest.newBuilder().setPrompt("").setTimeout(0);
QuxRequest bazRequest
= QuxRequest.newBuilder().setCommon(crb).setQux(qux).build();
// the challenge here would be determining how the common message is
defined. If you have dozens of requests that match on some fields here, some
there... it may get complicated.

YMMV,
-- TJ


On Fri, Jan 28, 2011 at 12:59 PM, koert <koertkuip...@gmail.com> wrote:

> i have several proto message definitions that all share the first 4
> fields. its as if they are all subclasses of one protobuf message
> format.
> in java can i create the FieldDescriptors for these 4 fields once and
> use them for the getters and setters of all these message classes? it
> would save me a lot in terms of logic and maybe also lead to somewhat
> bettter performance.
> best koert
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to protobuf@googlegroups.com.
> To unsubscribe from this group, send email to
> protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to