com.google.protobuf.Message.Builder Java Examples

The following examples show how to use com.google.protobuf.Message.Builder. 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: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void parserSupportAliasEnums() throws Exception {
  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
  String json = "{\n" + "  \"optionalAliasedEnum\": \"QUX\"\n" + "}";
  mergeFromJson(json, builder);
  assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);

  builder = TestAllTypes.newBuilder();
  json = "{\n" + "  \"optionalAliasedEnum\": \"qux\"\n" + "}";
  mergeFromJson(json, builder);
  assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);

  builder = TestAllTypes.newBuilder();
  json = "{\n" + "  \"optionalAliasedEnum\": \"bAz\"\n" + "}";
  mergeFromJson(json, builder);
  assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
}
 
Example #2
Source File: Server.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private RpcSaslProto buildNegotiateResponse(List<AuthMethod> authMethods)
    throws IOException {
  RpcSaslProto.Builder negotiateBuilder = RpcSaslProto.newBuilder();
  if (authMethods.contains(AuthMethod.SIMPLE) && authMethods.size() == 1) {
    // SIMPLE-only servers return success in response to negotiate
    negotiateBuilder.setState(SaslState.SUCCESS);
  } else {
    negotiateBuilder.setState(SaslState.NEGOTIATE);
    for (AuthMethod authMethod : authMethods) {
      SaslRpcServer saslRpcServer = new SaslRpcServer(authMethod);      
      SaslAuth.Builder builder = negotiateBuilder.addAuthsBuilder()
          .setMethod(authMethod.toString())
          .setMechanism(saslRpcServer.mechanism);
      if (saslRpcServer.protocol != null) {
        builder.setProtocol(saslRpcServer.protocol);
      }
      if (saslRpcServer.serverId != null) {
        builder.setServerId(saslRpcServer.serverId);
      }
    }
  }
  return negotiateBuilder.build();
}
 
Example #3
Source File: DefaultRpcDataConverter.java    From krpc with Apache License 2.0 6 votes vote down vote up
Map<String, Object> getCtxMap(Builder b, WebContextData ctx, DefaultWebReq req) {

        Map<String, Object> m = null;

        for (FieldDescriptor field : b.getDescriptorForType().getFields()) {
            String name = field.getName();
            Object value = getValue(ctx, req, name);
            if (value != null) {
                if (m == null) m = new HashMap<>();
                m.put(name, value);
                req.getParameters().putIfAbsent(name,value);
            }
        }

        return m;
    }
 
Example #4
Source File: VendorExtensionProtoConverter.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Message> T convertJsonToProto(T prototype, String json, String extensionName) {
  try {
    Builder builder = prototype.newBuilderForType();
    JsonFormat.parser().merge(json, builder);
    return (T) builder.build();
  } catch (InvalidProtocolBufferException ex) {
    diagCollector.addDiag(
        Diag.error(
            new SimpleLocation(extensionName),
            "Extension %s cannot be converted into proto type %s. Details: %s",
            extensionName,
            prototype.getDescriptorForType().getFullName(),
            ex.getMessage()));
    return prototype;
  }
}
 
Example #5
Source File: MessageToMessage.java    From krpc with Apache License 2.0 6 votes vote down vote up
static Builder getFieldBuilder(Builder b, Descriptors.FieldDescriptor f) {
    try {
        if(  b instanceof MapEntry.Builder ) {
            MapEntry.Builder bb = (MapEntry.Builder)b;
            return bb.newBuilderForField(f);
        }

        String fieldName = f.getName();
        String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) + "Builder";
        Method method = b.getClass().getDeclaredMethod(methodName, dummyTypes);
        Builder builder = (Builder) method.invoke(b, dummyParameters);
        return builder;
    } catch (Exception e) {
        throw new RuntimeException("getFieldBuilder exception", e);
    }
}
 
