use of com.palantir.conjure.java.types.TypeMapper in project conjure-java by palantir.
the class JerseyServiceGenerator method generate.
@Override
public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
ClassName binaryReturnType = options.jerseyBinaryAsResponse() ? BINARY_RETURN_TYPE_RESPONSE : BINARY_RETURN_TYPE_OUTPUT;
TypeName optionalBinaryReturnType = options.jerseyBinaryAsResponse() ? BINARY_RETURN_TYPE_RESPONSE : OPTIONAL_BINARY_RETURN_TYPE;
Map<com.palantir.conjure.spec.TypeName, TypeDefinition> types = TypeFunctions.toTypesMap(conjureDefinition);
ClassNameVisitor defaultVisitor = new DefaultClassNameVisitor(types.keySet(), options);
TypeMapper returnTypeMapper = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(defaultVisitor, types, binaryReturnType, optionalBinaryReturnType));
TypeMapper argumentTypeMapper = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(defaultVisitor, types, BINARY_ARGUMENT_TYPE));
return conjureDefinition.getServices().stream().map(serviceDef -> generateService(serviceDef, returnTypeMapper, argumentTypeMapper));
}
use of com.palantir.conjure.java.types.TypeMapper in project conjure-java by palantir.
the class DialogueServiceGenerator method generate.
@Override
public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
Map<TypeName, TypeDefinition> types = TypeFunctions.toTypesMap(conjureDefinition);
DialogueEndpointsGenerator endpoints = new DialogueEndpointsGenerator(options);
TypeMapper parameterTypes = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(new DefaultClassNameVisitor(types.keySet(), options), types, ClassName.get(BinaryRequestBody.class)));
TypeMapper returnTypes = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(new DefaultClassNameVisitor(types.keySet(), options), types, ClassName.get(InputStream.class)));
Map<TypeName, TypeDefinition> typeDefinitionsByName = conjureDefinition.getTypes().stream().collect(Collectors.toMap(type -> type.accept(TypeDefinitionVisitor.TYPE_NAME), Function.identity()));
DialogueInterfaceGenerator interfaceGenerator = new DialogueInterfaceGenerator(options, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes));
TypeNameResolver typeNameResolver = typeName -> Preconditions.checkNotNull(typeDefinitionsByName.get(typeName), "Referenced unknown TypeName", SafeArg.of("typeName", typeName));
StaticFactoryMethodGenerator asyncGenerator = new DefaultStaticFactoryMethodGenerator(options, typeNameResolver, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes), StaticFactoryMethodType.ASYNC);
StaticFactoryMethodGenerator blockingGenerator = new DefaultStaticFactoryMethodGenerator(options, typeNameResolver, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes), StaticFactoryMethodType.BLOCKING);
return conjureDefinition.getServices().stream().flatMap(serviceDef -> Stream.of(endpoints.endpointsClass(serviceDef), interfaceGenerator.generateBlocking(serviceDef, blockingGenerator), interfaceGenerator.generateAsync(serviceDef, asyncGenerator)));
}
use of com.palantir.conjure.java.types.TypeMapper in project conjure-java by palantir.
the class UndertowServiceHandlerGenerator method endpointInvocation.
private CodeBlock endpointInvocation(EndpointDefinition endpointDefinition, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typeDefinitions, TypeMapper typeMapper, TypeMapper returnTypeMapper) {
CodeBlock.Builder code = CodeBlock.builder();
// auth code
Optional<String> authVarName = addAuthCode(code, endpointDefinition);
// body parameter
getBodyParamTypeArgument(endpointDefinition.getArgs()).ifPresent(bodyParam -> {
String paramName = sanitizeVarName(bodyParam.getArgName().get(), endpointDefinition);
Type dealiased = TypeFunctions.toConjureTypeWithoutAliases(bodyParam.getType(), typeDefinitions);
if (TypeFunctions.isBinaryOrOptionalBinary(dealiased)) {
code.addStatement("$1T $2N = $3N.bodySerDe().deserializeInputStream($4N)", InputStream.class, paramName, RUNTIME_VAR_NAME, EXCHANGE_VAR_NAME);
} else {
code.addStatement("$1T $2N = $3N.deserialize($4N)", typeMapper.getClassName(bodyParam.getType()).box(), paramName, DESERIALIZER_VAR_NAME, EXCHANGE_VAR_NAME);
}
code.add(generateParamMetadata(bodyParam, bodyParam.getArgName().get(), paramName, typeMapper));
});
// path parameters
addPathParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
// header parameters
addHeaderParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
// query parameters
addQueryParamsCode(code, endpointDefinition, typeDefinitions, typeMapper);
List<CodeBlock> methodArgs = new ArrayList<>();
authVarName.ifPresent(name -> methodArgs.add(CodeBlock.of("$N", name)));
ParameterOrder.sorted(endpointDefinition.getArgs()).stream().map(arg -> arg.getArgName().get()).map(arg -> sanitizeVarName(arg, endpointDefinition)).map(arg -> CodeBlock.of("$N", arg)).forEach(methodArgs::add);
if (Tags.hasServerRequestContext(endpointDefinition)) {
methodArgs.add(CodeBlock.of("$N.contexts().createContext($N, this)", RUNTIME_VAR_NAME, EXCHANGE_VAR_NAME));
}
Optional<AsyncRequestProcessingMetadata> async = UndertowTypeFunctions.async(endpointDefinition, options);
if (async.isPresent() || endpointDefinition.getReturns().isPresent()) {
code.addStatement("$1T $2N = $3N.$4L($5L)", async.isPresent() ? UndertowTypeFunctions.getAsyncReturnType(endpointDefinition, returnTypeMapper, options) : returnTypeMapper.getClassName(endpointDefinition.getReturns().get()), RESULT_VAR_NAME, DELEGATE_VAR_NAME, JavaNameSanitizer.sanitize(endpointDefinition.getEndpointName().get()), methodArgs.stream().collect(CodeBlock.joining(",")));
} else {
code.addStatement("$1N.$2L($3L)", DELEGATE_VAR_NAME, endpointDefinition.getEndpointName(), methodArgs.stream().collect(CodeBlock.joining(",")));
}
if (async.isPresent()) {
AsyncRequestProcessingMetadata metadata = async.get();
if (metadata.timeout().isPresent()) {
HumanReadableDuration timeout = metadata.timeout().get();
code.add(CodeBlocks.statement("$N.async().register($N, this, $T.ofMillis(/* $L */ $L), $N)", RUNTIME_VAR_NAME, RESULT_VAR_NAME, Duration.class, timeout.toString(), timeout.toMilliseconds(), EXCHANGE_VAR_NAME));
} else {
code.add(CodeBlocks.statement("$1N.async().register($2N, this, $3N)", RUNTIME_VAR_NAME, RESULT_VAR_NAME, EXCHANGE_VAR_NAME));
}
} else {
code.add(generateReturnValueCodeBlock(endpointDefinition, typeDefinitions));
}
return code.build();
}
use of com.palantir.conjure.java.types.TypeMapper in project conjure-java by palantir.
the class UndertowServiceHandlerGenerator method generateParameterCodeBlock.
private CodeBlock generateParameterCodeBlock(EndpointDefinition endpoint, ParameterType.Visitor<Boolean> paramTypeVisitor, String paramsVarName, Function<ArgumentDefinition, String> toParamId, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typeDefinitions, TypeMapper typeMapper) {
return CodeBlocks.of(endpoint.getArgs().stream().filter(param -> param.getParamType().accept(paramTypeVisitor)).map(arg -> {
Type normalizedType = TypeFunctions.toConjureTypeWithoutAliases(arg.getType(), typeDefinitions);
String paramName = sanitizeVarName(arg.getArgName().get(), endpoint);
final CodeBlock retrieveParam;
if (normalizedType.equals(arg.getType()) || // Collections of alias types are handled the same way as external imports
TypeFunctions.isListOrSet(arg.getType())) {
// type is not an alias or optional of an alias
retrieveParam = decodePlainParameterCodeBlock(arg.getType(), typeMapper, paramName, paramsVarName, toParamId.apply(arg));
} else {
// type contains aliases: decode raw value and then construct real value from raw one
String rawVarName = arg.getArgName().get() + "Raw";
retrieveParam = CodeBlocks.of(decodePlainParameterCodeBlock(normalizedType, typeMapper, rawVarName, paramsVarName, toParamId.apply(arg)), CodeBlocks.statement("$1T $2N = $3L", typeMapper.getClassName(arg.getType()), paramName, createConstructorForTypeWithReference(arg.getType(), rawVarName, typeDefinitions, typeMapper)));
}
return CodeBlocks.of(retrieveParam, generateParamMetadata(arg, arg.getArgName().get(), paramName, typeMapper));
}).collect(ImmutableList.toImmutableList()));
}
use of com.palantir.conjure.java.types.TypeMapper in project conjure-java by palantir.
the class Retrofit2ServiceGenerator method generate.
@Override
public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
Map<com.palantir.conjure.spec.TypeName, TypeDefinition> types = TypeFunctions.toTypesMap(conjureDefinition);
ClassNameVisitor defaultVisitor = new DefaultClassNameVisitor(types.keySet(), options);
TypeMapper returnTypeMapper = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(defaultVisitor, types, BINARY_RETURN_TYPE, OPTIONAL_BINARY_RETURN_TYPE));
TypeMapper argumentTypeMapper = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(defaultVisitor, types, BINARY_ARGUMENT_TYPE));
return conjureDefinition.getServices().stream().map(serviceDef -> generateService(serviceDef, returnTypeMapper, argumentTypeMapper));
}
Aggregations