Example usage for org.apache.thrift.protocol TType ENUM

List of usage examples for org.apache.thrift.protocol TType ENUM

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TType ENUM.

Prototype

byte ENUM

To view the source code for org.apache.thrift.protocol TType ENUM.

Click Source Link

Usage

From source file:com.linecorp.armeria.common.thrift.text.StructContext.java

License:Apache License

/**
 * Compute a new field name map for the current thrift message
 * we are parsing./*  w ww.  j  a v  a 2  s .c o m*/
 */
private Map<String, TField> computeFieldNameMap(Class<?> clazz) {
    Map<String, TField> map = new HashMap<>();

    if (isTBase(clazz)) {
        // Get the metaDataMap for this Thrift class
        @SuppressWarnings("unchecked")
        Map<? extends TFieldIdEnum, FieldMetaData> metaDataMap = FieldMetaData
                .getStructMetaDataMap((Class<? extends TBase<?, ?>>) clazz);

        for (Entry<? extends TFieldIdEnum, FieldMetaData> e : metaDataMap.entrySet()) {
            final String fieldName = e.getKey().getFieldName();
            final FieldMetaData metaData = e.getValue();

            final FieldValueMetaData elementMetaData;
            if (metaData.valueMetaData.isContainer()) {
                if (metaData.valueMetaData instanceof SetMetaData) {
                    elementMetaData = ((SetMetaData) metaData.valueMetaData).elemMetaData;
                } else if (metaData.valueMetaData instanceof ListMetaData) {
                    elementMetaData = ((ListMetaData) metaData.valueMetaData).elemMetaData;
                } else if (metaData.valueMetaData instanceof MapMetaData) {
                    elementMetaData = ((MapMetaData) metaData.valueMetaData).valueMetaData;
                } else {
                    // Unrecognized container type, but let's still continue processing without
                    // special enum support.
                    elementMetaData = metaData.valueMetaData;
                }
            } else {
                elementMetaData = metaData.valueMetaData;
            }

            if (elementMetaData instanceof EnumMetaData) {
                classMap.put(fieldName, ((EnumMetaData) elementMetaData).enumClass);
            } else if (elementMetaData instanceof StructMetaData) {
                classMap.put(fieldName, ((StructMetaData) elementMetaData).structClass);
            }

            // Workaround a bug in the generated thrift message read()
            // method by mapping the ENUM type to the INT32 type
            // The thrift generated parsing code requires that, when expecting
            // a value of enum, we actually parse a value of type int32. The
            // generated read() method then looks up the enum value in a map.
            byte type = TType.ENUM == metaData.valueMetaData.type ? TType.I32 : metaData.valueMetaData.type;

            map.put(fieldName, new TField(fieldName, type, e.getKey().getThriftFieldId()));
        }
    } else { // TApplicationException
        map.put("message", new TField("message", (byte) 11, (short) 1));
        map.put("type", new TField("type", (byte) 8, (short) 2));
    }

    return map;
}

From source file:com.linecorp.armeria.common.thrift.ThriftUtil.java

License:Apache License

/**
 * Converts the specified {@link FieldValueMetaData} into its corresponding Java type.
 *//*from   w w w  . j  a va  2 s .  com*/
public static Class<?> toJavaType(FieldValueMetaData metadata) {
    switch (metadata.type) {
    case TType.BOOL:
        return Boolean.class;
    case TType.BYTE:
        return Byte.class;
    case TType.DOUBLE:
        return Double.class;
    case TType.ENUM:
        return Enum.class;
    case TType.I16:
        return Short.class;
    case TType.I32:
        return Integer.class;
    case TType.I64:
        return Long.class;
    case TType.LIST:
        return List.class;
    case TType.MAP:
        return Map.class;
    case TType.SET:
        return Set.class;
    case TType.STRING:
        return String.class;
    case TType.STRUCT:
        return ((StructMetaData) metadata).structClass;
    case TType.VOID:
        return Void.class;
    }

    // Should never reach here.
    throw new Error();
}

From source file:com.linecorp.armeria.server.docs.EnumInfo.java

License:Apache License

