Search in sources :

Example 1 with UnionDefinition

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

the class UnionGenerator method generateUnionType.

public static JavaFile generateUnionType(TypeMapper typeMapper, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typesMap, UnionDefinition typeDef, Options options) {
    com.palantir.conjure.spec.TypeName prefixedTypeName = Packages.getPrefixedName(typeDef.getTypeName(), options.packagePrefix());
    ClassName unionClass = ClassName.get(prefixedTypeName.getPackage(), prefixedTypeName.getName());
    ClassName baseClass = unionClass.nestedClass("Base");
    ClassName visitorClass = unionClass.nestedClass("Visitor");
    ClassName visitorBuilderClass = unionClass.nestedClass("VisitorBuilder");
    Map<FieldDefinition, TypeName> memberTypes = typeDef.getUnion().stream().collect(StableCollectors.toLinkedMap(Function.identity(), entry -> typeMapper.getClassName(entry.getType())));
    List<FieldSpec> fields = ImmutableList.of(FieldSpec.builder(baseClass, VALUE_FIELD_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
    TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(typeDef.getTypeName().getName()).addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(UnionGenerator.class)).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addFields(fields).addMethod(generateConstructor(baseClass)).addMethod(generateGetValue(baseClass)).addMethods(generateStaticFactories(typeMapper, unionClass, typeDef.getUnion())).addMethod(generateAcceptVisitMethod(visitorClass)).addType(generateVisitor(unionClass, visitorClass, memberTypes, visitorBuilderClass, options)).addType(generateVisitorBuilder(unionClass, visitorClass, visitorBuilderClass, memberTypes, options)).addTypes(generateVisitorBuilderStageInterfaces(unionClass, visitorClass, memberTypes, options)).addType(generateBase(baseClass, visitorClass, memberTypes)).addTypes(generateWrapperClasses(typeMapper, typesMap, baseClass, visitorClass, typeDef.getUnion(), options)).addType(generateUnknownWrapper(baseClass, visitorClass, options)).addMethod(generateEquals(unionClass)).addMethod(MethodSpecs.createEqualTo(unionClass, fields)).addMethod(MethodSpecs.createHashCode(fields)).addMethod(MethodSpecs.createToString(unionClass.simpleName(), fields.stream().map(fieldSpec -> FieldName.of(fieldSpec.name)).collect(Collectors.toList())));
    typeDef.getDocs().ifPresent(docs -> typeBuilder.addJavadoc("$L", Javadoc.render(docs)));
    return JavaFile.builder(prefixedTypeName.getPackage(), typeBuilder.build()).skipJavaLangImports(true).indent("    ").build();
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) BiFunction(java.util.function.BiFunction) ClassName(com.squareup.javapoet.ClassName) PeekingIterator(com.google.common.collect.PeekingIterator) StringUtils(org.apache.commons.lang3.StringUtils) Options(com.palantir.conjure.java.Options) JsonValue(com.fasterxml.jackson.annotation.JsonValue) Map(java.util.Map) JsonAnyGetter(com.fasterxml.jackson.annotation.JsonAnyGetter) StableCollectors(com.palantir.conjure.java.util.StableCollectors) ParameterSpec(com.squareup.javapoet.ParameterSpec) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) JavaFile(com.squareup.javapoet.JavaFile) JsonAnySetter(com.fasterxml.jackson.annotation.JsonAnySetter) List(java.util.List) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) TypeName(com.squareup.javapoet.TypeName) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Nulls(com.fasterxml.jackson.annotation.Nulls) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) DoubleFunction(java.util.function.DoubleFunction) HashMap(java.util.HashMap) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) SafeArg(com.palantir.logsafe.SafeArg) ImmutableList(com.google.common.collect.ImmutableList) Nonnull(javax.annotation.Nonnull) CodeBlock(com.squareup.javapoet.CodeBlock) IntFunction(java.util.function.IntFunction) TypeVariableName(com.squareup.javapoet.TypeVariableName) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) JavaNameSanitizer(com.palantir.conjure.java.util.JavaNameSanitizer) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) FieldName(com.palantir.conjure.spec.FieldName) Comparator(java.util.Comparator) Collections(java.util.Collections) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) TypeName(com.squareup.javapoet.TypeName) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with UnionDefinition

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

