Skip to content

Commit

Permalink
Use putAll instead of put for query and header params
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Kedida committed Dec 11, 2023
1 parent 82c4d76 commit 5cce107
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,45 +281,36 @@ private CodeBlock generatePlainSerializer(
Optional<ParameterEncoderType> maybeParameterEncoderType) {
return type.match(new ArgumentType.Cases<>() {
@Override
public CodeBlock primitive(TypeName _typeName, String parameterSerializerMethodName) {
public CodeBlock primitive(TypeName _typeName, String _parameterSerializerMethodName) {
return maybeParameterEncoderType.map(this::parameterEncoderType).orElseGet(() -> {
return CodeBlock.of(
"$L.$L($S, $L.$L($L));",
REQUEST,
singleValueMethod,
key,
PARAMETER_SERIALIZER,
parameterSerializerMethodName,
argName);
"$L.$L($S, $L);", REQUEST, singleValueMethod, key, generateSerializerCall(argName, type));
});
}

@Override
public CodeBlock list(TypeName _typeName, ListType listType) {
return maybeParameterEncoderType.map(this::parameterEncoderType).orElseGet(() -> {
CodeBlock elementName = CodeBlock.of("$L$L", argName, "Element");
CodeBlock elementCodeBlock = generatePlainSerializer(
singleValueMethod,
multiValueMethod,
key,
CodeBlock asList = CodeBlock.of(
"$L.stream().map($L -> $L).collect($T.toList())",
argName,
elementName,
listType.innerType(),
Optional.empty());
return CodeBlock.of("$L.forEach($L -> { $L });", argName, elementName, elementCodeBlock);
generateSerializerCall(elementName, listType.innerType()),
Collectors.class);
return CodeBlock.builder()
.add("$L.$L($S,", REQUEST, multiValueMethod, key)
.add(asList)
.add(");")
.build();
});
}

@Override
public CodeBlock alias(TypeName _typeName, String parameterSerializerMethodName) {
public CodeBlock alias(TypeName _typeName, String _parameterSerializerMethodName) {
return maybeParameterEncoderType.map(this::parameterEncoderType).orElseGet(() -> {
return CodeBlock.of(
"$L.$L($S, $L.$L($L.get()));",
REQUEST,
singleValueMethod,
key,
PARAMETER_SERIALIZER,
parameterSerializerMethodName,
argName);
"$L.$L($S, $L);", REQUEST, singleValueMethod, key, generateSerializerCall(argName, type));
});
}

Expand Down Expand Up @@ -392,4 +383,38 @@ public CodeBlock multimapParam() {
}
});
}

private CodeBlock generateSerializerCall(CodeBlock argName, ArgumentType type) {
return type.match(new ArgumentType.Cases<>() {
@Override
public CodeBlock primitive(TypeName _typeName, String parameterSerializerMethodName) {
return CodeBlock.of("$L.$L($L)", PARAMETER_SERIALIZER, parameterSerializerMethodName, argName);
}

@Override
public CodeBlock list(TypeName _typeName, ListType _listType) {
throw new UnsupportedOperationException("This should not happen");
}

@Override
public CodeBlock alias(TypeName _typeName, String parameterSerializerMethodName) {
return CodeBlock.of("$L.$L($L.get())", PARAMETER_SERIALIZER, parameterSerializerMethodName, argName);
}

@Override
public CodeBlock optional(TypeName _typeName, OptionalType _optionalType) {
throw new UnsupportedOperationException("This should not happen");
}

@Override
public CodeBlock rawRequestBody(TypeName _typeName) {
throw new UnsupportedOperationException("This should not happen");
}

@Override
public CodeBlock customType(TypeName _typeName) {
throw new UnsupportedOperationException("This should not happen");
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.annotation.processing.Generated;

@Generated("com.palantir.dialogue.annotations.processor.generate.DialogueServiceFactoryGenerator")
Expand Down Expand Up @@ -166,9 +167,11 @@ public final class MyServiceDialogueServiceFactory implements DialogueServiceFac
if (query5.isPresent()) {
_request.putQueryParams("q5", _parameterSerializer.serializeDouble(query5.getAsDouble()));
}
query6.forEach(query6Element -> {
_request.putQueryParams("q6", _parameterSerializer.serializeString(query6Element));
});
_request.putAllQueryParams(
"q6",
query6.stream()
.map(query6Element -> _parameterSerializer.serializeString(query6Element))
.collect(Collectors.toList()));
_request.putQueryParams("q7", _parameterSerializer.serializeString(query7.get()));
if (query8.isPresent()) {
_request.putQueryParams(
Expand Down Expand Up @@ -198,9 +201,11 @@ public final class MyServiceDialogueServiceFactory implements DialogueServiceFac
if (header5.isPresent()) {
_request.putHeaderParams("h5", _parameterSerializer.serializeDouble(header5.getAsDouble()));
}
header6.forEach(header6Element -> {
_request.putHeaderParams("h6", _parameterSerializer.serializeString(header6Element));
});
_request.putAllHeaderParams(
"h6",
header6.stream()
.map(header6Element -> _parameterSerializer.serializeString(header6Element))
.collect(Collectors.toList()));
_request.putHeaderParams("h7", _parameterSerializer.serializeString(header7.get()));
if (header8.isPresent()) {
_request.putHeaderParams(
Expand Down

0 comments on commit 5cce107

Please sign in to comment.