Skip to content

Commit

Permalink
Nested generics support for Structs
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenkrishna committed Dec 17, 2018
1 parent 800c823 commit 9427515
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -71,6 +73,7 @@
import java.util.OptionalLong;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicLong;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.getOnlyElement;
Expand Down Expand Up @@ -111,11 +114,14 @@
import static io.airlift.drift.codec.metadata.FieldKind.THRIFT_FIELD;
import static io.airlift.drift.codec.metadata.FieldKind.THRIFT_UNION_ID;
import static java.lang.String.format;
import static java.time.ZoneOffset.UTC;

@NotThreadSafe
public class ThriftCodecByteCodeGenerator<T>
{
private static final String PACKAGE = "$drift";
private static final AtomicLong CLASS_ID = new AtomicLong();
private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter.ofPattern("YYYYMMdd_HHmmss");

private static final Map<ThriftProtocolType, Method> READ_METHODS;
private static final Map<ThriftProtocolType, Method> WRITE_METHODS;
Expand Down Expand Up @@ -1038,8 +1044,10 @@ private static boolean needsCodec(ThriftFieldMetadata fieldMetadata)

private static ParameterizedType toCodecType(ThriftStructMetadata metadata)
{
String className = type(metadata.getStructClass()).getClassName();
return typeFromPathName(PACKAGE + "/" + className + "Codec");
String className = type(metadata.getStructClass()).getClassName()
+ "_" + Instant.now().atZone(UTC).format(TIMESTAMP_FORMAT)
+ "_" + CLASS_ID.incrementAndGet();
return typeFromPathName(PACKAGE + "/" + className + "Codec" + metadata.getStructType().hashCode());
}

private static boolean isOptionalWrapper(Type javaType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private ThriftType(ThriftStructMetadata structMetadata)
requireNonNull(structMetadata, "structMetadata is null");

this.protocolType = ThriftProtocolType.STRUCT;
this.javaType = structMetadata.getStructClass();
this.javaType = structMetadata.getStructType();
keyTypeReference = null;
valueTypeReference = null;
this.structMetadata = structMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ public void testBeanGeneric()
bean.setGenericProperty("genericValue");

testRoundTripSerialize(new TypeToken<GenericThriftStructBean<String>>() {}, bean);

GenericThriftStructBean<Long> beanForLong = new GenericThriftStructBean<>();
beanForLong.setGenericProperty(123L);

testRoundTripSerialize(new TypeToken<GenericThriftStructBean<Long>>() {}, beanForLong);
}

@Test
Expand Down Expand Up @@ -741,7 +746,6 @@ private <T> void testRoundTripSerialize(TypeToken<T> typeToken, T value, Functio
{
ThriftCodec<T> readCodec = (ThriftCodec<T>) readCodecManager.getCodec(typeToken.getType());
ThriftCodec<T> writeCodec = (ThriftCodec<T>) writeCodecManager.getCodec(typeToken.getType());

testRoundTripSerialize(readCodec, writeCodec, typeToken.getType(), value, protocolFactory);
}

Expand Down

0 comments on commit 9427515

Please sign in to comment.