use of com.palantir.conjure.spec.ArgumentDefinition 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.spec.ArgumentDefinition in project conjure-java by palantir.
the class JerseyServiceGenerator method generateCompatibilityBackfillServiceMethods.
/**
* Provides a linear expansion of optional query arguments to improve Java back-compat.
*/
private List<MethodSpec> generateCompatibilityBackfillServiceMethods(EndpointDefinition endpointDef, TypeMapper returnTypeMapper, TypeMapper argumentTypeMapper) {
List<ArgumentDefinition> queryArgs = new ArrayList<>();
for (ArgumentDefinition arg : endpointDef.getArgs()) {
if (arg.getParamType().accept(ParameterTypeVisitor.IS_QUERY) && arg.getType().accept(DefaultableTypeVisitor.INSTANCE)) {
queryArgs.add(arg);
}
}
List<MethodSpec> alternateMethods = new ArrayList<>();
for (int i = 0; i < queryArgs.size(); i++) {
alternateMethods.add(createCompatibilityBackfillMethod(endpointDef, returnTypeMapper, argumentTypeMapper, queryArgs.subList(i, queryArgs.size())));
}
return alternateMethods;
}
use of com.palantir.conjure.spec.ArgumentDefinition in project conjure-java by palantir.
the class Retrofit2ServiceGenerator method createCompatibilityBackfillMethod.
private MethodSpec createCompatibilityBackfillMethod(EndpointDefinition endpointDef, TypeMapper returnTypeMapper, TypeMapper argumentTypeMapper, Set<ArgumentName> encodedPathArgs, List<ArgumentDefinition> extraArgs) {
TypeName returnType = endpointDef.getReturns().map(returnTypeMapper::getClassName).orElse(ClassName.VOID);
// ensure the correct ordering of parameters by creating the complete sorted parameter list
List<ParameterSpec> sortedParams = createServiceMethodParameters(endpointDef, argumentTypeMapper, encodedPathArgs);
List<Optional<ArgumentDefinition>> sortedMaybeExtraArgs = sortedParams.stream().map(param -> extraArgs.stream().filter(arg -> arg.getArgName().get().equals(param.name)).findFirst()).collect(Collectors.toList());
// omit extraArgs from the back fill method signature
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(endpointDef.getEndpointName().get()).addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).addAnnotation(Deprecated.class).addParameters(IntStream.range(0, sortedParams.size()).filter(i -> !sortedMaybeExtraArgs.get(i).isPresent()).mapToObj(sortedParams::get).collect(Collectors.toList())).addAnnotations(ConjureAnnotations.getClientEndpointAnnotations(endpointDef));
endpointDef.getReturns().ifPresent(type -> methodBuilder.returns(ParameterizedTypeName.get(LISTENABLE_FUTURE_TYPE, returnType.box())));
// replace extraArgs with default values when invoking the complete method
StringBuilder sb = new StringBuilder(endpointDef.getReturns().isPresent() ? "return $N(" : "$N(");
List<Object> values = IntStream.range(0, sortedParams.size()).mapToObj(i -> {
Optional<ArgumentDefinition> maybeArgDef = sortedMaybeExtraArgs.get(i);
if (maybeArgDef.isPresent()) {
sb.append("$L, ");
return maybeArgDef.get().getType().accept(DefaultTypeValueVisitor.INSTANCE);
} else {
sb.append("$N, ");
return sortedParams.get(i);
}
}).collect(Collectors.toList());
// trim the end
sb.setLength(sb.length() - 2);
sb.append(")");
ImmutableList<Object> methodCallArgs = ImmutableList.builder().add(endpointDef.getEndpointName().get()).addAll(values).build();
methodBuilder.addStatement(sb.toString(), methodCallArgs.toArray(new Object[0]));
return methodBuilder.build();
}
Aggregations