Java Code Examples for com.google.protobuf.Message.Builder#setField()

The following examples show how to use com.google.protobuf.Message.Builder#setField() . 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: QueryResponseToProto.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static Object buildMessage(Builder builder, Map<String, Object> fields) {
  if (fields == null) {
    return builder.build();
  }
  Descriptor descriptor = builder.getDescriptorForType();
  for (Map.Entry<String, Object> entry : fields.entrySet()) {
    if (entry.getValue() == null) {
      continue;
    }
    FieldDescriptor field = getField(descriptor, entry.getKey());
    if (entry.getValue() instanceof List<?>) {
      List<Object> values = (List<Object>) entry.getValue();
      for (Object value : values) {
        builder.addRepeatedField(field, buildValue(builder, field, value));
      }

    } else {
      builder.setField(field, buildValue(builder, field, entry.getValue()));
    }
  }
  return builder.build();
}
 
Example 2
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 3
Source File: RandomProtobufGenerator.java    From incubator-retired-wave 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 4
Source File: MessageToMessage.java    From krpc with Apache License 2.0 4 votes vote down vote up
static void objToMessageObj(Builder b, Object value, Descriptors.FieldDescriptor field) {
    Object newObj = objToMessageObjInner(b, value, field, false);
    if (newObj != null)
        b.setField(field, newObj);
}
 
Example 5
Source File: MapToMessage.java    From krpc with Apache License 2.0 4 votes vote down vote up
static void objToMessageObj(Builder b, Object value, FieldDescriptor field) {
    Object newObj = objToMessageObjInner(b, value, field, false);
    if (newObj != null)
        b.setField(field, newObj);
}
 
Example 6
Source File: BeanToMessage.java    From krpc with Apache License 2.0 4 votes vote down vote up
static void objToMessageObj(Builder b, Object value, Descriptors.FieldDescriptor field) {
    Object newObj = objToMessageObjInner(b, value, field, false);
    if (newObj != null)
        b.setField(field, newObj);
}