Search in sources :

Example 6 with ArgumentDefinition

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()));
}
Also used : DefaultTypeVisitor(com.palantir.conjure.java.visitor.DefaultTypeVisitor) Endpoint(com.palantir.conjure.java.undertow.lib.Endpoint) HeaderAuthType(com.palantir.conjure.spec.HeaderAuthType) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) Modifier(javax.lang.model.element.Modifier) UndertowService(com.palantir.conjure.java.undertow.lib.UndertowService) HttpServerExchange(io.undertow.server.HttpServerExchange) AuthHeader(com.palantir.tokens.auth.AuthHeader) ClassName(com.squareup.javapoet.ClassName) ReturnValueWriter(com.palantir.conjure.java.undertow.lib.ReturnValueWriter) Collections2(com.google.common.collect.Collections2) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) StringUtils(org.apache.commons.lang3.StringUtils) Options(com.palantir.conjure.java.Options) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) HttpString(io.undertow.util.HttpString) TypeMarker(com.palantir.conjure.java.undertow.lib.TypeMarker) Duration(java.time.Duration) Map(java.util.Map) CodeBlocks(com.palantir.conjure.java.types.CodeBlocks) OptionalType(com.palantir.conjure.spec.OptionalType) MoreVisitors(com.palantir.conjure.java.visitor.MoreVisitors) HumanReadableDuration(com.palantir.humanreadabletypes.HumanReadableDuration) ImmutableSet(com.google.common.collect.ImmutableSet) AuthType(com.palantir.conjure.spec.AuthType) MoreCollectors(com.google.common.collect.MoreCollectors) Collection(java.util.Collection) Set(java.util.Set) TypeMapper(com.palantir.conjure.java.types.TypeMapper) Streams(com.google.common.collect.Streams) ParameterType(com.palantir.conjure.spec.ParameterType) JavaFile(com.squareup.javapoet.JavaFile) List(java.util.List) Tags(com.palantir.conjure.java.util.Tags) UndertowRuntime(com.palantir.conjure.java.undertow.lib.UndertowRuntime) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) Methods(io.undertow.util.Methods) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Iterables(com.google.common.collect.Iterables) ListType(com.palantir.conjure.spec.ListType) ExternalReference(com.palantir.conjure.spec.ExternalReference) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) AuthTypeVisitor(com.palantir.conjure.visitor.AuthTypeVisitor) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) Deque(java.util.Deque) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) BearerToken(com.palantir.tokens.auth.BearerToken) ImmutableList(com.google.common.collect.ImmutableList) ConjureTags(com.palantir.conjure.java.ConjureTags) AsyncRequestProcessingMetadata(com.palantir.conjure.java.services.UndertowTypeFunctions.AsyncRequestProcessingMetadata) SetType(com.palantir.conjure.spec.SetType) ParameterOrder(com.palantir.conjure.java.util.ParameterOrder) StatusCodes(io.undertow.util.StatusCodes) CodeBlock(com.squareup.javapoet.CodeBlock) Serializer(com.palantir.conjure.java.undertow.lib.Serializer) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) CookieAuthType(com.palantir.conjure.spec.CookieAuthType) JavaNameSanitizer(com.palantir.conjure.java.util.JavaNameSanitizer) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec) EndpointName(com.palantir.conjure.spec.EndpointName) HttpHandler(io.undertow.server.HttpHandler) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) Deserializer(com.palantir.conjure.java.undertow.lib.Deserializer) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) InputStream(java.io.InputStream) HeaderAuthType(com.palantir.conjure.spec.HeaderAuthType) OptionalType(com.palantir.conjure.spec.OptionalType) AuthType(com.palantir.conjure.spec.AuthType) ParameterType(com.palantir.conjure.spec.ParameterType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ListType(com.palantir.conjure.spec.ListType) Type(com.palantir.conjure.spec.Type) SetType(com.palantir.conjure.spec.SetType) CookieAuthType(com.palantir.conjure.spec.CookieAuthType) CodeBlock(com.squareup.javapoet.CodeBlock) HttpString(io.undertow.util.HttpString)

Example 7 with ArgumentDefinition

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;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) ArrayList(java.util.ArrayList)

Example 8 with ArgumentDefinition

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();
}
Also used : Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) ClassName(com.squareup.javapoet.ClassName) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Options(com.palantir.conjure.java.Options) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) MediaType(javax.ws.rs.core.MediaType) ClassNameVisitor(com.palantir.conjure.java.types.ClassNameVisitor) Matcher(java.util.regex.Matcher) Map(java.util.Map) ArgumentName(com.palantir.conjure.spec.ArgumentName) ParameterId(com.palantir.conjure.spec.ParameterId) ImmutableSet(com.google.common.collect.ImmutableSet) ParameterSpec(com.squareup.javapoet.ParameterSpec) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) AuthType(com.palantir.conjure.spec.AuthType) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) Collection(java.util.Collection) Set(java.util.Set) TypeMapper(com.palantir.conjure.java.types.TypeMapper) Collectors(java.util.stream.Collectors) ParameterType(com.palantir.conjure.spec.ParameterType) JavaFile(com.squareup.javapoet.JavaFile) List(java.util.List) Stream(java.util.stream.Stream) Paths(com.palantir.util.syntacticpath.Paths) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Joiner(com.google.common.base.Joiner) IntStream(java.util.stream.IntStream) SafeLoggerFactory(com.palantir.logsafe.logger.SafeLoggerFactory) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) AuthTypeVisitor(com.palantir.conjure.visitor.AuthTypeVisitor) HttpPath(com.palantir.conjure.spec.HttpPath) SafeLogger(com.palantir.logsafe.logger.SafeLogger) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Generator(com.palantir.conjure.java.Generator) ParameterOrder(com.palantir.conjure.java.util.ParameterOrder) Path(com.palantir.util.syntacticpath.Path) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Optional(java.util.Optional) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec)

Aggregations

ArgumentDefinition (com.palantir.conjure.spec.ArgumentDefinition)8 MethodSpec (com.squareup.javapoet.MethodSpec)5 EndpointDefinition (com.palantir.conjure.spec.EndpointDefinition)4 ArrayList (java.util.ArrayList)4 ImmutableList (com.google.common.collect.ImmutableList)3 ParameterType (com.palantir.conjure.spec.ParameterType)3 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 Map (java.util.Map)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)2 Options (com.palantir.conjure.java.Options)2 TypeMapper (com.palantir.conjure.java.types.TypeMapper)2 Packages (com.palantir.conjure.java.util.Packages)2 ParameterOrder (com.palantir.conjure.java.util.ParameterOrder)2 TypeFunctions (com.palantir.conjure.java.util.TypeFunctions)2 ArgumentName (com.palantir.conjure.spec.ArgumentName)2 AuthType (com.palantir.conjure.spec.AuthType)2 ServiceDefinition (com.palantir.conjure.spec.ServiceDefinition)2 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)2