the class TemplateTypeVisitor method visitReference.

@SuppressWarnings("PreferSafeLoggingPreconditions")
@Override
public JsonNode visitReference(TypeName value) {
    TypeDefinition definition = types.get(value);
    TemplateTypeVisitor visitor = this;
    return definition.accept(new TypeDefinition.Visitor<JsonNode>() {

        @Override
        public JsonNode visitAlias(AliasDefinition value) {
            JsonNode wrapped = value.getAlias().accept(visitor);
            if (wrapped instanceof TextNode) {
                return new TextNode(String.format("{{ %s(%s) }}", value.getTypeName().getName(), wrapped.toString().replaceAll("[\"{}]", "")));
            }
            return wrapped;
        }

        @Override
        public JsonNode visitEnum(EnumDefinition value) {
            return new TextNode(value.getValues().stream().map(EnumValueDefinition::getValue).collect(Collectors.joining("|")));
        }

        @Override
        public JsonNode visitObject(ObjectDefinition value) {
            if (seenTypeStack.contains(value.getTypeName())) {
                return new TextNode(String.format("{{%s}}", value.getTypeName().getName()));
            }
            seenTypeStack.push(value.getTypeName());
            ObjectNode node = objectMapper.createObjectNode();
            value.getFields().forEach(fieldDefinition -> node.set(fieldDefinition.getFieldName().get(), fieldDefinition.getType().accept(visitor)));
            Preconditions.checkState(seenTypeStack.pop().equals(value.getTypeName()));
            return node;
        }

        @Override
        public JsonNode visitUnion(UnionDefinition value) {
            if (value.getUnion().isEmpty()) {
                return null;
            } else {
                if (seenTypeStack.contains(value.getTypeName())) {
                    return new TextNode(String.format("{{%s}}", value.getTypeName().getName()));
                }
                seenTypeStack.push(value.getTypeName());
                String unionTypes = value.getUnion().stream().map(FieldDefinition::getFieldName).map(FieldName::get).collect(Collectors.joining("|"));
                ObjectNode templates = objectMapper.createObjectNode();
                value.getUnion().forEach(field -> templates.set(field.getFieldName().get(), field.getType().accept(visitor)));
                JsonNode union = objectMapper.createObjectNode().put("type", unionTypes).set("oneOf", templates);
                Preconditions.checkState(seenTypeStack.pop().equals(value.getTypeName()));
                return union;
            }
        }

        @Override
        public JsonNode visitUnknown(String _unknownType) {
            return new TextNode("{{UNKNOWN}}");
        }
    });
}
Also used : ListType(com.palantir.conjure.spec.ListType) ExternalReference(com.palantir.conjure.spec.ExternalReference) Type(com.palantir.conjure.spec.Type) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) Function(java.util.function.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) MapType(com.palantir.conjure.spec.MapType) Map(java.util.Map) OptionalType(com.palantir.conjure.spec.OptionalType) JsonNode(com.fasterxml.jackson.databind.JsonNode) SetType(com.palantir.conjure.spec.SetType) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EnumDefinition(com.palantir.conjure.spec.EnumDefinition) EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) TextNode(com.fasterxml.jackson.databind.node.TextNode) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) List(java.util.List) AliasDefinition(com.palantir.conjure.spec.AliasDefinition) TypeName(com.palantir.conjure.spec.TypeName) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Preconditions(com.google.common.base.Preconditions) FieldName(com.palantir.conjure.spec.FieldName) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ArrayDeque(java.util.ArrayDeque) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AliasDefinition(com.palantir.conjure.spec.AliasDefinition) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) EnumDefinition(com.palantir.conjure.spec.EnumDefinition) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) FieldName(com.palantir.conjure.spec.FieldName)

Example 3 with UnionDefinition

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

the class TypeNameFormatterVisitor method visitReference.

