Java Code Examples for org.apache.thrift.protocol.TType#I32

The following examples show how to use org.apache.thrift.protocol.TType#I32 . 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: InternalScribeCodec.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
static boolean parseResponse(TBinaryProtocol iprot) throws TException {
  Boolean result = null;
  iprot.readStructBegin();
  TField schemeField;
  while ((schemeField = iprot.readFieldBegin()).type != TType.STOP) {
    if (schemeField.id == 0 /* SUCCESS */ && schemeField.type == TType.I32) {
      result = iprot.readI32() == 0;
    } else {
      TProtocolUtil.skip(iprot, schemeField.type);
    }
  }
  iprot.readStructEnd();
  if (result != null) return result;
  throw new TApplicationException(MISSING_RESULT, "Log failed: unknown result");
}
 
Example 2
Source File: TApplicationExceptions.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a {@link TApplicationException} from the specified {@link TProtocol}.
 *
 * <p>Note: This has been copied from {@link TApplicationException#read(TProtocol)} due to API differences
 * between libthrift 0.9.x and 0.10.x.
 */
public static TApplicationException read(TProtocol iprot) throws TException {
    TField field;
    iprot.readStructBegin();

    String message = null;
    int type = TApplicationException.UNKNOWN;

    while (true) {
        field = iprot.readFieldBegin();
        if (field.type == TType.STOP) {
            break;
        }
        switch (field.id) {
            case 1:
                if (field.type == TType.STRING) {
                    message = iprot.readString();
                } else {
                    TProtocolUtil.skip(iprot, field.type);
                }
                break;
            case 2:
                if (field.type == TType.I32) {
                    type = iprot.readI32();
                } else {
                    TProtocolUtil.skip(iprot, field.type);
                }
                break;
            default:
                TProtocolUtil.skip(iprot, field.type);
                break;
        }
        iprot.readFieldEnd();
    }
    iprot.readStructEnd();

    return new TApplicationException(type, message);
}
 
Example 3
Source File: EnumTypeAdapterFactory.java    From Firefly with Apache License 2.0 4 votes vote down vote up
@Override
public byte getTType() {
    return TType.I32;
}
 
Example 4
Source File: TypeAdapter.java    From Firefly with Apache License 2.0 4 votes vote down vote up
@Override
public byte getTType() {
    return TType.I32;
}
 
Example 5
Source File: ThriftDocServicePlugin.java    From armeria with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static TypeSignature toTypeSignature(FieldValueMetaData fieldValueMetaData) {
    if (fieldValueMetaData instanceof StructMetaData) {
        return TypeSignature.ofNamed(((StructMetaData) fieldValueMetaData).structClass);
    }

    if (fieldValueMetaData instanceof EnumMetaData) {
        return TypeSignature.ofNamed(((EnumMetaData) fieldValueMetaData).enumClass);
    }

    if (fieldValueMetaData instanceof ListMetaData) {
        return TypeSignature.ofList(toTypeSignature(((ListMetaData) fieldValueMetaData).elemMetaData));
    }

    if (fieldValueMetaData instanceof SetMetaData) {
        return TypeSignature.ofSet(toTypeSignature(((SetMetaData) fieldValueMetaData).elemMetaData));
    }

    if (fieldValueMetaData instanceof MapMetaData) {
        return TypeSignature.ofMap(toTypeSignature(((MapMetaData) fieldValueMetaData).keyMetaData),
                                   toTypeSignature(((MapMetaData) fieldValueMetaData).valueMetaData));
    }

    if (fieldValueMetaData.isBinary()) {
        return BINARY;
    }

    switch (fieldValueMetaData.type) {
        case TType.VOID:
            return VOID;
        case TType.BOOL:
            return BOOL;
        case TType.BYTE:
            return I8;
        case TType.DOUBLE:
            return DOUBLE;
        case TType.I16:
            return I16;
        case TType.I32:
            return I32;
        case TType.I64:
            return I64;
        case TType.STRING:
            return STRING;
    }

    final String unresolvedName;
    if (fieldValueMetaData.isTypedef()) {
        unresolvedName = fieldValueMetaData.getTypedefName();
    } else {
        unresolvedName = null;
    }

    return TypeSignature.ofUnresolved(firstNonNull(unresolvedName, "unknown"));
}
 
Example 6
Source File: StructContext.java    From armeria with Apache License 2.0 4 votes vote down vote up
/**
 * Compute a new field name map for the current thrift message
 * we are parsing.
 */
private Map<String, TField> computeFieldNameMap(Class<?> clazz) {
    final Map<String, TField> map = new HashMap<>();

    if (isTBase(clazz)) {
        // Get the metaDataMap for this Thrift class
        @SuppressWarnings("unchecked")
        final 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);
            } else {
                // Workaround a bug where the generated 'FieldMetaData' does not provide
                // a fully qualified class name.
                final String typedefName = elementMetaData.getTypedefName();
                if (typedefName != null) {
                    final String fqcn = clazz.getPackage().getName() + '.' + typedefName;
                    Class<?> fieldClass = fieldMetaDataClassCache.get(fqcn);
                    if (fieldClass == null) {
                        fieldClass = fieldMetaDataClassCache.computeIfAbsent(fqcn, key -> {
                            try {
                                return Class.forName(key);
                            } catch (ClassNotFoundException ignored) {
                                return StructContext.class;
                            }
                        });
                    }
                    if (fieldClass != StructContext.class) {
                        classMap.put(fieldName, fieldClass);
                    }
                }
            }

            // 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.
            final 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;
}
 
