Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested generics support for Structs #103

Merged
merged 1 commit into from
Dec 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Nested generics support for Structs
praveenkrishna committed Dec 24, 2018
commit 0d7b886bdd6e90bba03745d02e588673d78fb512
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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>
@@ -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)
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
@@ -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