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));
}
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);
}
}
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();
}
});
}
Aggregations