Search in sources :

Example 6 with EnumValueDefinition

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

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

the class EnumGenerator method createValueOf.

private static MethodSpec createValueOf(ClassName thisClass, List<EnumValueDefinition> values) {
    ParameterSpec param = ParameterSpec.builder(ClassName.get(String.class), "value").addAnnotation(Nonnull.class).build();
    CodeBlock.Builder parser = CodeBlock.builder().beginControlFlow("switch (upperCasedValue)");
    for (EnumValueDefinition value : values) {
        parser.add("case $S:\n", value.getValue()).indent().addStatement("return $L", value.getValue()).unindent();
    }
    parser.add("default:\n").indent().addStatement("return new $T(Value.UNKNOWN, upperCasedValue)", thisClass).unindent().endControlFlow();
    boolean anyDeprecatedValues = values.stream().anyMatch(definition -> definition.getDeprecated().isPresent());
    return MethodSpec.methodBuilder("valueOf").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(thisClass).addAnnotation(ConjureAnnotations.delegatingJsonCreator()).addAnnotations(anyDeprecatedValues ? ImmutableList.of(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build()) : ImmutableList.of()).addParameter(param).addStatement("$L", Expressions.requireNonNull(param.name, param.name + " cannot be null")).addStatement("String upperCasedValue = $N.toUpperCase($T.ROOT)", param, Locale.class).addCode(parser.build()).build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) Nonnull(javax.annotation.Nonnull) EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) CodeBlock(com.squareup.javapoet.CodeBlock)

Aggregations

EnumValueDefinition (com.palantir.conjure.spec.EnumValueDefinition)7 CodeBlock (com.squareup.javapoet.CodeBlock)3 ImmutableList (com.google.common.collect.ImmutableList)2 EnumDefinition (com.palantir.conjure.spec.EnumDefinition)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 List (java.util.List)2 Locale (java.util.Locale)2 JsonValue (com.fasterxml.jackson.annotation.JsonValue)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 CaseFormat (com.google.common.base.CaseFormat)1 Preconditions (com.google.common.base.Preconditions)1 Iterables (com.google.common.collect.Iterables)1 Immutable (com.google.errorprone.annotations.Immutable)1