use of com.google.protobuf.Descriptors.FieldDescriptor.Type in project j2objc by google.
the class CompatibilityTest method testFindFieldByNumber.
public void testFindFieldByNumber() throws Exception {
Descriptor descriptor = TypicalData.Builder.getDescriptor();
Collection<FieldDescriptor> fields = descriptor.getFields();
for (FieldDescriptor field : fields) {
FieldDescriptor.Type type = field.getType();
int fieldId = field.getNumber();
switch(fieldId) {
case 1:
assertEquals(Type.INT32, type);
break;
case 2:
assertEquals(Type.BYTES, type);
break;
case 3:
assertEquals(Type.ENUM, type);
break;
}
FieldDescriptor result = descriptor.findFieldByNumber(fieldId);
assertEquals(field.getNumber(), result.getNumber());
assertEquals(field.getName(), result.getName());
}
}
use of com.google.protobuf.Descriptors.FieldDescriptor.Type in project core-java by SpineEventEngine.
the class Sample method valueFor.
/**
* Generates a non-default value for the given message field.
*
* <p>All the protobuf types are supported including nested {@link Message}s and
* the {@code enum}s.
*
* @param field {@link FieldDescriptor} to take the type info from
* @return a non-default generated value of type of the given field
*/
@SuppressWarnings("OverlyComplexMethod")
private static Object valueFor(FieldDescriptor field) {
final Type type = field.getType();
final JavaType javaType = type.getJavaType();
final Random random = new SecureRandom();
switch(javaType) {
case INT:
return random.nextInt();
case LONG:
return random.nextLong();
case FLOAT:
return random.nextFloat();
case DOUBLE:
return random.nextDouble();
case BOOLEAN:
return random.nextBoolean();
case STRING:
final byte[] bytes = new byte[8];
random.nextBytes(bytes);
return new String(bytes);
case BYTE_STRING:
final byte[] bytesPrimitive = new byte[8];
random.nextBytes(bytesPrimitive);
return ByteString.copyFrom(bytesPrimitive);
case ENUM:
return enumValueFor(field, random);
case MESSAGE:
return messageValueFor(field);
default:
throw new IllegalArgumentException(format("Field type %s is not supported.", type));
}
}
Aggregations