Search in sources :

Example 1 with ArgumentName

use of com.palantir.conjure.spec.ArgumentName in project conjure-java by palantir.

the class Retrofit2ServiceGenerator method extractEncodedPathArgs.

private Set<ArgumentName> extractEncodedPathArgs(HttpPath path) {
    Pattern pathArg = Pattern.compile("\\{([^\\}]+)\\}");
    Matcher matcher = pathArg.matcher(path.toString());
    ImmutableSet.Builder<ArgumentName> encodedArgs = ImmutableSet.builder();
    while (matcher.find()) {
        String arg = matcher.group(1);
        if (arg.contains(":")) {
            // Strip everything after first colon
            encodedArgs.add(ArgumentName.of(arg.substring(0, arg.indexOf(':'))));
        }
    }
    return encodedArgs.build();
}
Also used : Pattern(java.util.regex.Pattern) ImmutableSet(com.google.common.collect.ImmutableSet) Matcher(java.util.regex.Matcher) ArgumentName(com.palantir.conjure.spec.ArgumentName)

Example 2 with ArgumentName

use of com.palantir.conjure.spec.ArgumentName in project conjure-java by palantir.

the class Retrofit2ServiceGenerator 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) {
    Set<ArgumentName> encodedPathArgs = extractEncodedPathArgs(endpointDef.getHttpPath());
    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, encodedPathArgs, queryArgs.subList(i, queryArgs.size())));
    }
    return alternateMethods;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) ArrayList(java.util.ArrayList) ArgumentName(com.palantir.conjure.spec.ArgumentName)

Example 3 with ArgumentName

use of com.palantir.conjure.spec.ArgumentName in project conjure-java by palantir.

the class Retrofit2ServiceGenerator method generateServiceMethod.

private MethodSpec generateServiceMethod(EndpointDefinition endpointDef, TypeMapper returnTypeMapper, TypeMapper argumentTypeMapper) {
    TypeName returnType = endpointDef.getReturns().map(returnTypeMapper::getClassName).orElse(ClassName.VOID);
    Set<ArgumentName> encodedPathArgs = extractEncodedPathArgs(endpointDef.getHttpPath());
    HttpPath endpointPathWithoutRegex = replaceEncodedPathArgs(endpointDef.getHttpPath());
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(endpointDef.getEndpointName().get()).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).addAnnotation(AnnotationSpec.builder(httpMethodToClassName(endpointDef.getHttpMethod().get().name())).addMember("value", "$S", "." + endpointPathWithoutRegex).build()).addAnnotation(AnnotationSpec.builder(ClassName.get("retrofit2.http", "Headers")).addMember("value", "$S", "hr-path-template: " + endpointPathWithoutRegex).addMember("value", "$S", "Accept: " + getReturnMediaType(returnType)).build());
    if (returnType.equals(BINARY_RETURN_TYPE) || returnType.equals(OPTIONAL_BINARY_RETURN_TYPE)) {
        methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("retrofit2.http", "Streaming")).build());
    }
    endpointDef.getDeprecated().ifPresent(deprecatedDocsValue -> methodBuilder.addAnnotation(ClassName.get("java.lang", "Deprecated")));
    methodBuilder.addAnnotations(ConjureAnnotations.getClientEndpointAnnotations(endpointDef));
    ServiceGenerators.getJavaDoc(endpointDef).ifPresent(content -> methodBuilder.addJavadoc("$L", content));
    methodBuilder.returns(ParameterizedTypeName.get(LISTENABLE_FUTURE_TYPE, returnType.box()));
    methodBuilder.addParameters(createServiceMethodParameters(endpointDef, argumentTypeMapper, encodedPathArgs));
    return methodBuilder.build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) HttpPath(com.palantir.conjure.spec.HttpPath) MethodSpec(com.squareup.javapoet.MethodSpec) ArgumentName(com.palantir.conjure.spec.ArgumentName)

Example 4 with ArgumentName

use of com.palantir.conjure.spec.ArgumentName in project conjure by palantir.

the class ConjureParserUtils method parseArgs.

private static List<ArgumentDefinition> parseArgs(Map<ParameterName, com.palantir.conjure.parser.services.ArgumentDefinition> args, HttpPath httpPath, ReferenceTypeResolver typeResolver) {
    ImmutableList.Builder<ArgumentDefinition> resultBuilder = ImmutableList.builder();
    for (Map.Entry<ParameterName, com.palantir.conjure.parser.services.ArgumentDefinition> entry : args.entrySet()) {
        com.palantir.conjure.parser.services.ArgumentDefinition original = entry.getValue();
        ArgumentName argName = ArgumentName.of(entry.getKey().name());
        ParameterType paramType = parseParameterType(original, argName, httpPath);
        ArgumentDefinition.Builder builder = ArgumentDefinition.builder().argName(argName).type(original.type().visit(new ConjureTypeParserVisitor(typeResolver))).paramType(paramType).docs(original.docs().map(Documentation::of)).safety(original.safety().map(ConjureParserUtils::parseLogSafety)).markers(parseMarkers(original.markers(), typeResolver)).tags(original.tags().stream().peek(tag -> Preconditions.checkArgument(!tag.isEmpty(), "tag must not be empty")).collect(Collectors.toSet()));
        resultBuilder.add(builder.build());
    }
    return resultBuilder.build();
}
Also used : BodyParameterType(com.palantir.conjure.spec.BodyParameterType) ParameterType(com.palantir.conjure.spec.ParameterType) PathParameterType(com.palantir.conjure.spec.PathParameterType) QueryParameterType(com.palantir.conjure.spec.QueryParameterType) HeaderParameterType(com.palantir.conjure.spec.HeaderParameterType) ImmutableList(com.google.common.collect.ImmutableList) Documentation(com.palantir.conjure.spec.Documentation) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) ParameterName(com.palantir.conjure.parser.services.ParameterName) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentName(com.palantir.conjure.spec.ArgumentName)

Example 5 with ArgumentName

use of com.palantir.conjure.spec.ArgumentName 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

ArgumentName (com.palantir.conjure.spec.ArgumentName)5 ArgumentDefinition (com.palantir.conjure.spec.ArgumentDefinition)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 HttpPath (com.palantir.conjure.spec.HttpPath)2 ParameterType (com.palantir.conjure.spec.ParameterType)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeName (com.squareup.javapoet.TypeName)2 ArrayList (java.util.ArrayList)2 Joiner (com.google.common.base.Joiner)1 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)1 Generator (com.palantir.conjure.java.Generator)1 Options (com.palantir.conjure.java.Options)1 ClassNameVisitor (com.palantir.conjure.java.types.ClassNameVisitor)1 DefaultClassNameVisitor (com.palantir.conjure.java.types.DefaultClassNameVisitor)1 SpecializeBinaryClassNameVisitor (com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor)1 TypeMapper (com.palantir.conjure.java.types.TypeMapper)1 Javadoc (com.palantir.conjure.java.util.Javadoc)1 Packages (com.palantir.conjure.java.util.Packages)1