com.google.protobuf.UnknownFieldSet.Field Java Examples

The following examples show how to use com.google.protobuf.UnknownFieldSet.Field. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Bootstrap.java    From krpc with Apache License 2.0 5 votes vote down vote up
private void loadProtoFile(RpcApp app, FileDescriptor base, String file) throws IOException, DescriptorValidationException {

        InputStream in = getResource(file);
        FileDescriptorSet descriptorSet = FileDescriptorSet.parseFrom(in);
        in.close();

        Map<String, Descriptor> descriptors = new HashMap<>();
        for (FileDescriptorProto fdp : descriptorSet.getFileList()) {
            FileDescriptor fd = FileDescriptor.buildFrom(fdp, new FileDescriptor[]{base});

            for (Descriptor descriptor : fd.getMessageTypes()) {
                String className = descriptor.getName();
                descriptors.put(className, descriptor);
            }
            for (ServiceDescriptor svr : fd.getServices()) {
                Field f = svr.getOptions().getUnknownFields().getField(KrpcExt.SERVICEID_FIELD_NUMBER);
                String serviceName = svr.getName();
                int serviceId = f.getVarintList().get(0).intValue();
                for (MethodDescriptor m : svr.getMethods()) {
                    String msgName = m.getName();
                    Field f2 = m.getOptions().getUnknownFields().getField(KrpcExt.MSGID_FIELD_NUMBER);
                    int msgId = f2.getVarintList().get(0).intValue();
                    log.info(String.format("dynamic proto resource loaded, serviceId=%d,msgId=%d,serviceName=%s,msgName=%s", serviceId, msgId, serviceName, msgName));
                    Descriptor reqDesc = m.getInputType();
                    Descriptor resDesc = m.getOutputType();
                    app.serviceMetas.addDynamic(serviceId, msgId, reqDesc, resDesc, serviceName, msgName);
                }
            }
        }
    }
 
Example #2
Source File: TestLegacyProtobufSerializer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Test
public void testParse() throws InvalidProtocolBufferException {
  // Creating a protobuf message as it would be generated by protostuff for the following message
  // SearchQuery -> Boolean -> [ SearchQuery -> Term, SearchQuery -> Term ]

  final UnknownFieldSet term1 = UnknownFieldSet.newBuilder()
      .addField(SearchQuery.Term.FIELD_FIELD_NUMBER, Field.newBuilder().addLengthDelimited(ByteString.copyFromUtf8("foo")).build())
      .addField(SearchQuery.Term.VALUE_FIELD_NUMBER, Field.newBuilder().addLengthDelimited(ByteString.copyFromUtf8("bar")).build())
      .build();
  final UnknownFieldSet sq1 = UnknownFieldSet.newBuilder()
      .addField(SearchQuery.TYPE_FIELD_NUMBER, Field.newBuilder().addVarint(SearchQuery.Type.TERM_VALUE).build())
      .addField(SearchQuery.TERM_FIELD_NUMBER, Field.newBuilder().addGroup(term1).build())
      .build();

  final UnknownFieldSet term2 = UnknownFieldSet.newBuilder()
      .addField(SearchQuery.Term.FIELD_FIELD_NUMBER, Field.newBuilder().addLengthDelimited(ByteString.copyFromUtf8("foo")).build())
      .addField(SearchQuery.Term.VALUE_FIELD_NUMBER, Field.newBuilder().addLengthDelimited(ByteString.copyFromUtf8("baz")).build())
      .build();
  final UnknownFieldSet sq2 = UnknownFieldSet.newBuilder()
      .addField(SearchQuery.TYPE_FIELD_NUMBER, Field.newBuilder().addVarint(SearchQuery.Type.TERM_VALUE).build())
      .addField(SearchQuery.TERM_FIELD_NUMBER, Field.newBuilder().addGroup(term2).build())
      .build();

  final UnknownFieldSet bool = UnknownFieldSet.newBuilder()
      .addField(SearchQuery.Boolean.OP_FIELD_NUMBER, Field.newBuilder().addVarint(SearchQuery.BooleanOp.AND_VALUE).build())
      .addField(SearchQuery.Boolean.CLAUSES_FIELD_NUMBER, Field.newBuilder().addGroup(sq1).addGroup(sq2).build())
      .build();

  final SearchQuery query = SearchQuery.newBuilder()
      .setType(SearchQuery.Type.BOOLEAN)
      .setUnknownFields(UnknownFieldSet.newBuilder().addField(SearchQuery.BOOLEAN_FIELD_NUMBER, Field.newBuilder().addGroup(bool).build()).build())
      .build();

  final SearchQuery expected = SearchQuery.newBuilder()
      .setType(SearchQuery.Type.BOOLEAN)
      .setBoolean(
          SearchQuery.Boolean.newBuilder()
            .setOp(SearchQuery.BooleanOp.AND)
            .addClauses(SearchQuery.newBuilder()
                .setType(SearchQuery.Type.TERM).setTerm(Term.newBuilder().setField("foo").setValue("bar")))
            .addClauses(SearchQuery.newBuilder()
                .setType(SearchQuery.Type.TERM).setTerm(Term.newBuilder().setField("foo").setValue("baz")))
            )
      .build();

  assertThat(LegacyProtobufSerializer.parseFrom(SearchQuery.PARSER, query.toByteString()), is(equalTo(expected)));
}