The Builder interface has a method setField() which takes a FieldDescriptor
and a value.  You can pass the FieldDescriptor for an extension to set an
extension, and pass the message object as the value.  You can get the
FieldDescriptor from ExtensionRegistry or from
GeneratedExtension.getDescriptor().

On Thu, Jul 30, 2009 at 3:29 AM, Brice Figureau
<brice...@daysofwonder.com<brice%2...@daysofwonder.com>
> wrote:

>
> Hi,
>
> I'm just starting using Protocol Buffers for Java for a network
> protocol.
>
> Here is how I designed my proto file:
>
> request.proto:
> message Message {
>        enum Type {
>                AUTH = 100;
>                  CHAT = 101;
>                ...
>        }
>
>        required Type type = 1;
>        optional int32 id = 2;
>
>        extensions 100 to max;
> }
>
> message AuthRequest {
>        extend Message {
>                optional AuthRequest auth_request = 100;
>        }
>        required string name = 1;
>        required string pass = 2;
> }
>
> message ChatRequest {
>        extend Message {
>                optional ChatRequest chat_request = 101;
>        }
>        required int32 player_id = 1;
>        optional string message_string = 2;
> }
>
> This is based on the Union Types technique described in the
> documentation.
>
> Now, all is well, I can do:
> ChatRequest chat =
> ChatRequest.newBuilder().setMessage(msg).setPlayerId(id).build();
> Message request =
> Message.newBuilder().setType(Type.CHAT).setExtension(ChatRequest.chatRequest,
> fChatRequest).build();
> and then serialize request to the wire.
>
> What I'm trying to achieve now is to generalize this operation for all
> my defined request types, using the descriptors and registry extension:
>
> ExtensionRegistry registry = ExtensionRegistry.newInstance();
> Request.registerAllExtensions(registry);
> ...
>
> Then I can create "generically" a builder for a given type with:
>
> public Builder getBuilder(Type type)
> {
>  ExtensionRegistry.ExtensionInfo info =
> registry.findExtensionByNumber(Message.getDescriptor(), type.getNumber());
>  return info.defaultInstance.newBuilderForType();
> }
> And on client call site:
> ChatRequest chat = (ChatRequest.Builder)getBuilder(Type.CHAT);
> chat.setPlayerId(id)...
>
> Now, I'm trying to generically associate this ChatRequest to the correct
> Extension field in the Message type, but unfortunately I can't find a
> way of doing that, because setExtension() requires a
> GeneratedMessage.GeneratedExtension which I can't have generically (ie
> the extension registry even though generated with this information,
> doesn't keep it).
> What's worse is tha the Extension Registry has all the information, but
> I can't recreate this specific generic type or add the extension from
> the Registry information.
>
> First question: how can I solve this?
> Second question: if it's not solvable, then that means I'm not using PB
> as it was intended. In this case, how should I use it?
>
> Thanks,
> --
> Brice Figureau
> My Blog: http://www.masterzen.fr/
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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