ProtobufPath { public static Function<Message, ?> protobufPath( Descriptors.Descriptor messageDescriptor, String pathString) { List<PathFragment> fields = ProtobufPathParser.parse(pathString); List<PathFragmentDescriptor> pathFragments = ProtobufPathCompiler.compile(messageDescriptor, fields); return new ProtobufDynamicMessageLens(pathFragments); }  static Function<Message, ?> protobufPath(
      Descriptors.Descriptor messageDescriptor, String pathString);  }
@Test public void exampleUsage() { Message message = SimpleMessage.newBuilder().setName("bob").build(); Function<Message, ?> getter = protobufPath(message.getDescriptorForType(), "$.name"); assertThat(getter.apply(message), is("bob")); }
@Test public void repeatedMessage() { Message message = TestProtos.RepeatedMessage.newBuilder() .addSimpleMessage(SimpleMessage.newBuilder().setName("bruce")) .addSimpleMessage(SimpleMessage.newBuilder().setName("lee")) .build(); Function<Message, ?> getter = protobufPath(message.getDescriptorForType(), "$.simple_message[1].name"); assertThat(getter.apply(message), is("lee")); }
@Test public void nestedMessage() { Message message = NestedMessage.newBuilder().setFoo(NestedMessage.Foo.newBuilder().setName("lee")).build(); Function<Message, ?> getter = protobufPath(message.getDescriptorForType(), "$.foo.name"); assertThat(getter.apply(message), is("lee")); }
@Test public void messageWithEnum() { TestProtos.MessageWithEnum message = TestProtos.MessageWithEnum.newBuilder().setLetter(TestProtos.Letter.B).build(); Function<Message, ?> getter = protobufPath(message.getDescriptorForType(), "$.letter"); Object apply = getter.apply(message); assertThat(apply, is(TestProtos.Letter.B.getValueDescriptor())); }
@Test public void importedMessage() { Message message = TestProtos.ImportedMessage.newBuilder().setImported(Any.getDefaultInstance()).build(); Function<Message, ?> getter = protobufPath(message.getDescriptorForType(), "$.imported"); assertThat(getter.apply(message), is(Any.getDefaultInstance())); }
@Test public void oneOfMessage() { Message message = TestProtos.OneOfMessage.newBuilder().setBar(1234).build(); Function<Message, ?> getter = protobufPath(message.getDescriptorForType(), "$.bar"); assertThat(getter.apply(message), is(1234L)); }