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 24, 2018
1 parent 800c823 commit 0d7b886
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -84,6 +85,7 @@
import static io.airlift.bytecode.BytecodeUtils.dumpBytecodeTree;
import static io.airlift.bytecode.ClassGenerator.classGenerator;
import static io.airlift.bytecode.Parameter.arg;
import static io.airlift.bytecode.ParameterizedType.getPathName;
import static io.airlift.bytecode.ParameterizedType.type;
import static io.airlift.bytecode.ParameterizedType.typeFromPathName;
import static io.airlift.bytecode.control.SwitchStatement.switchBuilder;
Expand Down Expand Up @@ -111,6 +113,7 @@
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.util.stream.Collectors.joining;

@NotThreadSafe
public class ThriftCodecByteCodeGenerator<T>
Expand Down Expand Up @@ -1038,8 +1041,16 @@ 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 = getPathName(metadata.getStructClass()) + "Codec";

Type type = metadata.getStructType();
if (type instanceof java.lang.reflect.ParameterizedType) {
className += Arrays.stream(((java.lang.reflect.ParameterizedType) type).getActualTypeArguments())
.map(arg -> arg.getTypeName().replaceAll("[^a-zA-Z0-9]+", "_"))
.collect(joining("$", "$$", ""));
}

return typeFromPathName(PACKAGE + "/" + className);
}

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 @@ -57,6 +57,8 @@
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
Expand Down Expand Up @@ -467,6 +469,21 @@ 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);

GenericThriftStructBean<List<String>> beanForList = new GenericThriftStructBean<>();
beanForList.setGenericProperty(ImmutableList.of("abc", "xyz"));

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

GenericThriftStructBean<Map<String, List<String>>> beanForMap = new GenericThriftStructBean<>();
beanForMap.setGenericProperty(ImmutableMap.of("test", ImmutableList.of("abc", "xyz")));

testRoundTripSerialize(new TypeToken<GenericThriftStructBean<Map<String, List<String>>>>() {}, beanForMap);
}

@Test
Expand Down

0 comments on commit 0d7b886

Please sign in to comment.