Search in sources :

Example 1 with OptionalType

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

the class BeanBuilderAuxiliarySettersUtils method createOptionalSetterBuilder.

public static MethodSpec.Builder createOptionalSetterBuilder(EnrichedField enriched, TypeMapper typeMapper, ClassName returnClass) {
    FieldSpec field = enriched.poetSpec();
    OptionalType type = enriched.conjureDef().getType().accept(TypeVisitor.OPTIONAL);
    return publicSetter(enriched, returnClass).addParameter(Parameters.nonnullParameter(typeMapper.getClassName(type.getItemType()), field.name));
}
Also used : OptionalType(com.palantir.conjure.spec.OptionalType) FieldSpec(com.squareup.javapoet.FieldSpec)

Example 2 with OptionalType

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

the class UndertowServiceHandlerGenerator method createConstructorForTypeWithReference.

/**
 * Returns a CodeBlock that constructs the given type given the provided variable that holds the decoded value for
 * the type. inType must be a type that resolves to {@code (primitive|optional<primitive>)} and must use an alias
 * type somewhere in its type definition. This means that inType must be one of the following:
 * - {@code optional<alias that resolves to a primitive>}
 * - alias of a primitive
 * - alias of an {@code optional<primitive>}
 * - alias of an {@code optional<alias that resolves to a primitive>}
 * - alias of an alias that follows one of these rules (recursive definition)
 *
 * An "alias that resolves to a primitive" is either an alias of a primitive or an alias of an alias that follows
 * these rules (recursive definition).
 */
private static CodeBlock createConstructorForTypeWithReference(Type inType, String decodedVarName, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typeDefinitions, TypeMapper typeMapper) {
    // "in" must be 1 of 2 types: optional<alias that resolves to a primitive> or alias
    if (inType.accept(TypeVisitor.IS_OPTIONAL)) {
        // optional<alias that resolves to a primitive>
        Type typeOfOptional = inType.accept(TypeVisitor.OPTIONAL).getItemType();
        return CodeBlock.of("$1T.ofNullable($2N.isPresent() ? $3L : null)", Optional.class, decodedVarName, createConstructorForTypeWithReference(typeOfOptional, decodedVarName + '.' + getOptionalAccessor(TypeFunctions.toConjureTypeWithoutAliases(typeOfOptional, typeDefinitions)) + "()", typeDefinitions, typeMapper));
    } else {
        // alias
        CodeBlock ofContent;
        // alias must resolve to one of:
        // * primitive
        // * optional<primitive>
        // * optional<alias that resolves to a primitive>
        // * alias that follows one of these rules (recursive definition)
        Type aliasedType = TypeFunctions.getReferencedType(inType, typeDefinitions);
        if (aliasedType.accept(TypeVisitor.IS_PRIMITIVE) || aliasedType.accept(MoreVisitors.IS_EXTERNAL)) {
            // primitive
            ofContent = CodeBlock.of("$1N", decodedVarName);
        } else if (aliasedType.accept(TypeVisitor.IS_OPTIONAL)) {
            // optional<primitive> or optional<alias that resolves to primitive>
            Type optionalType = aliasedType.accept(TypeVisitor.OPTIONAL).getItemType();
            if (optionalType.accept(TypeVisitor.IS_PRIMITIVE)) {
                // optional<primitive>
                // can use decoded var directly because it will already be the proper type (OptionalInt,
                // OptionalDouble, or Optional<Primitive>)
                ofContent = CodeBlock.of("$1N", decodedVarName);
            } else {
                // optional<alias that resolves to primitive>
                ofContent = createConstructorForTypeWithReference(aliasedType, decodedVarName, typeDefinitions, typeMapper);
            }
        } else {
            // alias
            ofContent = createConstructorForTypeWithReference(aliasedType, decodedVarName, typeDefinitions, typeMapper);
        }
        return CodeBlock.of("$1T.of($2L)", typeMapper.getClassName(inType), ofContent);
    }
}
Also used : 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)

Example 3 with OptionalType

use of com.palantir.conjure.spec.OptionalType in project conjure-postman by palantir.

the class BodyParameterTypeVisitor method visitBody.

@Override
public Optional<PostmanRequest.Body> visitBody(BodyParameterType _value) {
    TemplateTypeVisitor visitor = new TemplateTypeVisitor(types);
    Type type = argumentDefinition.getType();
    return type.accept(new Type.Visitor<Optional<PostmanRequest.Body>>() {

        @Override
        public Optional<PostmanRequest.Body> visitPrimitive(PrimitiveType value) {
            switch(value.get()) {
                case BINARY:
                    return Optional.of(PostmanRequest.FileBody.builder().build());
                default:
                    return rawBody(visitor.visitPrimitive(value));
            }
        }

        @Override
        public Optional<PostmanRequest.Body> visitOptional(OptionalType value) {
            return rawBody(visitor.visitOptional(value));
        }

        @Override
        public Optional<PostmanRequest.Body> visitList(ListType value) {
            return rawBody(visitor.visitList(value));
        }

        @Override
        public Optional<PostmanRequest.Body> visitSet(SetType value) {
            return rawBody(visitor.visitSet(value));
        }

        @Override
        public Optional<PostmanRequest.Body> visitMap(MapType value) {
            return rawBody(visitor.visitMap(value));
        }

        @Override
        public Optional<PostmanRequest.Body> visitReference(TypeName value) {
            return rawBody(visitor.visitReference(value));
        }

        @Override
        public Optional<PostmanRequest.Body> visitExternal(ExternalReference value) {
            return rawBody(visitor.visitExternal(value));
        }

        @Override
        public Optional<PostmanRequest.Body> visitUnknown(String _unknownType) {
            return Optional.empty();
        }
    });
}
Also used : TypeName(com.palantir.conjure.spec.TypeName) Optional(java.util.Optional) MapType(com.palantir.conjure.spec.MapType) ListType(com.palantir.conjure.spec.ListType) Type(com.palantir.conjure.spec.Type) BodyParameterType(com.palantir.conjure.spec.BodyParameterType) MapType(com.palantir.conjure.spec.MapType) OptionalType(com.palantir.conjure.spec.OptionalType) SetType(com.palantir.conjure.spec.SetType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) OptionalType(com.palantir.conjure.spec.OptionalType) ExternalReference(com.palantir.conjure.spec.ExternalReference) SetType(com.palantir.conjure.spec.SetType) ListType(com.palantir.conjure.spec.ListType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) PostmanRequest(com.palantir.conjure.postman.api.PostmanRequest)

Aggregations

OptionalType (com.palantir.conjure.spec.OptionalType)3 ListType (com.palantir.conjure.spec.ListType)2 PrimitiveType (com.palantir.conjure.spec.PrimitiveType)2 SetType (com.palantir.conjure.spec.SetType)2 Type (com.palantir.conjure.spec.Type)2 PostmanRequest (com.palantir.conjure.postman.api.PostmanRequest)1 AuthType (com.palantir.conjure.spec.AuthType)1 BodyParameterType (com.palantir.conjure.spec.BodyParameterType)1 CookieAuthType (com.palantir.conjure.spec.CookieAuthType)1 ExternalReference (com.palantir.conjure.spec.ExternalReference)1 HeaderAuthType (com.palantir.conjure.spec.HeaderAuthType)1 MapType (com.palantir.conjure.spec.MapType)1 ParameterType (com.palantir.conjure.spec.ParameterType)1 TypeName (com.palantir.conjure.spec.TypeName)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 Optional (java.util.Optional)1