Search in sources :

Example 36 with FieldDescriptor

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

the class CompatibilityTest method testSetAndGetFieldWithFieldDescriptor.

public void testSetAndGetFieldWithFieldDescriptor() throws Exception {
    FieldDescriptor[] fields = new FieldDescriptor[19];
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    for (int i = 1; i <= 18; i++) {
        fields[i] = descriptor.findFieldByNumber(i);
    }
    TypicalData.Builder dataBuilder = TypicalData.newBuilder();
    dataBuilder.setField(fields[1], new Integer(42));
    dataBuilder.setField(fields[2], ByteString.copyFrom("foo".getBytes()));
    dataBuilder.setField(fields[3], TypicalData.EnumType.VALUE9.getValueDescriptor());
    dataBuilder.setField(fields[11], TypicalDataMessage.newBuilder().build());
    dataBuilder.setField(fields[12], Boolean.TRUE);
    dataBuilder.setField(fields[13], new Float(43.8));
    dataBuilder.setField(fields[14], new Double(44.5));
    dataBuilder.setField(fields[15], "bar");
    dataBuilder.setField(fields[16], new Integer(24));
    dataBuilder.setField(fields[17], new Long(4422));
    dataBuilder.setField(fields[18], new Long(2244));
    dataBuilder.addRepeatedField(fields[4], new Integer(72));
    dataBuilder.addRepeatedField(fields[8], "aaa");
    dataBuilder.addRepeatedField(fields[8], "bbb");
    dataBuilder.setRepeatedField(fields[8], 1, "ccc");
    ArrayList<Double> doubles = new ArrayList<Double>();
    doubles.add(1.2);
    doubles.add(3.4);
    dataBuilder.setField(fields[7], doubles);
    checkGetters(dataBuilder.build(), fields);
    checkGetters(dataBuilder, fields);
}
Also used : TypicalData(protos.TypicalData) ArrayList(java.util.ArrayList) 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 37 with FieldDescriptor

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

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

use of com.google.protobuf.Descriptors.FieldDescriptor in project beam by apache.

the class ProtobufUtil method verifyDeterministic.

/**
   * Recursively checks whether the specified class uses any Protocol Buffers fields that cannot
   * be deterministically encoded.
   *
   * @throws NonDeterministicException if the object cannot be encoded deterministically.
   */
static void verifyDeterministic(ProtoCoder<?> coder) throws NonDeterministicException {
    Class<? extends Message> message = coder.getMessageType();
    ExtensionRegistry registry = coder.getExtensionRegistry();
    Set<Descriptor> descriptors = getRecursiveDescriptorsForClass(message, registry);
    for (Descriptor d : descriptors) {
        for (FieldDescriptor fd : d.getFields()) {
            // be encoded deterministically.
            if (fd.isMapField()) {
                String reason = String.format("Protocol Buffers message %s transitively includes Map field %s (from file %s)." + " Maps cannot be deterministically encoded.", message.getName(), fd.getFullName(), fd.getFile().getFullName());
                throw new NonDeterministicException(coder, reason);
            }
        }
    }
}
Also used : NonDeterministicException(org.apache.beam.sdk.coders.Coder.NonDeterministicException) GenericDescriptor(com.google.protobuf.Descriptors.GenericDescriptor) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 40 with FieldDescriptor

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

the class MessageField method getFieldName.

/**
     * Obtains Protobuf field name for the passed message.
     *
     * @param msg   a message to inspect
     * @param index a zero-based index of the field
     * @return name of the field
     */
public static String getFieldName(Message msg, int index) {
    final FieldDescriptor fieldDescriptor = getFieldDescriptor(msg, index);
    final String fieldName = fieldDescriptor.getName();
    return fieldName;
}
Also used : FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)58 Descriptor (com.google.protobuf.Descriptors.Descriptor)22 Test (org.junit.Test)17 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)11 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)11 BooleanFieldValidator (io.spine.validate.BooleanFieldValidator)9 ByteStringFieldValidator (io.spine.validate.ByteStringFieldValidator)9 FloatFieldValidator (io.spine.validate.FloatFieldValidator)9 LongFieldValidator (io.spine.validate.LongFieldValidator)9 StringFieldValidator (io.spine.validate.StringFieldValidator)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 TypicalData (protos.TypicalData)5 Collection (java.util.Collection)4 ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)3 FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)3 ListItemConfigNode (com.google.api.codegen.configgen.nodes.ListItemConfigNode)3 NullConfigNode (com.google.api.codegen.configgen.nodes.NullConfigNode)3 ScalarConfigNode (com.google.api.codegen.configgen.nodes.ScalarConfigNode)3 ByteString (com.google.protobuf.ByteString)3