Skip to content

Commit

Permalink
Expose a TypeMarker factory to wrap an arbitrary Type (#2221)
Browse files Browse the repository at this point in the history
Expose a TypeMarker factory to wrap an arbitrary Type
  • Loading branch information
carterkozak authored Mar 22, 2024
1 parent 6f255a5 commit ce016a7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
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());
}

@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);
}
}
}

0 comments on commit ce016a7

Please sign in to comment.