use of org.apache.thrift.TEnum in project parquet-mr by apache.
the class ThriftSchemaConverter method toThriftField.
private static ThriftField toThriftField(String name, Field field, ThriftField.Requirement requirement) {
ThriftType type;
switch(ThriftTypeID.fromByte(field.getType())) {
case STOP:
case VOID:
default:
throw new UnsupportedOperationException("can't convert type of " + field);
case BOOL:
type = new BoolType();
break;
case BYTE:
type = new ByteType();
break;
case DOUBLE:
type = new DoubleType();
break;
case I16:
type = new I16Type();
break;
case I32:
type = new I32Type();
break;
case I64:
type = new I64Type();
break;
case STRING:
StringType stringType = new StringType();
FieldMetaData fieldMetaData = field.getFieldMetaData();
// binary data is represented by String type with an additional binary flag.
if (fieldMetaData != null && fieldMetaData.valueMetaData.isBinary()) {
stringType.setBinary(true);
}
type = stringType;
break;
case STRUCT:
type = toStructType(field.gettStructDescriptor());
break;
case MAP:
final Field mapKeyField = field.getMapKeyField();
final Field mapValueField = field.getMapValueField();
type = new ThriftType.MapType(toThriftField(mapKeyField.getName(), mapKeyField, requirement), toThriftField(mapValueField.getName(), mapValueField, requirement));
break;
case SET:
final Field setElemField = field.getSetElemField();
type = new ThriftType.SetType(toThriftField(setElemField.getName(), setElemField, requirement));
break;
case LIST:
final Field listElemField = field.getListElemField();
type = new ThriftType.ListType(toThriftField(listElemField.getName(), listElemField, requirement));
break;
case ENUM:
Collection<TEnum> enumValues = field.getEnumValues();
List<EnumValue> values = new ArrayList<ThriftType.EnumValue>();
for (TEnum tEnum : enumValues) {
values.add(new EnumValue(tEnum.getValue(), tEnum.toString()));
}
type = new EnumType(values);
break;
}
return new ThriftField(name, field.getId(), requirement, type);
}
use of org.apache.thrift.TEnum in project sw360portal by sw360.
the class DisplayEnumSelection method doEnumValues.
private void doEnumValues(Iterable<? extends TEnum> enums) throws IOException {
JspWriter jspWriter = getJspContext().getOut();
Iterator<? extends TEnum> iterator = enums.iterator();
while (iterator.hasNext()) {
TEnum enumItem = iterator.next();
String enumItemDescription = ThriftEnumUtils.enumToString(enumItem);
boolean selected = enumItem.equals(this.selected) || enumItem.toString().equals(this.selectedName);
String value = useStringValues ? enumItem.toString() : "" + enumItem.getValue();
String result = String.format("<option value=\"%s\" class=\"textlabel stackedLabel\" " + (selected ? "selected=\"selected\" " : "") + ">%s</option>", value, enumItemDescription);
if (inQuotes && iterator.hasNext()) {
jspWriter.write("\'" + result + "\' +");
} else if (inQuotes) {
jspWriter.write("\'" + result + "\'");
} else {
jspWriter.write(result);
}
}
}
use of org.apache.thrift.TEnum in project providence by morimekta.
the class TProtocolSerializerTest method assertConsistent.
@SuppressWarnings("unchecked")
public <Providence extends PMessage<Providence, ProvidenceField>, ProvidenceField extends PField, Thrift extends TBase<Thrift, ThriftField>, ThriftField extends TFieldIdEnum> void assertConsistent(String prefix, Providence providence, Thrift thrift) {
if (providence.descriptor().getVariant() == PMessageVariant.UNION) {
TUnion<?, ThriftField> t_union = (TUnion) thrift;
PUnion<?, ProvidenceField> p_union = (PUnion) providence;
ThriftField t_field = t_union.getSetField();
PField p_field = p_union.unionField();
assertEquals(p_field.getId(), t_field.getThriftFieldId());
} else {
for (PField field : providence.descriptor().getFields()) {
ThriftField thriftField = thrift.fieldForId(field.getId());
String fieldPath = (prefix.isEmpty() ? "" : prefix + ".") + field.getName();
assertEquals("has " + fieldPath, providence.has(field.getId()), thrift.isSet(thriftField));
if (providence.has(field.getId())) {
switch(field.getType()) {
case MESSAGE:
assertConsistent(fieldPath, (PMessage) providence.get(field.getId()), (TBase) thrift.getFieldValue(thriftField));
break;
case ENUM:
{
PEnumValue<?> pe = providence.get(field.getId());
TEnum te = (TEnum) thrift.getFieldValue(thriftField);
assertEquals(fieldPath, pe.asInteger(), te.getValue());
break;
}
case BINARY:
{
Binary pBin = providence.get(field.getId());
byte[] tBytes = (byte[]) thrift.getFieldValue(thriftField);
Binary tBin = Binary.wrap(tBytes);
assertEquals(fieldPath, pBin, tBin);
break;
}
case MAP:
{
Map pm = providence.get(field.getId());
Map tm = (Map) thrift.getFieldValue(thriftField);
assertEquals(fieldPath + " size", pm.size(), tm.size());
// TODO: Compare actual content.
break;
}
case SET:
{
Set ps = providence.get(field.getId());
Set ts = (Set) thrift.getFieldValue(thriftField);
assertEquals(fieldPath + " size", ps.size(), ts.size());
// TODO: Compare actual content.
break;
}
case LIST:
{
List pl = providence.get(field.getId());
List tl = (List) thrift.getFieldValue(thriftField);
assertEquals(fieldPath + " size", pl.size(), tl.size());
for (int i = 0; i < pl.size(); ++i) {
String itemPath = fieldPath + "[" + i + "]";
Object pi = pl.get(i);
Object ti = tl.get(i);
if (pi instanceof PMessage) {
assertConsistent(itemPath, (PMessage) pi, (TBase) ti);
} else if (pi instanceof Set) {
// TODO: Compare actual content.
} else if (pi instanceof List) {
// TODO: Compare actual content.
} else if (pi instanceof Map) {
// TODO: Compare actual content.
} else if (pi instanceof Binary) {
Binary pb = (Binary) pi;
Binary tb = Binary.wrap(((ByteBuffer) ti).array());
assertEquals(itemPath, pb, tb);
} else if (pi instanceof PEnumValue) {
PEnumValue pe = (PEnumValue) pi;
TEnum te = (TEnum) ti;
assertEquals(itemPath, pe.asInteger(), te.getValue());
} else {
assertEquals(itemPath, pi, ti);
}
}
break;
}
default:
assertEquals(fieldPath, providence.get(field.getId()), thrift.getFieldValue(thriftField));
break;
}
}
}
}
}
Aggregations