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

The following examples show how to use com.google.protobuf.Message.Builder#addRepeatedField() . 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: 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 3
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 4
Source File: MapToMessage.java    From krpc with Apache License 2.0 5 votes vote down vote up
private static void objToMap(Builder b, FieldDescriptor field, Object map0)   {

        if( !(map0 instanceof  Map) ) return;

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

            Map map  = (Map)map0;
            for(Object e: map.entrySet() ) {
                Map.Entry entry = (Map.Entry)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 5
Source File: MapToMessage.java    From krpc with Apache License 2.0 5 votes vote down vote up
static void objToMessageObjRepeated(Builder b, Object value, 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 6
Source File: BeanToMessage.java    From krpc with Apache License 2.0 5 votes vote down vote up
private static void objToMap(Builder b, Descriptors.FieldDescriptor field, Object map0)   {

        if( !(map0 instanceof Map) ) return;

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

            Map map  = (Map)map0;
            for(Object e: map.entrySet() ) {
                Map.Entry entry = (Map.Entry)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 7
Source File: BeanToMessage.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);
    }
}