Search in sources :

Example 1 with ContainerType

use of com.palantir.conjure.java.undertow.processor.data.DefaultDecoderNames.ContainerType in project conjure-java by palantir.

the class ParamTypesResolver method getParamDecoder.

private CodeBlock getParamDecoder(VariableElement variableElement, AnnotationReflector annotationReflector) {
    // If the default marker interface is not used (overwritten by user), we want to use the user-provided decoder.
    TypeMirror typeMirror = annotationReflector.getAnnotationValue("decoder", TypeMirror.class);
    if (!context.isSameTypes(typeMirror, DefaultParamDecoder.class)) {
        return Instantiables.instantiate(typeMirror);
    }
    TypeMirror variableType = variableElement.asType();
    // For param decoders, we don't support list and set container types.
    Optional<TypeMirror> innerOptionalType = context.getGenericInnerType(Optional.class, variableType);
    TypeMirror decoderType = innerOptionalType.orElse(variableType);
    ContainerType decoderOutputType = getOutputType(Optional.empty(), Optional.empty(), innerOptionalType);
    return DefaultDecoderNames.getDefaultDecoderFactory(decoderType, ContainerType.NONE, decoderOutputType).orElseGet(() -> {
        context.reportError("No default decoder exists for parameter. " + "Types with a valueOf(String) method are supported, as are conjure types", variableElement, SafeArg.of("variableType", variableType), SafeArg.of("supportedTypes", DefaultDecoderNames.SUPPORTED_CLASSES));
        return CodeBlock.of("// error");
    });
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ContainerType(com.palantir.conjure.java.undertow.processor.data.DefaultDecoderNames.ContainerType) DefaultParamDecoder(com.palantir.conjure.java.undertow.annotations.DefaultParamDecoder)

Example 2 with ContainerType

use of com.palantir.conjure.java.undertow.processor.data.DefaultDecoderNames.ContainerType in project conjure-java by palantir.

the class ParamTypesResolver method getCollectionParamDecoder.

private CodeBlock getCollectionParamDecoder(VariableElement variableElement, AnnotationReflector annotationReflector) {
    // If the default marker interface is not used (overwritten by user), we want to use the user-provided decoder.
    TypeMirror typeMirror = annotationReflector.getAnnotationValue("decoder", TypeMirror.class);
    if (!context.isSameTypes(typeMirror, DefaultParamDecoder.class)) {
        return Instantiables.instantiate(typeMirror);
    }
    TypeMirror variableType = variableElement.asType();
    Optional<TypeMirror> innerListType = context.getGenericInnerType(List.class, variableType);
    Optional<TypeMirror> innerSetType = context.getGenericInnerType(Set.class, variableType);
    Optional<TypeMirror> innerOptionalType = context.getGenericInnerType(Optional.class, variableType);
    TypeMirror decoderType = innerListType.or(() -> innerSetType).or(() -> innerOptionalType).orElse(variableType);
    ContainerType decoderOutputType = getOutputType(innerListType, innerSetType, innerOptionalType);
    return DefaultDecoderNames.getDefaultDecoderFactory(decoderType, ContainerType.LIST, decoderOutputType).orElseGet(() -> {
        context.reportError("No default decoder exists for parameter. " + "Types with a valueOf(String) method are supported, as are conjure types", variableElement, SafeArg.of("variableType", variableType), SafeArg.of("supportedTypes", DefaultDecoderNames.SUPPORTED_CLASSES));
        return CodeBlock.of("// error");
    });
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ContainerType(com.palantir.conjure.java.undertow.processor.data.DefaultDecoderNames.ContainerType) DefaultParamDecoder(com.palantir.conjure.java.undertow.annotations.DefaultParamDecoder)

Example 3 with ContainerType

use of com.palantir.conjure.java.undertow.processor.data.DefaultDecoderNames.ContainerType in project conjure-java by palantir.

the class DefaultDecoderNamesTest method getParamDecoderArguments.

private static List<Arguments> getParamDecoderArguments() {
    List<Optional<String>> supportedClasses = Stream.concat(Stream.of(Optional.<String>empty()), DefaultDecoderNames.SUPPORTED_CLASSES.stream().map(Optional::of)).filter(maybeName -> maybeName.map(name -> !name.equals(OptionalDouble.class.getName()) && !name.equals(OptionalInt.class.getName())).orElse(true)).collect(Collectors.toList());
    ImmutableList.Builder<Arguments> arguments = ImmutableList.builder();
    // For 'ParamDecoder', we only support mapping to Optional as output container.
    for (ContainerType outputContainer : ImmutableList.of(ContainerType.NONE, ContainerType.OPTIONAL)) {
        arguments.addAll(getArgumentsForOutputContainer(ContainerType.NONE, outputContainer, supportedClasses));
    }
    // For 'CollectionParamDecoder', we support mapping to Optional, List, and Set as output container.
    for (ContainerType outputContainer : ContainerType.values()) {
        arguments.addAll(getArgumentsForOutputContainer(ContainerType.LIST, outputContainer, supportedClasses));
    }
    return arguments.build();
}
Also used : Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ContainerType(com.palantir.conjure.java.undertow.processor.data.DefaultDecoderNames.ContainerType) OptionalDouble(java.util.OptionalDouble) Set(java.util.Set) ParamDecoders(com.palantir.conjure.java.undertow.annotations.ParamDecoders) Arguments(org.junit.jupiter.params.provider.Arguments) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) Optional(java.util.Optional) Method(java.lang.reflect.Method) MethodSource(org.junit.jupiter.params.provider.MethodSource) Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) ContainerType(com.palantir.conjure.java.undertow.processor.data.DefaultDecoderNames.ContainerType) Arguments(org.junit.jupiter.params.provider.Arguments) OptionalInt(java.util.OptionalInt) OptionalDouble(java.util.OptionalDouble)

Aggregations

ContainerType (com.palantir.conjure.java.undertow.processor.data.DefaultDecoderNames.ContainerType)3 DefaultParamDecoder (com.palantir.conjure.java.undertow.annotations.DefaultParamDecoder)2 TypeMirror (javax.lang.model.type.TypeMirror)2 ImmutableList (com.google.common.collect.ImmutableList)1 ParamDecoders (com.palantir.conjure.java.undertow.annotations.ParamDecoders)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 OptionalDouble (java.util.OptionalDouble)1 OptionalInt (java.util.OptionalInt)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 Arguments (org.junit.jupiter.params.provider.Arguments)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1