Search in sources :

Example 16 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project j2objc by google.

the class CompatibilityTest method testGetJavaType.

public void testGetJavaType() throws Exception {
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    FieldDescriptor intField = descriptor.findFieldByNumber(1);
    assertEquals(FieldDescriptor.JavaType.INT, intField.getJavaType());
    FieldDescriptor bytesField = descriptor.findFieldByNumber(2);
    assertEquals(FieldDescriptor.JavaType.BYTE_STRING, bytesField.getJavaType());
    FieldDescriptor booleanField = descriptor.findFieldByNumber(5);
    assertEquals(FieldDescriptor.JavaType.BOOLEAN, booleanField.getJavaType());
    FieldDescriptor stringField = descriptor.findFieldByNumber(8);
    assertEquals(FieldDescriptor.JavaType.STRING, stringField.getJavaType());
}
Also used : 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 17 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project j2objc by google.

the class CompatibilityTest method testGetUnsetField.

public void testGetUnsetField() throws Exception {
    TypicalData data = TypicalData.newBuilder().build();
    Descriptor descriptor = TypicalData.getDescriptor();
    assertEquals(0, data.getField(descriptor.findFieldByNumber(1)));
    Object result = data.getField(descriptor.findFieldByNumber(3));
    assertTrue(result instanceof EnumValueDescriptor);
    assertEquals(TypicalData.EnumType.VALUE1.getValueDescriptor().getNumber(), ((EnumValueDescriptor) result).getNumber());
    assertTrue(data.getField(descriptor.findFieldByNumber(11)) instanceof TypicalDataMessage);
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) TypicalData(protos.TypicalData) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor)

Example 18 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor 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 19 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project core-java by SpineEventEngine.

the class KnownTypesShould method provide_proto_descriptor_by_type_name.

@Test
public void provide_proto_descriptor_by_type_name() {
    final String typeName = "spine.test.types.Task";
    final Descriptor typeDescriptor = (Descriptor) getDescriptor(typeName);
    assertNotNull(typeDescriptor);
    assertEquals(typeName, typeDescriptor.getFullName());
}
Also used : KnownTypes.getDescriptor(io.spine.type.KnownTypes.getDescriptor) Descriptor(com.google.protobuf.Descriptors.Descriptor) Test(org.junit.Test)

Example 20 with Descriptor

use of com.google.protobuf.Descriptors.Descriptor in project core-java by SpineEventEngine.

the class Sample method builderForType.

/**
     * Generates a new stub {@link Message.Builder} with all the fields set to
     * {@link Random random} values.
     *
     * <p> All the fields are guaranteed to be not {@code null} and not default.
     * Number and {@code boolean} fields may or may not have their default values ({@code 0} and
     * {@code false}).
     *
     * @param clazz Java class of the stub message
     * @param <M>   type of the required message
     * @param <B>   type of the {@link Message.Builder} for the message
     * @return new instance of the {@link Message.Builder} for given type
     * @see #valueFor(FieldDescriptor)
     */
public static <M extends Message, B extends Message.Builder> B builderForType(Class<M> clazz) {
    checkClass(clazz);
    final B builder = builderFor(clazz);
    final Descriptor builderDescriptor = builder.getDescriptorForType();
    final Collection<FieldDescriptor> fields = builderDescriptor.getFields();
    for (FieldDescriptor field : fields) {
        final Object value = valueFor(field);
        if (field.isRepeated()) {
            builder.addRepeatedField(field, value);
        } else {
            builder.setField(field, value);
        }
    }
    return builder;
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

Descriptor (com.google.protobuf.Descriptors.Descriptor)25 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)22 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)13 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)13 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)4 TypicalData (protos.TypicalData)4 ArrayList (java.util.ArrayList)3 TypicalDataMessage (protos.TypicalDataMessage)3 GenericDescriptor (com.google.protobuf.Descriptors.GenericDescriptor)2 GeneratedMessage (com.google.protobuf.GeneratedMessage)2 HashSet (java.util.HashSet)2 ByteString (com.google.protobuf.ByteString)1 Descriptors (com.google.protobuf.Descriptors)1 Type (com.google.protobuf.Descriptors.FieldDescriptor.Type)1 ExtensionRegistryLite (com.google.protobuf.ExtensionRegistryLite)1 ExtendableMessageOrBuilder (com.google.protobuf.GeneratedMessage.ExtendableMessageOrBuilder)1 Message (com.google.protobuf.Message)1 KnownTypes.getDescriptor (io.spine.type.KnownTypes.getDescriptor)1 TypeUrl (io.spine.type.TypeUrl)1 NonDeterministicException (org.apache.beam.sdk.coders.Coder.NonDeterministicException)1