Hi,
I do only create one FileDescriptor instance that contains all message
definitions, because they are all constructed dynamically.
For creating the FileDescriptorProtoBuilder I call:
FileDescriptorProto.Builder fileDescriptorProtoBuilder =
FileDescriptorProto.newBuilder();
Then I create all the message protos by calling (here A and B):
DescriptorProto.Builder messageProtoBuilderA =
DescriptorProto.newBuilder();
messageProtoBuilderA.setName( "A" );
DescriptorProto.Builder messageProtoBuilderB =
DescriptorProto.newBuilder();
messageProtoBuilderB.setName( "B" );
If message A references message B using field f I call:
messageProtoBuilderA.addFieldBuilder()
.setName("f")
.setNumber(2)
.setTypeName("B");
Then I add the message protos to the file descriptor:
fileDescriptorProtoBuilder.addMessageType(messageProtoBuilderA);
fileDescriptorProtoBuilder.addMessageType(messageProtoBuilderB);
Finally I create the file descriptor using:
FileDescriptorProto fileDescriptorProto =
fileDescriptorProtoBuilder.build();
FileDescriptor fileDescriptor =
FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]);
Now I can create concrete messages by calling:
Builder messageA =
DynamicMessage.newBuilder( fileDescriptor.findMessageTypeByName("A") );
Builder messageB =
DynamicMessage.newBuilder( fileDescriptor.findMessageTypeByName("B") );
messageA.setField(
fileDescriptor.findMessageTypeByName("A").findFieldByName("f"),
messageB.build() );
I hope I did not add any errors when abstracting the code and that I
understood your problem correctly.
Kind regards,
Robert
On 28 Jul., 14:35, Giancarlo Frison <[email protected]> wrote:
> on the field type descriptor previously create i set name as:
> b2.setName(typename)
>
> in the container class (which has reference the b2 field) I setup the
> field as:
> fb = DescriptorProtos.FieldDescriptorProto.newBuilder()
> .setName(fieldName).setNumber(i+
> +).setLabel(label).setTypeName(typename);
> b1.addField(fb.build());
>
> For creating the b1 descriptor, first I generate the descriptor of b2
> then:
> d1 = b1.build();
> fdp1 =
> DescriptorProtos.FileDescriptorProto.newBuilder().addMessageType(d1).build();
>
> but I get an error because fdp1.getDependenciesCount()==0
> (Descriptors.java:233) Dependencies passed to
> FileDescriptor.buildFrom() don't match those listed in the
> FileDescriptorProto.
>
> thrown here:
> dynamicDescriptor = Descriptors.FileDescriptor.buildFrom(fdp1, fds);
>
> (fds is array of 1, the b2 descriptor)
>
> Why the d1 builder doesn't have the dependency defined adding the
> complex typed field?
>
> Thanks!!
>
> Ps.: Robert, it's very kind of you whether send me the related
> snippet. Thank you
>
> On Jul 27, 8:10 pm, Pherl Liu <[email protected]> wrote:
>
>
>
> > Take a look at the buildFrom() method. You need to provide an array of
> > FileDescriptor as dependencies. If one of your message field is a complex
> > type, you should build that complex type first, and pass the built
> > FileDescriptor object to the buildFrom() in the dependencies array.
>
> > On Wed, Jul 27, 2011 at 7:36 AM, Giancarlo Frison <[email protected]>wrote:
>
> > > if I call setTypeName(messageName), how protobuf match the name with
> > > related Descriptor?
> > > How can I map messageName and the descriptor of the type?
>
> > > On Jul 27, 3:56 pm, Robert <[email protected]> wrote:
> > > > Hi,
>
> > > > you either call setType() for primitive types or setTypeName(String)
> > > > for referenced messages.
> > > > You pass in the name of the message.
>
> > > > Kind regards,
> > > > Robert
>
> > > > On 27 Jul., 10:35, Giancarlo Frison <[email protected]> wrote:
>
> > > > > Thank you very much Pherl!
>
> > > > > An another question. What about not-primitive fields, or message
> > > > > fields?
>
> > > > > when I add fields I do:
>
> > > > > DescriptorProtos.DescriptorProto.Builder desBuilder
> > > > > ....
> > > > > DescriptorProtos.FieldDescriptorProto.Builder fd1Builder =
> > > > > DescriptorProtos.FieldDescriptorProto.newBuilder()
> > > > > .setName(fieldName).setNumber(i+
> > > > > +).setType(type).setLabel(label);
> > > > > desBuilder.addField(fd1Builder.build());
>
> > > > > with message typed field I set type as
> > > > > 'DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE'. How do I
> > > > > describe the field type?
> > > > > I mean, I suppose to describe the field with a inner
> > > > > 'DescriptorProtos.DescriptorProto.Builder', is it correct? How I can
> > > > > do to describe a complex type field?
>
> > > > > Thanks!
>
> > > > > On Jul 26, 8:21 pm, Pherl Liu <[email protected]> wrote:
>
> > > > > > Not sure if I understand your question correctly. You can set the
> > > label in
> > > > > > FieldDescriptorProto:
>
> > > > > > LABEL_OPTIONAL = 1;
>
> > > > > > LABEL_REQUIRED = 2;LABEL_REPEATED = 3;
>
> > > > > > On Tue, Jul 26, 2011 at 12:21 AM, Giancarlo Frison <
> > > [email protected]>wrote:
>
> > > > > > > There is this example that I'm approaching to re-implement
>
> > >http://flori.posterous.com/dynamically-creating-protocol-buffer-objects
>
> > > > > > > a question:
>
> > > > > > > How to handle repeated fields? How can you describe them in the
> > > > > > > fieldDescriptor?
>
> > > > > > > On Jul 18, 5:20 pm, yoave <[email protected]> wrote:
> > > > > > > > Hi all,
>
> > > > > > > > I've got 2 questions:
>
> > > > > > > > 1. how do I generate a Descriptor from a FileDescriptorProto in
> > > Java?
> > > > > > > > In cpp, I'm using DescriptorPool class's 'BuildFile()'
> > > > > > > > functionality.
>
> > > > > > > > 2. how do I generate a Dynamic Message from a Descriptor in
> > > > > > > > Java?
> > > > > > > > In cpp, I'm using DynamicMessageFactory class's
> > > 'GetPrototype()'
> > > > > > > > functionality.
>
> > > > > > > > thanks
>
> > > > > > > --
> > > > > > > You received this message because you are subscribed to the Google
> > > Groups
> > > > > > > "Protocol Buffers" group.
> > > > > > > To post to this group, send email to [email protected].
> > > > > > > To unsubscribe from this group, send email to
> > > > > > > [email protected].
> > > > > > > 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 [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected].
> > > For more options, visit this group at
> > >http://groups.google.com/group/protobuf?hl=en.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en.