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

Expose a TypeMarker factory to wrap an arbitrary Type #2221

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2221.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Expose a TypeMarker factory to wrap an arbitrary Type
links:
- https://github.com/palantir/dialogue/pull/2221
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand All @@ -56,8 +57,8 @@ final class ConjureBodySerDe implements BodySerDe {
private final Deserializer<InputStream> binaryInputStreamDeserializer;
private final Deserializer<Optional<InputStream>> optionalBinaryInputStreamDeserializer;
private final Deserializer<Void> emptyBodyDeserializer;
private final LoadingCache<TypeMarker<?>, Serializer<?>> serializers;
private final LoadingCache<TypeMarker<?>, Deserializer<?>> deserializers;
private final LoadingCache<Type, Serializer<?>> serializers;
private final LoadingCache<Type, Deserializer<?>> deserializers;

/**
* Selects the first (based on input order) of the provided encodings that
Expand Down Expand Up @@ -86,11 +87,11 @@ final class ConjureBodySerDe implements BodySerDe {
this.emptyBodyDeserializer = new EmptyBodyDeserializer(errorDecoder);
// Class unloading: Not supported, Jackson keeps strong references to the types
// it sees: https://github.com/FasterXML/jackson-databind/issues/489
this.serializers =
Caffeine.from(cacheSpec).build(token -> new EncodingSerializerRegistry<>(defaultEncoding, token));
this.serializers = Caffeine.from(cacheSpec)
.build(type -> new EncodingSerializerRegistry<>(defaultEncoding, TypeMarker.of(type)));
this.deserializers = Caffeine.from(cacheSpec)
.build(token -> new EncodingDeserializerRegistry<>(
encodingsSortedByWeight, errorDecoder, emptyContainerDeserializer, token));
.build(type -> new EncodingDeserializerRegistry<>(
encodingsSortedByWeight, errorDecoder, emptyContainerDeserializer, TypeMarker.of(type)));
}

private static List<WeightedEncoding> decorateEncodings(List<WeightedEncoding> input) {
Expand All @@ -112,13 +113,13 @@ private ImmutableList<Encoding> sortByWeight(List<WeightedEncoding> encodings) {
@Override
@SuppressWarnings("unchecked")
public <T> Serializer<T> serializer(TypeMarker<T> token) {
return (Serializer<T>) serializers.get(token);
return (Serializer<T>) serializers.get(token.getType());
}

@Override
@SuppressWarnings("unchecked")
public <T> Deserializer<T> deserializer(TypeMarker<T> token) {
return (Deserializer<T>) deserializers.get(token);
return (Deserializer<T>) deserializers.get(token.getType());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to this file would also make sense in a separate PR

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.palantir.dialogue;

import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -45,6 +46,10 @@ protected TypeMarker() {
}
}

private TypeMarker(Type type) {
this.type = Preconditions.checkNotNull(type, "Type is required");
}

public final Type getType() {
return type;
}
Expand All @@ -70,4 +75,15 @@ public final int hashCode() {
public final String toString() {
return "TypeMarker{type=" + type + '}';
}

/** Create a new {@link TypeMarker} instance wrapping the provided {@link Type}. */
public static TypeMarker<?> of(Type type) {
return new WrappingTypeMarker(type);
}

private static final class WrappingTypeMarker extends TypeMarker<Object> {
private WrappingTypeMarker(Type type) {
super(type);
}
}
}
Loading