Search in sources :

Example 1 with ReturnType

use of com.palantir.dialogue.annotations.processor.data.ReturnType in project dialogue by palantir.

the class ServiceImplementationGenerator method clientImpl.

private MethodSpec clientImpl(EndpointDefinition def) {
    List<ParameterSpec> params = def.arguments().stream().map(arg -> ParameterSpec.builder(ArgumentTypes.caseOf(arg.argType()).primitive((javaTypeName, _parameterSerializerMethodName, _isList) -> javaTypeName).rawRequestBody(typeName -> typeName).optional((optionalJavaType, _unused) -> optionalJavaType).mapType(typeName -> typeName).customType(typeName -> typeName), arg.argName().get()).build()).collect(Collectors.toList());
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(def.endpointName().get()).addModifiers(Modifier.PUBLIC).addParameters(params).addAnnotation(Override.class);
    methodBuilder.addCode("$T $L = $T.builder();", Request.Builder.class, REQUEST, Request.class);
    def.arguments().forEach(arg -> methodBuilder.addCode(generateParam(arg)));
    methodBuilder.returns(def.returns().returnType());
    boolean isAsync = def.returns().asyncInnerType().isPresent();
    String executeCode = isAsync ? "$L.clients().call($L, $L.build(), $L);" : "$L.clients().callBlocking($L, $L.build(), $L);";
    CodeBlock execute = CodeBlock.of(executeCode, serviceDefinition.conjureRuntimeArgName(), def.channelFieldName(), REQUEST, def.returns().deserializerFieldName());
    methodBuilder.addCode(!def.returns().isVoid() || isAsync ? "return $L" : "$L", execute);
    return methodBuilder.build();
}
Also used : OptionalType(com.palantir.dialogue.annotations.processor.data.ArgumentType.OptionalType) Modifier(javax.lang.model.element.Modifier) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) Serializer(com.palantir.dialogue.Serializer) ParameterEncoderType(com.palantir.dialogue.annotations.processor.data.ParameterEncoderType) Cases(com.palantir.dialogue.annotations.processor.data.ParameterType.Cases) ParameterSerializer(com.palantir.dialogue.annotations.ParameterSerializer) ErrorHandlingVoidDeserializer(com.palantir.dialogue.annotations.ErrorHandlingVoidDeserializer) Request(com.palantir.dialogue.Request) CodeBlock(com.squareup.javapoet.CodeBlock) DefaultParameterSerializer(com.palantir.dialogue.annotations.DefaultParameterSerializer) ParameterSpec(com.squareup.javapoet.ParameterSpec) ParameterTypes(com.palantir.dialogue.annotations.processor.data.ParameterTypes) Deserializer(com.palantir.dialogue.Deserializer) MethodSpec(com.squareup.javapoet.MethodSpec) EncoderType(com.palantir.dialogue.annotations.processor.data.ParameterEncoderType.EncoderType) ServiceDefinition(com.palantir.dialogue.annotations.processor.data.ServiceDefinition) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ReturnType(com.palantir.dialogue.annotations.processor.data.ReturnType) ArgumentTypes(com.palantir.dialogue.annotations.processor.data.ArgumentTypes) TypeSpec(com.squareup.javapoet.TypeSpec) Collectors(java.util.stream.Collectors) ErrorHandlingDeserializerFactory(com.palantir.dialogue.annotations.ErrorHandlingDeserializerFactory) EndpointChannel(com.palantir.dialogue.EndpointChannel) List(java.util.List) ArgumentType(com.palantir.dialogue.annotations.processor.data.ArgumentType) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) TypeMarker(com.palantir.dialogue.TypeMarker) ArgumentDefinition(com.palantir.dialogue.annotations.processor.data.ArgumentDefinition) EndpointDefinition(com.palantir.dialogue.annotations.processor.data.EndpointDefinition) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) Request(com.palantir.dialogue.Request) CodeBlock(com.squareup.javapoet.CodeBlock)

Aggregations

Deserializer (com.palantir.dialogue.Deserializer)1 EndpointChannel (com.palantir.dialogue.EndpointChannel)1 Request (com.palantir.dialogue.Request)1 Serializer (com.palantir.dialogue.Serializer)1 TypeMarker (com.palantir.dialogue.TypeMarker)1 DefaultParameterSerializer (com.palantir.dialogue.annotations.DefaultParameterSerializer)1 ErrorHandlingDeserializerFactory (com.palantir.dialogue.annotations.ErrorHandlingDeserializerFactory)1 ErrorHandlingVoidDeserializer (com.palantir.dialogue.annotations.ErrorHandlingVoidDeserializer)1 ParameterSerializer (com.palantir.dialogue.annotations.ParameterSerializer)1 ArgumentDefinition (com.palantir.dialogue.annotations.processor.data.ArgumentDefinition)1 ArgumentType (com.palantir.dialogue.annotations.processor.data.ArgumentType)1 OptionalType (com.palantir.dialogue.annotations.processor.data.ArgumentType.OptionalType)1 ArgumentTypes (com.palantir.dialogue.annotations.processor.data.ArgumentTypes)1 EndpointDefinition (com.palantir.dialogue.annotations.processor.data.EndpointDefinition)1 ParameterEncoderType (com.palantir.dialogue.annotations.processor.data.ParameterEncoderType)1 EncoderType (com.palantir.dialogue.annotations.processor.data.ParameterEncoderType.EncoderType)1 Cases (com.palantir.dialogue.annotations.processor.data.ParameterType.Cases)1 ParameterTypes (com.palantir.dialogue.annotations.processor.data.ParameterTypes)1 ReturnType (com.palantir.dialogue.annotations.processor.data.ReturnType)1 ServiceDefinition (com.palantir.dialogue.annotations.processor.data.ServiceDefinition)1