Search in sources :

Example 6 with FieldName

use of com.palantir.conjure.spec.FieldName 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 7 with FieldName

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

the class BeanBuilderGenerator method createField.

private EnrichedField createField(FieldName fieldName, FieldDefinition field) {
    FieldSpec.Builder spec = FieldSpec.builder(typeMapper.getClassName(field.getType()), JavaNameSanitizer.sanitize(fieldName), Modifier.PRIVATE);
    if (field.getType().accept(TypeVisitor.IS_LIST)) {
        spec.initializer("new $T<>()", ArrayList.class);
    } else if (field.getType().accept(TypeVisitor.IS_SET)) {
        spec.initializer("new $T<>()", LinkedHashSet.class);
    } else if (field.getType().accept(TypeVisitor.IS_MAP)) {
        spec.initializer("new $T<>()", LinkedHashMap.class);
    } else if (field.getType().accept(TypeVisitor.IS_OPTIONAL)) {
        spec.initializer("$T.empty()", asRawType(typeMapper.getClassName(field.getType())));
    } else if (field.getType().accept(MoreVisitors.IS_INTERNAL_REFERENCE)) {
        com.palantir.conjure.spec.TypeName name = field.getType().accept(TypeVisitor.REFERENCE);
        typeMapper.getType(name).filter(definition -> definition.accept(TypeDefinitionVisitor.IS_ALIAS)).map(definition -> definition.accept(TypeDefinitionVisitor.ALIAS)).ifPresent(aliasDefinition -> {
            Type aliasType = aliasDefinition.getAlias();
            if (aliasType.accept(MoreVisitors.IS_COLLECTION) || aliasType.accept(TypeVisitor.IS_OPTIONAL)) {
                spec.initializer("$T.empty()", typeMapper.getClassName(field.getType()));
            }
        });
    }
    return EnrichedField.of(fieldName, field, spec.build());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) Modifier(javax.lang.model.element.Modifier) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) ClassName(com.squareup.javapoet.ClassName) Collections2(com.google.common.collect.Collections2) Options(com.palantir.conjure.java.Options) ByteBuffer(java.nio.ByteBuffer) MapType(com.palantir.conjure.spec.MapType) Map(java.util.Map) OptionalType(com.palantir.conjure.spec.OptionalType) MoreVisitors(com.palantir.conjure.java.visitor.MoreVisitors) EnumSet(java.util.EnumSet) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) ParameterSpec(com.squareup.javapoet.ParameterSpec) Collection(java.util.Collection) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) List(java.util.List) EnrichedField(com.palantir.conjure.java.types.BeanGenerator.EnrichedField) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Nulls(com.fasterxml.jackson.annotation.Nulls) ListType(com.palantir.conjure.spec.ListType) ExternalReference(com.palantir.conjure.spec.ExternalReference) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) FieldSpec(com.squareup.javapoet.FieldSpec) ConjureCollections(com.palantir.conjure.java.lib.internal.ConjureCollections) Type(com.palantir.conjure.spec.Type) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SafeArg(com.palantir.logsafe.SafeArg) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Buffer(java.nio.Buffer) SetType(com.palantir.conjure.spec.SetType) CodeBlock(com.squareup.javapoet.CodeBlock) LinkedHashSet(java.util.LinkedHashSet) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) 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) Collections(java.util.Collections) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) Preconditions(com.palantir.logsafe.Preconditions) MapType(com.palantir.conjure.spec.MapType) OptionalType(com.palantir.conjure.spec.OptionalType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ListType(com.palantir.conjure.spec.ListType) Type(com.palantir.conjure.spec.Type) SetType(com.palantir.conjure.spec.SetType) FieldSpec(com.squareup.javapoet.FieldSpec)

Aggregations

FieldName (com.palantir.conjure.spec.FieldName)7 FieldDefinition (com.palantir.conjure.spec.FieldDefinition)4 ListType (com.palantir.conjure.spec.ListType)4 MapType (com.palantir.conjure.spec.MapType)4 ObjectDefinition (com.palantir.conjure.spec.ObjectDefinition)4 OptionalType (com.palantir.conjure.spec.OptionalType)4 PrimitiveType (com.palantir.conjure.spec.PrimitiveType)4 SetType (com.palantir.conjure.spec.SetType)4 Type (com.palantir.conjure.spec.Type)4 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)4 CodeBlock (com.squareup.javapoet.CodeBlock)4 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)3 Collections2 (com.google.common.collect.Collections2)3 ImmutableList (com.google.common.collect.ImmutableList)3 ConjureAnnotations (com.palantir.conjure.java.ConjureAnnotations)3 Options (com.palantir.conjure.java.Options)3 JavaNameSanitizer (com.palantir.conjure.java.util.JavaNameSanitizer)3 TypeFunctions (com.palantir.conjure.java.util.TypeFunctions)3 MoreVisitors (com.palantir.conjure.java.visitor.MoreVisitors)3 TypeVisitor (com.palantir.conjure.visitor.TypeVisitor)3