Example #6
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@SuppressWarnings("InconsistentOverloads")
private static void mergeFromJson(
    boolean ignoringUnknownFields, String json, Builder builder, Message... additionalTypes)
    throws IOException {
  MessageMarshaller.Builder marshallerBuilder =
      MessageMarshaller.builder()
          .register(builder.getDefaultInstanceForType())
          .ignoringUnknownFields(ignoringUnknownFields);
  for (Message prototype : additionalTypes) {
    marshallerBuilder.register(prototype);
  }
  MessageMarshaller marshaller = marshallerBuilder.build();
  marshaller.mergeValue(json, builder);

  Message.Builder builder2 = builder.build().newBuilderForType();
  marshaller.mergeValue(json.getBytes(StandardCharsets.UTF_8), builder2);
  assertThat(builder2.build()).isEqualTo(builder.build());

  Message.Builder builder3 = builder.build().newBuilderForType();
  try (ByteArrayInputStream bis =
      new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) {
    marshaller.mergeValue(bis, builder3);
  }
  assertThat(builder3.build()).isEqualTo(builder.build());
}
 
Example #7
Source File: Server.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private RpcSaslProto buildSaslNegotiateResponse()
    throws IOException, InterruptedException {
  RpcSaslProto negotiateMessage = negotiateResponse;
  // accelerate token negotiation by sending initial challenge
  // in the negotiation response
  if (enabledAuthMethods.contains(AuthMethod.TOKEN)) {
    saslServer = createSaslServer(AuthMethod.TOKEN);
    byte[] challenge = saslServer.evaluateResponse(new byte[0]);
    RpcSaslProto.Builder negotiateBuilder =
        RpcSaslProto.newBuilder(negotiateResponse);
    negotiateBuilder.getAuthsBuilder(0)  // TOKEN is always first
        .setChallenge(ByteString.copyFrom(challenge));
    negotiateMessage = negotiateBuilder.build();
  }
  sentNegotiate = true;
  return negotiateMessage;
}
 
Example #8
Source File: MapToMessage.java    From krpc with Apache License 2.0 6 votes vote down vote up
static public Message toMessage(Builder b, Map<String, Object> params, Map<String, Object> ctx) {
    for (FieldDescriptor field : b.getDescriptorForType().getFields()) {
        String name = field.getName();
        Object value = getValue(params, ctx, name);
        if (value == null) continue;
        if (field.isMapField()) {
            objToMap(b, field, value);
        } else if (field.isRepeated()) {
            objToMessageObjRepeated(b, value, field);
        } else {
            if (value instanceof List) {
                value = ((List) value).get(0);
                if (value == null) continue;
            }
            objToMessageObj(b, value, field);
        }
    }

    return b.build();
}
 
Example #9
Source File: MapToMessage.java    From krpc with Apache License 2.0 6 votes vote down vote up
static Builder getFieldBuilder(Builder b, FieldDescriptor f) {

        try {
            if(  b instanceof MapEntry.Builder ) {
                 MapEntry.Builder bb = (MapEntry.Builder)b;
                 return bb.newBuilderForField(f);
            }

            String fieldName = f.getName();
            String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) + "Builder";
            Method method = b.getClass().getDeclaredMethod(methodName, dummyTypes);
            Builder builder = (Builder) method.invoke(b, dummyParameters);
            return builder;
        } catch (Exception e) {
            throw new RuntimeException("getFieldBuilder exception", e);
        }
    }
 
Example #10
Source File: BeanToMessage.java    From krpc with Apache License 2.0 6 votes vote down vote up
static public Message toMessage(Builder b,  Object bean) {
    try {
        for (Descriptors.FieldDescriptor field : b.getDescriptorForType().getFields()) {
            String name = field.getName();
            Object value = getValue(bean, name);
            if (value == null) continue;
            if (field.isMapField()) {
                objToMap(b, field, value);
            } else if (field.isRepeated()) {
                objToMessageObjRepeated(b, value, field);
            } else {
                objToMessageObj(b, value, field);
            }
        }

        return b.build();
    } catch(Exception e) {
        log.error("toMessage exception, e="+e.getMessage());
        return null;
    }
}
 
