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();
}
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;
}
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());
}
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;
}
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());
}
Aggregations