Search in sources :

Example 6 with EnumValueDescriptor

use of com.google.protobuf.Descriptors.EnumValueDescriptor in project atlasdb by palantir.

the class TableMetadataSerializer method serialize.

private void serialize(JsonGenerator jgen, EnumDescriptor enumType) throws IOException {
    jgen.writeObjectField("name", enumType.getName());
    jgen.writeArrayFieldStart("values");
    for (EnumValueDescriptor value : enumType.getValues()) {
        jgen.writeString(value.getName());
    }
    jgen.writeEndArray();
}
Also used : EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor)

Example 7 with EnumValueDescriptor

use of com.google.protobuf.Descriptors.EnumValueDescriptor in project elephant-bird by twitter.

the class ProtobufStructObjectInspector method getStructFieldData.

@Override
public Object getStructFieldData(Object data, StructField structField) {
    if (data == null) {
        return null;
    }
    Message m = (Message) data;
    ProtobufStructField psf = (ProtobufStructField) structField;
    FieldDescriptor fieldDescriptor = psf.getFieldDescriptor();
    Object result = m.getField(fieldDescriptor);
    if (fieldDescriptor.getType() == Type.ENUM) {
        return ((EnumValueDescriptor) result).getName();
    }
    if (fieldDescriptor.getType() == Type.BYTES && (result instanceof ByteString)) {
        return ((ByteString) result).toByteArray();
    }
    return result;
}
Also used : Message(com.google.protobuf.Message) ByteString(com.google.protobuf.ByteString) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 8 with EnumValueDescriptor

use of com.google.protobuf.Descriptors.EnumValueDescriptor in project curiostack by curioswitch.

the class ParseSupport method parseEnum.

/**
 * Parsers an enum value out of the input. Supports both numeric and string representations.
 */
static int parseEnum(JsonParser parser, EnumDescriptor descriptor) throws IOException {
    JsonToken json = parser.currentToken();
    if (json == JsonToken.VALUE_NULL) {
        // This should only be possible if this is a NullValue enum, parseEnum will not be called
        // otherwise when the value is null.
        checkArgument(descriptor == NullValue.getDescriptor());
        return NullValue.NULL_VALUE_VALUE;
    }
    if (json.isNumeric()) {
        try {
            return parser.getIntValue();
        } catch (IOException e) {
        // Fall through.
        }
    } else {
        try {
            String value = parser.getValueAsString();
            EnumValueDescriptor enumValue = descriptor.findValueByName(value);
            if (enumValue != null) {
                return enumValue.getNumber();
            }
        } catch (IOException e) {
        // Fall through.
        }
    }
    throw new InvalidProtocolBufferException("Invalid enum value: " + json + " for enum type: " + descriptor.getFullName());
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor)

Example 9 with EnumValueDescriptor

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

the class ProtoSchemaTranslator method beamFieldTypeFromSingularProtoField.