Example #11
Source File: BeanToMessage.java    From krpc with Apache License 2.0 6 votes vote down vote up
static Builder getFieldBuilder(Builder b, Descriptors.FieldDescriptor f) {
    try {
        if(  b instanceof MapEntry.Builder ) {
            MapEntry.Builder bb = (MapEntry.Builder)b;
            return bb.newBuilderForField(f);
        }

        String fieldName = f.getName();
        String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) + "Builder";
        Method method = b.getClass().getDeclaredMethod(methodName, dummyTypes);
        Builder builder = (Builder) method.invoke(b, dummyParameters);
        return builder;
    } catch (Exception e) {
        throw new RuntimeException("getFieldBuilder exception", e);
    }
}
 
Example #12
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void sortedMapKeys() throws Exception {
  TestMap.Builder mapBuilder = TestMap.newBuilder();
  mapBuilder.putStringToInt32Map("\ud834\udd20", 3); // utf-8 F0 9D 84 A0
  mapBuilder.putStringToInt32Map("foo", 99);
  mapBuilder.putStringToInt32Map("xxx", 123);
  mapBuilder.putStringToInt32Map("\u20ac", 1); // utf-8 E2 82 AC
  mapBuilder.putStringToInt32Map("abc", 20);
  mapBuilder.putStringToInt32Map("19", 19);
  mapBuilder.putStringToInt32Map("8", 8);
  mapBuilder.putStringToInt32Map("\ufb00", 2); // utf-8 EF AC 80
  mapBuilder.putInt32ToInt32Map(3, 3);
  mapBuilder.putInt32ToInt32Map(10, 10);
  mapBuilder.putInt32ToInt32Map(5, 5);
  mapBuilder.putInt32ToInt32Map(4, 4);
  mapBuilder.putInt32ToInt32Map(1, 1);
  mapBuilder.putInt32ToInt32Map(2, 2);
  mapBuilder.putInt32ToInt32Map(-3, -3);
  TestMap mapMessage = mapBuilder.build();
  assertMatchesUpstream(mapMessage, false, false, false, false, true);

  TestMap emptyMap = TestMap.getDefaultInstance();
  assertMatchesUpstream(emptyMap, false, false, false, false, true);
}
 
Example #13
Source File: Server.java    From big-c with Apache License 2.0 6 votes vote down vote up
private RpcSaslProto buildSaslNegotiateResponse()
    throws IOException, InterruptedException {
  RpcSaslProto negotiateMessage = negotiateResponse;
  // accelerate token negotiation by sending initial challenge
  // in the negotiation response
  if (enabledAuthMethods.contains(AuthMethod.TOKEN)) {
    saslServer = createSaslServer(AuthMethod.TOKEN);
    byte[] challenge = saslServer.evaluateResponse(new byte[0]);
    RpcSaslProto.Builder negotiateBuilder =
        RpcSaslProto.newBuilder(negotiateResponse);
    negotiateBuilder.getAuthsBuilder(0)  // TOKEN is always first
        .setChallenge(ByteString.copyFrom(challenge));
    negotiateMessage = negotiateBuilder.build();
  }
  sentNegotiate = true;
  return negotiateMessage;
}
 
Example #14
Source File: QueryResponseToProto.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static Object buildValue(
    Message.Builder parentBuilder, FieldDescriptor field, Object value) {
  if (field == null) {
    return value;
  }
  if (field.getType() == FieldDescriptor.Type.MESSAGE) {
    if (field.isRepeated()) {}
    Message.Builder fieldBuilder = parentBuilder.newBuilderForField(field);
    return buildMessage(fieldBuilder, (Map<String, Object>) value);
  } else if (field.getType() == FieldDescriptor.Type.ENUM) {
    return field.getEnumType().findValueByName((String) value);
  } else {
    switch (field.getType()) {
      case FLOAT: // float is a special case
        return Float.valueOf(value.toString());
      default:
        return value;
    }
  }
}
 
