Skip to content

Commit

Permalink
Add NestedSet support in AutoCodec for type parameters which have an …
Browse files Browse the repository at this point in the history
…InjectingObjectCodec.

PiperOrigin-RevId: 184566571
  • Loading branch information
calpeyser authored and Copybara-Service committed Feb 5, 2018
1 parent d5de133 commit c5c2a76
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.common.hash.Hashing;
import com.google.common.hash.HashingOutputStream;
import com.google.devtools.build.lib.skyframe.serialization.EnumCodec;
import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodecAdapter;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.protobuf.ByteString;
Expand Down Expand Up @@ -46,6 +48,10 @@ public NestedSetCodec(ObjectCodec<T> objectCodec) {
this.objectCodec = objectCodec;
}

public <D> NestedSetCodec(InjectingObjectCodec<T, D> injectingObjectCodec, D dependency) {
this.objectCodec = new InjectingObjectCodecAdapter<>(injectingObjectCodec, dependency);
}

@SuppressWarnings("unchecked")
@Override
public Class<NestedSet<T>> getEncodedClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,27 +707,51 @@ public boolean matches(DeclaredType type) {
public void addSerializationCode(Context context) {
TypeMirror typeParameter = context.getDeclaredType().getTypeArguments().get(0);
String nestedSetCodec = context.makeName("nestedSetCodec");
context.builder.addStatement(
"$T<$T> $L = new $T<>($T.CODEC)",
NestedSetCodec.class,
typeParameter,
nestedSetCodec,
NestedSetCodec.class,
typeParameter);

TypeMirror typeParameterCodec = getCodec((DeclaredType) typeParameter).get().asType();
if (matchesErased(typeParameterCodec, InjectingObjectCodec.class)) {
context.builder.addStatement(
"$T<$T> $L = new $T<>($T.CODEC, dependency)",
NestedSetCodec.class,
typeParameter,
nestedSetCodec,
NestedSetCodec.class,
typeParameter);
} else {
context.builder.addStatement(
"$T<$T> $L = new $T<>($T.CODEC)",
NestedSetCodec.class,
typeParameter,
nestedSetCodec,
NestedSetCodec.class,
typeParameter);
}
context.builder.addStatement("$L.serialize($L, codedOut)", nestedSetCodec, context.name);
}

@Override
public void addDeserializationCode(Context context) {
TypeMirror typeParameter = context.getDeclaredType().getTypeArguments().get(0);
String nestedSetCodec = context.makeName("nestedSetCodec");
context.builder.addStatement(
"$T<$T> $L = new $T<>($T.CODEC)",
NestedSetCodec.class,
typeParameter,
nestedSetCodec,
NestedSetCodec.class,
typeParameter);

TypeMirror typeParameterCodec = getCodec((DeclaredType) typeParameter).get().asType();
if (matchesErased(typeParameterCodec, InjectingObjectCodec.class)) {
context.builder.addStatement(
"$T<$T> $L = new $T<>($T.CODEC, dependency)",
NestedSetCodec.class,
typeParameter,
nestedSetCodec,
NestedSetCodec.class,
typeParameter);
} else {
context.builder.addStatement(
"$T<$T> $L = new $T<>($T.CODEC)",
NestedSetCodec.class,
typeParameter,
nestedSetCodec,
NestedSetCodec.class,
typeParameter);
}
context.builder.addStatement(
"$L = $L.deserialize(codedIn)", context.name, nestedSetCodec);
}
Expand Down

0 comments on commit c5c2a76

Please sign in to comment.