org.apache.thrift.TEnum Java Examples

The following examples show how to use org.apache.thrift.TEnum. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TTextProtocol.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public void writeI32(int i32) throws TException {
    if (!useNamedEnums) {
        writeNameOrValue(TypedParser.INTEGER, i32);
        return;
    }

    final Class<?> fieldClass = getCurrentFieldClassIfIs(TEnum.class);
    if (fieldClass == null) {
        writeNameOrValue(TypedParser.INTEGER, i32);
        return;
    }

    try {
        final Method method = fieldClass.getMethod("findByValue", int.class);
        final String str = method.invoke(null, i32).toString();
        writeNameOrValue(TypedParser.STRING, str);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new TTransportException("invalid value for enum field " +
                                      fieldClass.getSimpleName() + ':' + i32);
    }
}
 
Example #2
Source File: TTextProtocol.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public int readI32() throws TException {
    final Class<?> fieldClass = getCurrentFieldClassIfIs(TEnum.class);
    if (fieldClass != null) {
        // Enum fields may be set by string, even though they represent integers.
        getCurrentContext().read();
        final JsonNode elem = getCurrentContext().getCurrentChild();
        if (elem.isInt()) {
            return TypedParser.INTEGER.readFromJsonElement(elem);
        } else if (elem.isTextual()) {
            // All TEnum are enums
            @SuppressWarnings({ "unchecked", "rawtypes" })
            final TEnum tEnum = (TEnum) Enum.valueOf((Class<Enum>) fieldClass,
                                               TypedParser.STRING.readFromJsonElement(elem));
            return tEnum.getValue();
        } else {
            throw new TTransportException("invalid value type for enum field: " + elem.getNodeType() +
                                          " (" + elem + ')');
        }
    } else {
        return readNameOrValue(TypedParser.INTEGER);
    }
}
 
Example #3
Source File: ThriftDocServicePlugin.java    From armeria with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static EnumInfo newEnumInfo(Class<? extends Enum<? extends TEnum>> enumType) {
    final List<EnumValueInfo> values = Arrays.stream(enumType.getEnumConstants())
                                             .map(e -> new EnumValueInfo(e.name(), ((TEnum) e).getValue()))
                                             .collect(toImmutableList());

    return new EnumInfo(enumType.getTypeName(), values);
}
 
Example #4
Source File: ThriftSchemaConverter.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
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();
      // There is no real binary type (see THRIFT-1920) in Thrift,
      // 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);
}