Example #15
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void unknownEnumValue() throws Exception {
  TestAllTypes message =
      TestAllTypes.newBuilder()
          .setOptionalNestedEnumValue(12345)
          .addRepeatedNestedEnumValue(12345)
          .addRepeatedNestedEnumValue(0)
          .build();
  assertMatchesUpstream(message);

  TestMap.Builder mapBuilder = TestMap.newBuilder();
  mapBuilder.putInt32ToEnumMapValue(1, 0);
  Map<Integer, Integer> mapWithInvalidValues = new HashMap<>();
  mapWithInvalidValues.put(2, 12345);
  mapBuilder.putAllInt32ToEnumMapValue(mapWithInvalidValues);
  TestMap mapMessage = mapBuilder.build();
  assertMatchesUpstream(mapMessage);
}
 
Example #16
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void parserAcceptsStringForNumericField() throws Exception {
  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
  mergeFromJson(
      "{\n"
          + "  \"optionalInt32\": \"1234\",\n"
          + "  \"optionalUint32\": \"5678\",\n"
          + "  \"optionalSint32\": \"9012\",\n"
          + "  \"optionalFixed32\": \"3456\",\n"
          + "  \"optionalSfixed32\": \"7890\",\n"
          + "  \"optionalFloat\": \"1.5\",\n"
          + "  \"optionalDouble\": \"1.25\",\n"
          + "  \"optionalBool\": \"true\"\n"
          + "}",
      builder);
  TestAllTypes message = builder.build();
  assertEquals(1234, message.getOptionalInt32());
  assertEquals(5678, message.getOptionalUint32());
  assertEquals(9012, message.getOptionalSint32());
  assertEquals(3456, message.getOptionalFixed32());
  assertEquals(7890, message.getOptionalSfixed32());
  assertEquals(1.5f, message.getOptionalFloat(), 0.000001);
  assertEquals(1.25, message.getOptionalDouble(), 0.000001);
  assertEquals(true, message.getOptionalBool());
}
 
Example #17
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void mapNullValueIsRejected() throws Exception {
  TestMap.Builder builder = TestMap.newBuilder();
  assertThatThrownBy(
          () ->
              mergeFromJson(
                  "{\n"
                      + "  \"int32ToInt32Map\": {null: 1},\n"
                      + "  \"int32ToMessageMap\": {null: 2}\n"
                      + "}",
                  builder))
      .isInstanceOf(InvalidProtocolBufferException.class);

  TestMap.Builder builder2 = TestMap.newBuilder();
  assertThatThrownBy(
          () ->
              mergeFromJson(
                  "{\n"
                      + "  \"int32ToInt32Map\": {\"1\": null},\n"
                      + "  \"int32ToMessageMap\": {\"2\": null}\n"
                      + "}",
                  builder2))
      .isInstanceOf(InvalidProtocolBufferException.class);
}
 
Example #18
Source File: Server.java    From big-c with Apache License 2.0 6 votes vote down vote up
private RpcSaslProto buildNegotiateResponse(List<AuthMethod> authMethods)
    throws IOException {
  RpcSaslProto.Builder negotiateBuilder = RpcSaslProto.newBuilder();
  if (authMethods.contains(AuthMethod.SIMPLE) && authMethods.size() == 1) {
    // SIMPLE-only servers return success in response to negotiate
    negotiateBuilder.setState(SaslState.SUCCESS);
  } else {
    negotiateBuilder.setState(SaslState.NEGOTIATE);
    for (AuthMethod authMethod : authMethods) {
      SaslRpcServer saslRpcServer = new SaslRpcServer(authMethod);      
      SaslAuth.Builder builder = negotiateBuilder.addAuthsBuilder()
          .setMethod(authMethod.toString())
          .setMechanism(saslRpcServer.mechanism);
      if (saslRpcServer.protocol != null) {
        builder.setProtocol(saslRpcServer.protocol);
      }
      if (saslRpcServer.serverId != null) {
        builder.setServerId(saslRpcServer.serverId);
      }
    }
  }
  return negotiateBuilder.build();
}
 
