public ThriftType(ThriftType uncoercedType, Type javaType) { this.javaType = javaType; this.uncoercedType = uncoercedType; this.protocolType = uncoercedType.getProtocolType(); keyType = null; valueType = null; structMetadata = null; enumMetadata = null; }
@Override public ThriftProtocolType getProtocolType() { return thriftType.getProtocolType(); }
public ThriftType(ThriftType uncoercedType, Type javaType) { this.javaType = javaType; this.uncoercedType = uncoercedType; this.protocolType = uncoercedType.getProtocolType(); keyTypeReference = null; valueTypeReference = null; structMetadata = null; enumMetadata = null; }
@Override public boolean apply(@Nullable ThriftType t) { ThriftProtocolType proto = checkNotNull(t).getProtocolType(); if (proto == ThriftProtocolType.ENUM || proto == ThriftProtocolType.STRUCT) { return verifyStruct(t, true); } else { Preconditions.checkState(false, "Top-level non-enum and non-struct?"); return false; // silence compiler } } });
public ThriftContext(String namespace, List<String> includes, List<ThriftType> thriftTypes, List<ThriftServiceMetadata> thriftServices, Map<String, String> namespaceMap) { this.namespace = namespace; this.includes = includes; for (ThriftType t: thriftTypes) { switch (t.getProtocolType()) { case STRUCT: this.thriftStructs.add(t.getStructMetadata()); break; case ENUM: this.thriftEnums.add(t.getEnumMetadata()); break; default: throw new IllegalStateException("Unknown protocol type: " + t.getProtocolType()); } } this.thriftServices = thriftServices; customNamespaces = namespaceMap; }
private Method getWriteMethod(ThriftType thriftType) { if (ReflectionHelper.isArray(thriftType.getJavaType())) { return ARRAY_WRITE_METHODS.get(thriftType.getJavaType()); } return WRITE_METHODS.get(thriftType.getProtocolType()); }
private Method getReadMethod(ThriftType thriftType) { if (ReflectionHelper.isArray(thriftType.getJavaType())) { return ARRAY_READ_METHODS.get(thriftType.getJavaType()); } return READ_METHODS.get(thriftType.getProtocolType()); }
public <T> void writeField(String name, short id, ThriftCodec<T> codec, T value) throws Exception { if (value == null) { return; } protocol.writeFieldBegin(new TField(name, codec.getType().getProtocolType().getType(), id)); codec.write(value, protocol); protocol.writeFieldEnd(); }
public Object readField(ThriftCodec<?> codec) throws Exception { if (!checkReadState(codec.getType().getProtocolType().getType())) { return null; } currentField = null; Object fieldValue = codec.read(protocol); protocol.readFieldEnd(); return fieldValue; }
public <K, V> void writeMap(ThriftCodec<K> keyCodec, ThriftCodec<V> valueCodec, Map<K, V> map) throws Exception { if (map == null) { return; } protocol.writeMapBegin(new TMap(keyCodec.getType().getProtocolType().getType(), valueCodec.getType().getProtocolType().getType(), map.size())); for (Map.Entry<K, V> entry : map.entrySet()) { keyCodec.write(entry.getKey(), protocol); valueCodec.write(entry.getValue(), protocol); } protocol.writeMapEnd(); } }
private boolean needsCodec(ThriftFieldMetadata fieldMetadata) { if (ReflectionHelper.isArray(fieldMetadata.getThriftType().getJavaType())) { return false; } ThriftProtocolType protocolType = fieldMetadata.getThriftType().getProtocolType(); return protocolType == ENUM || protocolType == STRUCT || protocolType == SET || protocolType == LIST || protocolType == MAP; }
public <T> void writeSet(ThriftCodec<T> elementCodec, Set<T> set) throws Exception { if (set == null) { return; } protocol.writeSetBegin(new TSet(elementCodec.getType().getProtocolType().getType(), set.size())); for (T element : set) { elementCodec.write(element, protocol); } protocol.writeSetEnd(); }
public <T> void writeList(ThriftCodec<T> elementCodec, List<T> list) throws Exception { if (list == null) { return; } protocol.writeListBegin(new TList(elementCodec.getType().getProtocolType().getType(), list.size())); for (T element : list) { elementCodec.write(element, protocol); } protocol.writeListEnd(); }
private boolean verifyField(ThriftType t) { ThriftProtocolType proto = t.getProtocolType(); if (proto == ThriftProtocolType.SET || proto == ThriftProtocolType.LIST) { return verifyElementType(t.getValueTypeReference()); } else if (proto == ThriftProtocolType.MAP) { // no short-circuit return verifyElementType(t.getKeyTypeReference()) & verifyElementType(t.getValueTypeReference()); } else { if (knownTypes.contains(t)) { return true; } if (includeMap.containsKey(t)) { usedIncludedTypes.add(t); return true; } if (recursive) { // recursive but type is unknown - add it to the list and recurse thriftTypes.add(t); return verifyStruct(t, true); } return false; } }
return coercion.getThriftType().getProtocolType();
private boolean verifyStruct(ThriftType t, boolean quiet) { if (t.getProtocolType() == ThriftProtocolType.ENUM) { knownTypes.add(t); return true; } ThriftStructMetadata metadata = t.getStructMetadata(); boolean ok = true; knownTypes.add(t); for (ThriftFieldMetadata fieldMetadata: metadata.getFields(FieldKind.THRIFT_FIELD)) { if (!recursive && fieldMetadata.isTypeReferenceRecursive()) { continue; } boolean fieldOk = verifyField(fieldMetadata.getThriftType()); if (!fieldOk) { ok = false; if (!quiet) { LOG.error("Unknown type %s in %s.%s", thriftTypeRenderer.toString(fieldMetadata.getThriftType()), metadata.getStructName(), fieldMetadata.getName()); } } } if (!ok) { knownTypes.remove(t); } return ok; }
switch (type.getProtocolType()) { case STRUCT: { return factory.generateThriftTypeCodec(ThriftCodecManager.this, type.getStructMetadata());
public String toString(ThriftType t) { switch (t.getProtocolType()) { case BOOL: return "bool"; case BYTE: return "byte"; case DOUBLE: return "double"; case I16: return "i16"; case I32: return "i32"; case I64: return "i64"; case ENUM: return prefix(t) + t.getEnumMetadata().getEnumName(); case MAP: return "map<" + toString(t.getKeyTypeReference().get()) + ", " + toString(t.getValueTypeReference().get()) + ">"; case SET: return "set<" + toString(t.getValueTypeReference().get()) + ">"; case LIST: return "list<" + toString(t.getValueTypeReference().get()) + ">"; // void is encoded as a struct case STRUCT: return t.equals(ThriftType.VOID) ? "void" : prefix(t) + t.getStructMetadata().getStructName(); case STRING: return "string"; case BINARY: return "binary"; } throw new IllegalStateException("Bad protocol type" + t.getProtocolType()); }
throw new IllegalArgumentException("Unsupported field type " + field.getThriftType().getProtocolType());
throw new IllegalArgumentException("Unsupported field type " + field.getThriftType().getProtocolType());