Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritham Marupaka committed Jan 9, 2025
1 parent f00bd5f commit 9d1791d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ final class ConjureBodySerDe implements BodySerDe {
emptyContainerDeserializer,
BinaryEncoding.MARKER,
DeserializerArgs.<InputStream>builder()
.withBaseType(BinaryEncoding.MARKER)
.withExpectedResult(BinaryEncoding.MARKER)
.baseType(BinaryEncoding.MARKER)
.success(BinaryEncoding.MARKER)
.build());
this.optionalBinaryInputStreamDeserializer = new EncodingDeserializerForEndpointRegistry<>(
ImmutableList.of(BinaryEncoding.INSTANCE),
emptyContainerDeserializer,
BinaryEncoding.OPTIONAL_MARKER,
DeserializerArgs.<Optional<InputStream>>builder()
.withBaseType(BinaryEncoding.OPTIONAL_MARKER)
.withExpectedResult(BinaryEncoding.OPTIONAL_MARKER)
.baseType(BinaryEncoding.OPTIONAL_MARKER)
.success(BinaryEncoding.OPTIONAL_MARKER)
.build());
this.emptyBodyDeserializer =
new EmptyBodyDeserializer(new EndpointErrorDecoder<>(Collections.emptyMap(), Optional.empty()));
Expand All @@ -108,8 +108,8 @@ private <T> EncodingDeserializerForEndpointRegistry<?> buildCacheEntry(TypeMarke
emptyContainerDeserializer,
typeMarker,
DeserializerArgs.<T>builder()
.withBaseType(typeMarker)
.withExpectedResult(typeMarker)
.baseType(typeMarker)
.success(typeMarker)
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.CharStreams;
import com.google.common.net.HttpHeaders;
import com.google.common.primitives.Longs;
Expand Down Expand Up @@ -53,7 +55,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Extracts the error from a {@link Response}.
Expand All @@ -71,10 +72,8 @@ final class EndpointErrorDecoder<T> {
EndpointErrorDecoder(
Map<String, TypeMarker<? extends T>> errorNameToTypeMap, Optional<Encoding> maybeJsonEncoding) {
this.errorNameToJsonDeserializerMap = maybeJsonEncoding
.<Map<String, Encoding.Deserializer<? extends T>>>map(
jsonEncoding -> errorNameToTypeMap.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey, entry -> jsonEncoding.deserializer(entry.getValue()))))
.<Map<String, Encoding.Deserializer<? extends T>>>map(jsonEncoding ->
ImmutableMap.copyOf(Maps.transformValues(errorNameToTypeMap, jsonEncoding::deserializer)))
.orElseGet(Collections::emptyMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public void testDeserializeCustomError() throws IOException {
.code(500);
BodySerDe serializers = conjureBodySerDe("application/json", "text/plain");
DeserializerArgs<EndpointReturnBaseType> deserializerArgs = DeserializerArgs.<EndpointReturnBaseType>builder()
.withBaseType(new TypeMarker<>() {})
.withExpectedResult(new TypeMarker<ExpectedReturnValue>() {})
.withErrorType("Default:FailedPrecondition", new TypeMarker<ErrorReturnValue>() {})
.baseType(new TypeMarker<>() {})
.success(new TypeMarker<ExpectedReturnValue>() {})
.error("Default:FailedPrecondition", new TypeMarker<ErrorReturnValue>() {})
.build();

// When
Expand Down Expand Up @@ -155,8 +155,8 @@ public void testDeserializingUndefinedErrorFallsbackToSerializableError() throws
.code(500);
BodySerDe serializers = conjureBodySerDe("application/json", "text/plain");
DeserializerArgs<EndpointReturnBaseType> deserializerArgs = DeserializerArgs.<EndpointReturnBaseType>builder()
.withBaseType(new TypeMarker<>() {})
.withExpectedResult(new TypeMarker<ExpectedReturnValue>() {})
.baseType(new TypeMarker<>() {})
.success(new TypeMarker<ExpectedReturnValue>() {})
// Note: no error types are registered.
.build();

Expand Down Expand Up @@ -190,9 +190,9 @@ public void testDeserializeExpectedValue() {
.code(200);
BodySerDe serializers = conjureBodySerDe("application/json", "text/plain");
DeserializerArgs<EndpointReturnBaseType> deserializerArgs = DeserializerArgs.<EndpointReturnBaseType>builder()
.withBaseType(new TypeMarker<>() {})
.withExpectedResult(new TypeMarker<ExpectedReturnValue>() {})
.withErrorType("Default:FailedPrecondition", new TypeMarker<ErrorReturnValue>() {})
.baseType(new TypeMarker<>() {})
.success(new TypeMarker<ExpectedReturnValue>() {})
.error("Default:FailedPrecondition", new TypeMarker<ErrorReturnValue>() {})
.build();
// When
EndpointReturnBaseType value =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public final class DeserializerArgs<T> {
private final TypeMarker<T> baseType;
Expand All @@ -42,59 +43,48 @@ public static <T> Builder<T> builder() {

public static final class Builder<T> {
private boolean buildInvoked = false;
private TypeMarker<T> baseType;
private boolean baseTypeSet = false;
private TypeMarker<? extends T> expectedResultType;
private boolean expectedResultSet = false;
private @Nullable TypeMarker<T> baseType;
private @Nullable TypeMarker<? extends T> successType;
private final Map<String, TypeMarker<? extends T>> errorNameToTypeMarker;

@SuppressWarnings("NullAway")
// We ensure that the baseType and expectedResultType are set before building.
private Builder() {
this.errorNameToTypeMarker = new HashMap<>();
}

public Builder<T> withBaseType(@Nonnull TypeMarker<T> base) {
public Builder<T> baseType(@Nonnull TypeMarker<T> baseT) {
checkNotBuilt();
this.baseType = Preconditions.checkNotNull(base, "base type must be non-null");
this.baseTypeSet = true;
this.baseType = Preconditions.checkNotNull(baseT, "base type must be non-null");
return this;
}

public Builder<T> withExpectedResult(TypeMarker<? extends T> expectedResultT) {
public Builder<T> success(@Nonnull TypeMarker<? extends T> successT) {
checkNotBuilt();
this.expectedResultType =
Preconditions.checkNotNull(expectedResultT, "expected result type must be non-null");
this.expectedResultSet = true;
this.successType = Preconditions.checkNotNull(successT, "success type must be non-null");
return this;
}

public Builder<T> withErrorType(@Nonnull String errorName, @Nonnull TypeMarker<? extends T> errorType) {
public Builder<T> error(@Nonnull String errorName, @Nonnull TypeMarker<? extends T> errorT) {
checkNotBuilt();
this.errorNameToTypeMarker.put(
Preconditions.checkNotNull(errorName, "error name must be non-null"),
Preconditions.checkNotNull(errorType, "error type must be non-null"));
Preconditions.checkNotNull(errorT, "error type must be non-null"));
return this;
}

public DeserializerArgs<T> build() {
checkNotBuilt();
checkRequiredArgsSet();
Preconditions.checkNotNull(baseType, "base type must be set");
Preconditions.checkNotNull(successType, "success type must be set");
buildInvoked = true;
return new DeserializerArgs<>(baseType, expectedResultType, ImmutableMap.copyOf(errorNameToTypeMarker));
return new DeserializerArgs<>(baseType, successType, ImmutableMap.copyOf(errorNameToTypeMarker));
}

private void checkNotBuilt() {
Preconditions.checkState(!buildInvoked, "Build has already been called");
}

private void checkRequiredArgsSet() {
Preconditions.checkState(baseTypeSet, "base type must be set");
Preconditions.checkState(expectedResultSet, "expected result type must be set");
Preconditions.checkState(!buildInvoked, "build has already been called");
}
}

public TypeMarker<? extends T> baseType() {
public TypeMarker<T> baseType() {
return baseType;
}

Expand Down

0 comments on commit 9d1791d

Please sign in to comment.