On Mon, Jul 6, 2015 at 11:23 AM, Edgard Lima <[email protected]> wrote:

> // my .proto
> syntax = "proto3";import "google/protobuf/any.proto";
> message CommandListPrinters {}
> message Commands {
>   int32 id = 1;
>   repeated google.protobuf.Any command = 2;}
>
> // my.java
>
> CommandListPrinters commandListPrinters = 
> CommandListPrinters.newBuilder().build();Any any = 
> Any.newBuilder().setValue(commandListPrinters.toByteString()).build();Commands.Builder
>  commandsBuilder = Commands.newBuilder().setId(0);
> commandsBuilder.addCommand(any);Commands commands = 
> commandsBuilder.build();//byte [] ba = commands.toByteArray();        
> Commands cmds2 = Commands.parseFrom(ba);for (Any any2 : 
> cmds2.getCommandList()) {
>     Descriptor fe = any2.getDescriptorForType();
>
>             // This IF is FALSE;
>             if (fe.equals(CommandListPrinters.getDescriptor()) ) {
>                 CommandListPrinters cmdLR = 
> CommandListPrinters.parseFrom(any2.getValue());
>             }
>         }
>
> We will add some helper functions in Java to ease the use of Any pretty
soon. For the time being you will probably use Any like this:

String getTypeUrl(Descriptor descriptor) {
  return "type.googleapis.com/" + descriptor.getFullName();
}

// Create Any message:
Any createAny(Message message) {
  return Any.newBuilder()
      .setTypeId(getTypeUrl(message.getDescriptorForType()))
      .setValue(message.toByteString())
      .build();
}

CommandListPrinters commandListPrinters =
CommandListPrinters.newBuilder().build();
Commands.Builder commandsBuilder = Commands.newBuilder().setId(0);
commandsBuilder.addCommand(createAny(commandListPrinters));
Commands commands = commandsBuilder.build();


for (Any any : commands.getCommandList()) {
  if
(any.getTypeUrl().equals(getTypeUrl(CommandListPrinters.getDescriptor()))) {
    CommandListPrinters cmdLR =
CommandListPrinters.parseFrom(any2.getValue());
    ...
  }
}

With the helper functions we will introduce soon, the code will be
simplified to this:


CommandListPrinters commandListPrinters =
CommandListPrinters.newBuilder().build();
Commands.Builder commandsBuilder = Commands.newBuilder().setId(0);
commandsBuilder.addCommand(*Any.pack(commandListPrinters)*);
Commands commands = commandsBuilder.build();


for (Any any : commands.getCommandList()) {
  if (*any.is <http://any.is>(CommandListPrinters.getClass())*) {
    CommandListPrinters cmdLR = *any.unpack(CommandListPrinters.getClass())*
;
    ...
  }
}

 --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to