static EnumInfo of(EnumMetaData enumMetaData, Map<String, String> docStrings) {
    requireNonNull(enumMetaData, "enumMetaData");

    final Class<?> enumClass = enumMetaData.enumClass;

    assert enumMetaData.type == TType.ENUM;
    assert !enumMetaData.isBinary();

    final List<Object> constants = new ArrayList<>();
    final Field[] fields = enumClass.getDeclaredFields();
    for (Field field : fields) {
        if (field.isEnumConstant()) {
            try {
                constants.add(field.get(null));
            } catch (IllegalAccessException ignored) {
                // Skip inaccessible fields.
            }/*from  w w  w .ja  va 2  s . c  om*/
        }
    }

    final String name = enumClass.getName();
    return new EnumInfo(name, constants, docStrings.get(name));
}

From source file:com.linecorp.armeria.server.docs.EnumInfoTest.java

License:Apache License

@Test
public void testOf() throws Exception {
    final EnumInfo enumInfo = EnumInfo.of(new EnumMetaData(TType.ENUM, FooEnum.class));

    assertThat(enumInfo,//from  w w  w  .j  av  a 2 s . c  o m
            is(EnumInfo.of(FooEnum.class.getName(), Arrays.asList(FooEnum.VAL1, FooEnum.VAL2, FooEnum.VAL3))));
}

From source file:com.linecorp.armeria.server.docs.StructInfoTest.java

License:Apache License

