Search in sources :

Example 1 with Type

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());
    }
}
Also used : Type(com.google.protobuf.Descriptors.FieldDescriptor.Type) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 2 with Type

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));
    }
}
Also used : Type(com.google.protobuf.Descriptors.FieldDescriptor.Type) JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) Random(java.util.Random) SecureRandom(java.security.SecureRandom) SecureRandom(java.security.SecureRandom) ByteString(com.google.protobuf.ByteString)

Aggregations

Type (com.google.protobuf.Descriptors.FieldDescriptor.Type)2 ByteString (com.google.protobuf.ByteString)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)1 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 JavaType (com.google.protobuf.Descriptors.FieldDescriptor.JavaType)1 SecureRandom (java.security.SecureRandom)1 Random (java.util.Random)1