Example #19
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void anyInMaps() throws Exception {
  TestAny.Builder testAny = TestAny.newBuilder();
  testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
  testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
  testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
  testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
  testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
  Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
  Struct.Builder struct = Struct.newBuilder();
  struct.putFields("number", numberValue);
  testAny.putAnyMap("struct", Any.pack(struct.build()));
  Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  testAny.putAnyMap(
      "list_value",
      Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
  testAny.putAnyMap("number_value", Any.pack(numberValue));
  testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
  testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
  testAny.putAnyMap("default", Any.getDefaultInstance());

  assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance());
}
 
Example #20
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@Test
public void testNullFirstInDuplicateOneof() throws Exception {
  TestOneof.Builder builder = TestOneof.newBuilder();
  mergeFromJson("{\"oneofNestedMessage\": null, \"oneofInt32\": 1}", builder);
  TestOneof message = builder.build();
  assertEquals(1, message.getOneofInt32());
}
 
Example #21
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@Test
public void parserAcceptsFloatingPointValueForIntegerField() throws Exception {
  // Test that numeric values like "1.000", "1e5" will also be accepted.
  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
  mergeFromJson(
      "{\n"
          + "  \"repeatedInt32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
          + "  \"repeatedUint32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
          + "  \"repeatedInt64\": [1.000, 1e5, \"1.000\", \"1e5\"],\n"
          + "  \"repeatedUint64\": [1.000, 1e5, \"1.000\", \"1e5\"]\n"
          + "}",
      builder);
  int[] expectedValues = new int[] {1, 100000, 1, 100000};
  assertEquals(4, builder.getRepeatedInt32Count());
  assertEquals(4, builder.getRepeatedUint32Count());
  assertEquals(4, builder.getRepeatedInt64Count());
  assertEquals(4, builder.getRepeatedUint64Count());
  for (int i = 0; i < 4; ++i) {
    assertEquals(expectedValues[i], builder.getRepeatedInt32(i));
    assertEquals(expectedValues[i], builder.getRepeatedUint32(i));
    assertEquals(expectedValues[i], builder.getRepeatedInt64(i));
    assertEquals(expectedValues[i], builder.getRepeatedUint64(i));
  }

  // Non-integers will still be rejected.
  assertRejects("optionalInt32", "1.5");
  assertRejects("optionalUint32", "1.5");
  assertRejects("optionalInt64", "1.5");
  assertRejects("optionalUint64", "1.5");
}
 
Example #22
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
private static void assertAccepts(String name, String value) throws IOException {
  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
  // Both numeric form and string form are accepted.
  mergeFromJson("{\"" + name + "\":" + value + "}", builder);
  builder.clear();
  mergeFromJson("{\"" + name + "\":\"" + value + "\"}", builder);
}
 
Example #23
Source File: MessageToMessage.java    From krpc with Apache License 2.0 5 votes vote down vote up
private static void objToMap(Builder b, Descriptors.FieldDescriptor field, Object v0)   {

        if( !(v0 instanceof Collection) ) return;

        Descriptors.Descriptor type = field.getMessageType();
        Descriptors.FieldDescriptor keyField = type.findFieldByName("key");
        Descriptors.FieldDescriptor valueField = type.findFieldByName("value");
        if (keyField != null && valueField != null) {

            Collection v0c  = (Collection)v0;
            for(Object e: v0c ) {
                MapEntry entry = (MapEntry)e;
                Object key = entry.getKey();
                Object value = entry.getValue();

                com.google.protobuf.Message.Builder entryBuilder = b.newBuilderForField(field);

                Object k = objToMessageObjInner(entryBuilder,key,keyField,false);
                Object v = objToMessageObjInner(entryBuilder,value,valueField,false);

                if(k == null || v == null ) continue;

                entryBuilder.setField(keyField, k);
                entryBuilder.setField(valueField, v);
                b.addRepeatedField(field, entryBuilder.build());
            }

        } else {
            throw new RuntimeException("Invalid map field");
        }
    }
 