@Override
public String visitReference(TypeName value) {
    TypeDefinition definition = types.get(value);
    TypeNameFormatterVisitor visitor = this;
    return definition.accept(new TypeDefinition.Visitor<String>() {

        @Override
        public String visitAlias(AliasDefinition value) {
            return String.format("%s(%s)", value.getTypeName().getName(), value.getAlias().accept(visitor));
        }

        @Override
        public String visitEnum(EnumDefinition value) {
            return value.getTypeName().getName();
        }

        @Override
        public String visitObject(ObjectDefinition value) {
            return value.getTypeName().getName();
        }

        @Override
        public String visitUnion(UnionDefinition value) {
            return value.getTypeName().getName();
        }

        @Override
        public String visitUnknown(String _unknownType) {
            return "UNKNOWN";
        }
    });
}
Also used : AliasDefinition(com.palantir.conjure.spec.AliasDefinition) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) EnumDefinition(com.palantir.conjure.spec.EnumDefinition) TypeDefinition(com.palantir.conjure.spec.TypeDefinition)

Example 4 with UnionDefinition

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

the class ConjureParserUtils method parseUnionType.

public static TypeDefinition parseUnionType(TypeName name, com.palantir.conjure.parser.types.complex.UnionTypeDefinition def, ConjureTypeParserVisitor.ReferenceTypeResolver typeResolver) {
    UnionDefinition unionType = UnionDefinition.builder().typeName(name).union(parseField(def.union(), typeResolver)).docs(def.docs().map(Documentation::of)).build();
    UnionDefinitionValidator.validateAll(unionType);
    return TypeDefinition.union(unionType);
}
Also used : Documentation(com.palantir.conjure.spec.Documentation) UnionDefinition(com.palantir.conjure.spec.UnionDefinition)

Example 5 with UnionDefinition

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

the class DealiasingTypeVisitor method visitReference.

@Override
public Either<TypeDefinition, Type> visitReference(TypeName value) {
    TypeDefinition typeDefinition = objects.get(value);
    Preconditions.checkState(typeDefinition != null, "Referenced TypeDefinition not found in map of types for TypeName: %s", value);
    return typeDefinition.accept(new TypeDefinition.Visitor<Either<TypeDefinition, Type>>() {

        @Override
        public Either<TypeDefinition, Type> visitAlias(AliasDefinition value) {
            // Recursively visit target of alias
            return value.getAlias().accept(DealiasingTypeVisitor.this);
        }

        @Override
        public Either<TypeDefinition, Type> visitEnum(EnumDefinition value) {
            return Either.left(TypeDefinition.enum_(value));
        }

        @Override
        public Either<TypeDefinition, Type> visitObject(ObjectDefinition value) {
            return Either.left(TypeDefinition.object(value));
        }

        @Override
        public Either<TypeDefinition, Type> visitUnion(UnionDefinition value) {
            return Either.left(TypeDefinition.union(value));
        }

        @Override
        public Either<TypeDefinition, Type> visitUnknown(String unknownType) {
            throw new IllegalStateException("Unsupported type: " + unknownType);
        }
    });
}
Also used : AliasDefinition(com.palantir.conjure.spec.AliasDefinition) Either(com.palantir.conjure.either.Either) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) EnumDefinition(com.palantir.conjure.spec.EnumDefinition) TypeDefinition(com.palantir.conjure.spec.TypeDefinition)

Aggregations

UnionDefinition (com.palantir.conjure.spec.UnionDefinition)5 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)4 AliasDefinition (com.palantir.conjure.spec.AliasDefinition)3 EnumDefinition (com.palantir.conjure.spec.EnumDefinition)3 ObjectDefinition (com.palantir.conjure.spec.ObjectDefinition)3 FieldDefinition (com.palantir.conjure.spec.FieldDefinition)2 FieldName (com.palantir.conjure.spec.FieldName)2 Type (com.palantir.conjure.spec.Type)2 List (java.util.List)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 JsonAnyGetter (com.fasterxml.jackson.annotation.JsonAnyGetter)1 JsonAnySetter (com.fasterxml.jackson.annotation.JsonAnySetter)1 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonSetter (com.fasterxml.jackson.annotation.JsonSetter)1 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonTypeName (com.fasterxml.jackson.annotation.JsonTypeName)1