Search in sources :

Example 1 with ParameterId

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

the class JerseyServiceGenerator method getParamTypeAnnotation.

private Optional<AnnotationSpec> getParamTypeAnnotation(ArgumentDefinition def) {
    AnnotationSpec.Builder annotationSpecBuilder;
    ParameterType paramType = def.getParamType();
    if (paramType.accept(ParameterTypeVisitor.IS_PATH)) {
        annotationSpecBuilder = AnnotationSpec.builder(ClassName.get("javax.ws.rs", "PathParam")).addMember("value", "$S", def.getArgName().get());
    } else if (paramType.accept(ParameterTypeVisitor.IS_QUERY)) {
        ParameterId paramId = paramType.accept(ParameterTypeVisitor.QUERY).getParamId();
        annotationSpecBuilder = AnnotationSpec.builder(ClassName.get("javax.ws.rs", "QueryParam")).addMember("value", "$S", paramId.get());
    } else if (paramType.accept(ParameterTypeVisitor.IS_HEADER)) {
        ParameterId paramId = paramType.accept(ParameterTypeVisitor.HEADER).getParamId();
        annotationSpecBuilder = AnnotationSpec.builder(ClassName.get("javax.ws.rs", "HeaderParam")).addMember("value", "$S", paramId.get());
    } else if (paramType.accept(ParameterTypeVisitor.IS_BODY)) {
        if (def.getType().accept(TypeVisitor.IS_OPTIONAL) || !options.requireNotNullAuthAndBodyParams()) {
            return Optional.empty();
        }
        annotationSpecBuilder = AnnotationSpec.builder(NOT_NULL);
    } else {
        throw new IllegalStateException("Unrecognized argument type: " + def.getParamType());
    }
    return Optional.of(annotationSpecBuilder.build());
}
Also used : ParameterType(com.palantir.conjure.spec.ParameterType) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ParameterId(com.palantir.conjure.spec.ParameterId)

Example 2 with ParameterId

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

the class Retrofit2ServiceGenerator method createEndpointParameter.

private ParameterSpec createEndpointParameter(TypeMapper argumentTypeMapper, Set<ArgumentName> encodedPathArgs, ArgumentDefinition def) {
    ParameterSpec.Builder param = ParameterSpec.builder(argumentTypeMapper.getClassName(def.getType()), def.getArgName().get());
    ParameterType paramType = def.getParamType();
    if (paramType.accept(ParameterTypeVisitor.IS_PATH)) {
        AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get("retrofit2.http", "Path")).addMember("value", "$S", def.getArgName().get());
        if (encodedPathArgs.contains(def.getArgName())) {
            builder.addMember("encoded", "$L", true);
        }
        param.addAnnotation(builder.build());
    } else if (paramType.accept(ParameterTypeVisitor.IS_QUERY)) {
        ParameterId paramId = paramType.accept(ParameterTypeVisitor.QUERY).getParamId();
        param.addAnnotation(AnnotationSpec.builder(ClassName.get("retrofit2.http", "Query")).addMember("value", "$S", paramId.get()).build());
    } else if (paramType.accept(ParameterTypeVisitor.IS_HEADER)) {
        ParameterId paramId = paramType.accept(ParameterTypeVisitor.HEADER).getParamId();
        param.addAnnotation(AnnotationSpec.builder(ClassName.get("retrofit2.http", "Header")).addMember("value", "$S", paramId.get()).build());
    } else if (paramType.accept(ParameterTypeVisitor.IS_BODY)) {
        param.addAnnotation(ClassName.get("retrofit2.http", "Body"));
    } else {
        throw new IllegalStateException("Unrecognized argument type: " + def.getParamType());
    }
    return param.build();
}
Also used : ParameterType(com.palantir.conjure.spec.ParameterType) ParameterSpec(com.squareup.javapoet.ParameterSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ParameterId(com.palantir.conjure.spec.ParameterId)

Aggregations

ParameterId (com.palantir.conjure.spec.ParameterId)2 ParameterType (com.palantir.conjure.spec.ParameterType)2 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)2 ParameterSpec (com.squareup.javapoet.ParameterSpec)1