Example #24
Source File: MessageToMessage.java    From krpc with Apache License 2.0 5 votes vote down vote up
static void objToMessageObjRepeated(Builder b, Object value, Descriptors.FieldDescriptor field) {
    List<Object> list = TypeSafe.anyToList(value);
    if (list == null) return;

    for (Object o : list) {
        Object newObj = objToMessageObjInner(b, o, field, true);
        if (newObj != null)
            b.addRepeatedField(field, newObj);
    }
}
 
Example #25
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
private static void assertRejects(String name, String value) {
  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
  // Numeric form is rejected.
  assertThatThrownBy(() -> mergeFromJson("{\"" + name + "\":" + value + "}", builder))
      .isInstanceOf(IOException.class);
  // String form is also rejected.
  assertThatThrownBy(() -> mergeFromJson("{\"" + name + "\":\"" + value + "\"}", builder))
      .isInstanceOf(IOException.class);
}
 
Example #26
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@Test
public void testNullLastInDuplicateOneof() throws Exception {
  TestOneof.Builder builder = TestOneof.newBuilder();
  mergeFromJson("{\"oneofInt32\": 1, \"oneofNestedMessage\": null}", builder);
  TestOneof message = builder.build();
  assertEquals(1, message.getOneofInt32());
}
 
Example #27
Source File: Server.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Decode the a protobuf from the given input stream 
 * @param builder - Builder of the protobuf to decode
 * @param dis - DataInputStream to read the protobuf
 * @return Message - decoded protobuf
 * @throws WrappedRpcServerException - deserialization failed
 */
@SuppressWarnings("unchecked")
private <T extends Message> T decodeProtobufFromStream(Builder builder,
    DataInputStream dis) throws WrappedRpcServerException {
  try {
    builder.mergeDelimitedFrom(dis);
    return (T)builder.build();
  } catch (Exception ioe) {
    Class<?> protoClass = builder.getDefaultInstanceForType().getClass();
    throw new WrappedRpcServerException(
        RpcErrorCodeProto.FATAL_DESERIALIZING_REQUEST,
        "Error decoding " + protoClass.getSimpleName() + ": "+ ioe);
  }
}
 
Example #28
Source File: RandomProtobufGenerator.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a random protocol buffer, filling in all required fields but
 * with a p chance of not setting an optional field and p chance of having
 * an empty repeated field.
 */
@SuppressWarnings("unchecked")
public E generate(double p) {
  Builder builder = instance.newBuilderForType();
  Descriptor descriptor = instance.getDescriptorForType();
  for (FieldDescriptor field : descriptor.getFields()) {
    if (!field.isRequired() && random.nextDouble() < p) {
      continue;
    }
    builder.setField(field, getRandomValue(field, p));
  }
  return (E) builder.build();
}
 
Example #29
Source File: MessageToMessage.java    From krpc with Apache License 2.0 5 votes vote down vote up
public static Builder generateBuilder(Class<?> messageCls) {
    try {
        Method method = messageCls.getDeclaredMethod("newBuilder", dummyTypes);
        Builder builder = (Builder) method.invoke(null, dummyParameters);
        return builder;
    } catch (Exception e) {
        log.error("generateBuilder exception, e="+e.getMessage()+",messageCls="+messageCls);
        return null;
    }
}
 
Example #30
Source File: ProtobufParquetFileReaderWriterFactory.java    From secor with Apache License 2.0 5 votes vote down vote up
@Override
public KeyValue next() throws IOException {
    Builder messageBuilder = (Builder) reader.read();
    if (messageBuilder != null) {
        return new KeyValue(offset++, messageBuilder.build().toByteArray());
    }
    return null;
}