Example 7
Source File: ProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
void readOneValue(TProtocol in, TProtocol out, byte type)
    throws TException {
  switch (type) {
  case TType.LIST:
    readOneList(in, out);
    break;
  case TType.MAP:
    readOneMap(in, out);
    break;
  case TType.SET:
    readOneSet(in, out);
    break;
  case TType.STRUCT:
    readOneStruct(in, out);
    break;
  case TType.STOP:
    break;
  case TType.BOOL:
    out.writeBool(in.readBool());
    break;
  case TType.BYTE:
    out.writeByte(in.readByte());
    break;
  case TType.DOUBLE:
    out.writeDouble(in.readDouble());
    break;
  case TType.I16:
    out.writeI16(in.readI16());
    break;
  case TType.ENUM: // same as i32 => actually never seen in the protocol layer as enums are written as a i32 field
  case TType.I32:
    out.writeI32(in.readI32());
    break;
  case TType.I64:
    out.writeI64(in.readI64());
    break;
  case TType.STRING:
    out.writeBinary(in.readBinary());
    break;
  case TType.VOID:
    break;
  default:
    throw new TException("Unknown type: " + type);
  }
}
 
Example 8
Source File: BufferedProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
/**
 * @return true when all value is consumed, false when some values is ignored due to the field is not defined in expectedType
 * @throws TException
 */
private boolean readOneValue(TProtocol in, byte type, List<Action> buffer, ThriftType expectedType) throws TException {
  if (expectedType != null && expectedType.getType().getSerializedThriftType() != type) {
    throw new DecodingSchemaMismatchException("the data type does not match the expected thrift structure: expected " + expectedType + " got " + typeName(type));
  }
  boolean hasFieldsIgnored = false;
  switch (type) {
  case TType.LIST:
    hasFieldsIgnored = readOneList(in, buffer, (ListType)expectedType);
    break;
  case TType.MAP:
    hasFieldsIgnored = readOneMap(in, buffer, (MapType)expectedType);
    break;
  case TType.SET:
    hasFieldsIgnored = readOneSet(in, buffer, (SetType)expectedType);
    break;
  case TType.STRUCT:
    hasFieldsIgnored = readOneStruct(in, buffer, (StructType)expectedType);
    break;
  case TType.STOP:
    break;
  case TType.BOOL:
    final boolean bool = in.readBool();
    writeBoolAction(buffer, bool);
    break;
  case TType.BYTE:
    final byte b = in.readByte();
    writeByteAction(buffer, b);
    break;
  case TType.DOUBLE:
    final double d = in.readDouble();
    writeDoubleAction(buffer, d);
    break;
  case TType.I16:
    final short s = in.readI16();
    writeShortAction(buffer, s);
    break;
  case TType.ENUM: // same as i32 => actually never seen in the protocol layer as enums are written as a i32 field
  case TType.I32:
    final int i = in.readI32();
    checkEnum(expectedType,i);
    writeIntAction(buffer, i);
    break;
  case TType.I64:
    final long l = in.readI64();
    writeLongAction(buffer, l);
    break;
  case TType.STRING:
    final ByteBuffer bin = in.readBinary();
    writeStringAction(buffer, bin);
    break;
  case TType.VOID:
    break;
  default:
    throw new TException("Unknown type: " + type);
  }
  return hasFieldsIgnored;
}