private static FieldType beamFieldTypeFromSingularProtoField(Descriptors.FieldDescriptor protoFieldDescriptor) {
    Descriptors.FieldDescriptor.Type fieldDescriptor = protoFieldDescriptor.getType();
    FieldType fieldType;
    switch(fieldDescriptor) {
        case INT32:
            fieldType = FieldType.INT32;
            break;
        case INT64:
            fieldType = FieldType.INT64;
            break;
        case FLOAT:
            fieldType = FieldType.FLOAT;
            break;
        case DOUBLE:
            fieldType = FieldType.DOUBLE;
            break;
        case BOOL:
            fieldType = FieldType.BOOLEAN;
            break;
        case STRING:
            fieldType = FieldType.STRING;
            break;
        case BYTES:
            fieldType = FieldType.BYTES;
            break;
        case UINT32:
            fieldType = FieldType.logicalType(new UInt32());
            break;
        case SINT32:
            fieldType = FieldType.logicalType(new SInt32());
            break;
        case FIXED32:
            fieldType = FieldType.logicalType(new Fixed32());
            break;
        case SFIXED32:
            fieldType = FieldType.logicalType(new SFixed32());
            break;
        case UINT64:
            fieldType = FieldType.logicalType(new UInt64());
            break;
        case SINT64:
            fieldType = FieldType.logicalType(new SInt64());
            break;
        case FIXED64:
            fieldType = FieldType.logicalType(new Fixed64());
            break;
        case SFIXED64:
            fieldType = FieldType.logicalType(new SFixed64());
            break;
        case ENUM:
            Map<String, Integer> enumValues = Maps.newHashMap();
            for (EnumValueDescriptor enumValue : protoFieldDescriptor.getEnumType().getValues()) {
                if (enumValues.putIfAbsent(enumValue.getName(), enumValue.getNumber()) != null) {
                    throw new RuntimeException("Aliased enumerations not currently supported.");
                }
            }
            fieldType = FieldType.logicalType(EnumerationType.create(enumValues));
            break;
        case MESSAGE:
        case GROUP:
            String fullName = protoFieldDescriptor.getMessageType().getFullName();
            switch(fullName) {
                case "google.protobuf.Timestamp":
                    fieldType = FieldType.logicalType(new NanosInstant());
                    break;
                case "google.protobuf.Int32Value":
                case "google.protobuf.UInt32Value":
                case "google.protobuf.Int64Value":
                case "google.protobuf.UInt64Value":
                case "google.protobuf.FloatValue":
                case "google.protobuf.DoubleValue":
                case "google.protobuf.StringValue":
                case "google.protobuf.BoolValue":
                case "google.protobuf.BytesValue":
                    fieldType = beamFieldTypeFromSingularProtoField(protoFieldDescriptor.getMessageType().findFieldByNumber(1));
                    break;
                case "google.protobuf.Duration":
                    fieldType = FieldType.logicalType(new NanosDuration());
                    break;
                case "google.protobuf.Any":
                    throw new RuntimeException("Any not yet supported");
                default:
                    fieldType = FieldType.row(getSchema(protoFieldDescriptor.getMessageType()));
            }
            // all messages are nullable in Proto
            if (protoFieldDescriptor.isOptional()) {
                fieldType = fieldType.withNullable(true);
            }
            break;
        default:
            throw new RuntimeException("Field type not matched.");
    }
    return fieldType;
}
Also used : SInt64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SInt64) SInt32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SInt32) NanosDuration(org.apache.beam.sdk.schemas.logicaltypes.NanosDuration) SFixed32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed32) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) SFixed64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed64) NanosInstant(org.apache.beam.sdk.schemas.logicaltypes.NanosInstant) SFixed32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed32) Fixed32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.Fixed32) Fixed64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.Fixed64) SFixed64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.SFixed64) UInt64(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.UInt64) UInt32(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaLogicalTypes.UInt32) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor)

Example 10 with EnumValueDescriptor

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

the class CompatibilityTest method testEnumDescriptor.

public void testEnumDescriptor() throws Exception {
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(3);
    assertEquals(Type.ENUM, fieldDescriptor.getType());
    EnumDescriptor enumDescriptor = fieldDescriptor.getEnumType();
    assertNotNull(enumDescriptor);
    EnumValueDescriptor enumValueDescriptor = enumDescriptor.findValueByNumber(1);
    assertEquals(1, enumValueDescriptor.getNumber());
    assertEquals("VALUE1", enumValueDescriptor.getName());
}
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) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)11 ByteString (com.google.protobuf.ByteString)6 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)6 Descriptor (com.google.protobuf.Descriptors.Descriptor)4 Builder (com.google.protobuf.Message.Builder)3 DataHandler (javax.activation.DataHandler)3 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)2 DynamicMessage (com.google.protobuf.DynamicMessage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 SBase (org.bimserver.shared.meta.SBase)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1