Re: [protobuf] he simple code bellow doesn't work. Is it the best way to use Any ? I'm just trying to read from/write to Any and identify the type stored by Any.
Thanks, works like a charm! On Monday, July 6, 2015 at 6:13:33 PM UTC-3, Feng Xiao wrote: On Mon, Jul 6, 2015 at 11:23 AM, Edgard Lima edgar...@gmail.com javascript: 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 protobuf+u...@googlegroups.com javascript:. To post to this group, send email to prot...@googlegroups.com javascript:. 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
Re: [protobuf] he simple code bellow doesn't work. Is it the best way to use Any ? I'm just trying to read from/write to Any and identify the type stored by Any.
On Mon, Jul 6, 2015 at 11:23 AM, Edgard Lima edgard.l...@gmail.com 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. 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 protobuf+unsubscr...@googlegroups.com. To post to this group, send email to protobuf@googlegroups.com. Visit this group at http://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.