@Test
public void testOf() throws Exception {
    final TypeInfo string = TypeInfo.of(ValueType.STRING, false);
    final EnumInfo fooEnum = EnumInfo.of(new EnumMetaData(TType.ENUM, FooEnum.class));
    final StructInfo union = StructInfo.of(new StructMetaData(TType.STRUCT, FooUnion.class));

    final List<FieldInfo> fields = new ArrayList<>();
    fields.add(FieldInfo.of("boolVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.BOOL, false)));
    fields.add(FieldInfo.of("byteVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.BYTE, false)));
    fields.add(FieldInfo.of("i16Val", RequirementType.DEFAULT, TypeInfo.of(ValueType.I16, false)));
    fields.add(FieldInfo.of("i32Val", RequirementType.DEFAULT, TypeInfo.of(ValueType.I32, false)));
    fields.add(FieldInfo.of("i64Val", RequirementType.DEFAULT, TypeInfo.of(ValueType.I64, false)));
    fields.add(FieldInfo.of("doubleVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.DOUBLE, false)));
    fields.add(FieldInfo.of("stringVal", RequirementType.DEFAULT, string));
    fields.add(FieldInfo.of("binaryVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.STRING, true)));
    fields.add(FieldInfo.of("enumVal", RequirementType.DEFAULT, fooEnum));
    fields.add(FieldInfo.of("unionVal", RequirementType.DEFAULT, union));
    fields.add(FieldInfo.of("mapVal", RequirementType.DEFAULT, MapInfo.of(string, fooEnum)));
    fields.add(FieldInfo.of("setVal", RequirementType.DEFAULT, SetInfo.of(union)));
    fields.add(FieldInfo.of("listVal", RequirementType.DEFAULT, ListInfo.of(string)));

    final StructInfo fooStruct = StructInfo.of(new StructMetaData(TType.STRUCT, FooStruct.class));
    assertThat(fooStruct, is(StructInfo.of(FooStruct.class.getName(), fields)));
}

From source file:com.linecorp.armeria.server.docs.ValueType.java

License:Apache License

static ValueType of(byte type) {
    switch (type) {
    case TType.STOP:
        return ValueType.STOP;
    case TType.VOID:
        return ValueType.VOID;
    case TType.BOOL:
        return ValueType.BOOL;
    case TType.BYTE:
        return ValueType.BYTE;
    case TType.DOUBLE:
        return ValueType.DOUBLE;
    case TType.I16:
        return ValueType.I16;
    case TType.I32:
        return ValueType.I32;
    case TType.I64:
        return ValueType.I64;
    case TType.STRING:
        return ValueType.STRING;
    case TType.STRUCT:
        return ValueType.STRUCT;
    case TType.MAP:
        return ValueType.MAP;
    case TType.SET:
        return ValueType.SET;
    case TType.LIST:
        return ValueType.LIST;
    case TType.ENUM:
        return ValueType.ENUM;
    default://  w  ww .j  a  v  a2s .co m
        throw new IllegalArgumentException("unknown field value type: " + type);
    }
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceSpecificationGenerator.java

License:Apache License

@VisibleForTesting
static EnumInfo newEnumInfo(EnumMetaData enumMetaData, Map<String, String> docStrings) {
    requireNonNull(enumMetaData, "enumMetaData");

    final Class<?> enumClass = enumMetaData.enumClass;

    assert enumMetaData.type == TType.ENUM;
    assert !enumMetaData.isBinary();

    final List<Object> constants = new ArrayList<>();
    final Field[] fields = enumClass.getDeclaredFields();
    for (Field field : fields) {
        if (field.isEnumConstant()) {
            try {
                constants.add(field.get(null));
            } catch (IllegalAccessException ignored) {
                // Skip inaccessible fields.
            }/*from   www.  jav  a2s  . c  o  m*/
        }
    }

    final String name = enumClass.getName();
    return new EnumInfo(name, constants, docStrings.get(name));
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceSpecificationGeneratorTest.java

License:Apache License

@Test
public void testNewEnumInfo() throws Exception {
    final EnumInfo enumInfo = newEnumInfo(new EnumMetaData(TType.ENUM, FooEnum.class), emptyMap());

    assertThat(enumInfo).isEqualTo(//from w w w. j a  v  a2 s  .  c o  m
            new EnumInfo(FooEnum.class.getName(), Arrays.asList(FooEnum.VAL1, FooEnum.VAL2, FooEnum.VAL3)));
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceSpecificationGeneratorTest.java

License:Apache License

@Test
public void testNewStructInfoTest() throws Exception {
    final EnumInfo fooEnum = newEnumInfo(new EnumMetaData(TType.ENUM, FooEnum.class), emptyMap());
    final StructInfo union = newStructInfo(new StructMetaData(TType.STRUCT, FooUnion.class), emptyMap());

    final List<FieldInfo> fields = new ArrayList<>();
    fields.add(new FieldInfo("boolVal", FieldRequirement.DEFAULT, TypeInfo.BOOL));
    fields.add(new FieldInfo("byteVal", FieldRequirement.DEFAULT, TypeInfo.I8));
    fields.add(new FieldInfo("i16Val", FieldRequirement.DEFAULT, TypeInfo.I16));
    fields.add(new FieldInfo("i32Val", FieldRequirement.DEFAULT, TypeInfo.I32));
    fields.add(new FieldInfo("i64Val", FieldRequirement.DEFAULT, TypeInfo.I64));
    fields.add(new FieldInfo("doubleVal", FieldRequirement.DEFAULT, TypeInfo.DOUBLE));
    fields.add(new FieldInfo("stringVal", FieldRequirement.DEFAULT, TypeInfo.STRING));
    fields.add(new FieldInfo("binaryVal", FieldRequirement.DEFAULT, TypeInfo.BINARY));
    fields.add(new FieldInfo("enumVal", FieldRequirement.DEFAULT, fooEnum));
    fields.add(new FieldInfo("unionVal", FieldRequirement.DEFAULT, union));
    fields.add(new FieldInfo("mapVal", FieldRequirement.DEFAULT, new MapInfo(TypeInfo.STRING, fooEnum)));
    fields.add(new FieldInfo("setVal", FieldRequirement.DEFAULT, new SetInfo(union)));
    fields.add(new FieldInfo("listVal", FieldRequirement.DEFAULT, new ListInfo(TypeInfo.STRING)));

    final StructInfo fooStruct = newStructInfo(new StructMetaData(TType.STRUCT, FooStruct.class), emptyMap());
    assertThat(fooStruct).isEqualTo(new StructInfo(FooStruct.class.getName(), fields));
}

From source file:com.siemens.sw360.datahandler.common.Moderator.java

License:Open Source License

protected T updateBasicField(U field, FieldMetaData fieldMetaData, T project, T projectAdditions,
        T projectDeletions) {/*from w  w  w.j  ava 2s  . com*/
    switch (fieldMetaData.valueMetaData.type) {
    case TType.SET:
        Set<String> originalSet = (Set<String>) project.getFieldValue(field);
        originalSet.removeAll((Set<String>) projectDeletions.getFieldValue(field));
        originalSet.addAll((Set<String>) projectAdditions.getFieldValue(field));
        break;

    case TType.STRING:
    case TType.ENUM:
        project.setFieldValue(field, projectAdditions.getFieldValue(field));
        break;

    default:
        log.error("Unknown project field in ProjectModerator: " + field.getFieldName());
    }
    return project;
}