Search in sources :

Example 1 with TypeName

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

the class DialogueServiceGenerator method generate.

@Override
public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
    Map<TypeName, TypeDefinition> types = TypeFunctions.toTypesMap(conjureDefinition);
    DialogueEndpointsGenerator endpoints = new DialogueEndpointsGenerator(options);
    TypeMapper parameterTypes = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(new DefaultClassNameVisitor(types.keySet(), options), types, ClassName.get(BinaryRequestBody.class)));
    TypeMapper returnTypes = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(new DefaultClassNameVisitor(types.keySet(), options), types, ClassName.get(InputStream.class)));
    Map<TypeName, TypeDefinition> typeDefinitionsByName = conjureDefinition.getTypes().stream().collect(Collectors.toMap(type -> type.accept(TypeDefinitionVisitor.TYPE_NAME), Function.identity()));
    DialogueInterfaceGenerator interfaceGenerator = new DialogueInterfaceGenerator(options, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes));
    TypeNameResolver typeNameResolver = typeName -> Preconditions.checkNotNull(typeDefinitionsByName.get(typeName), "Referenced unknown TypeName", SafeArg.of("typeName", typeName));
    StaticFactoryMethodGenerator asyncGenerator = new DefaultStaticFactoryMethodGenerator(options, typeNameResolver, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes), StaticFactoryMethodType.ASYNC);
    StaticFactoryMethodGenerator blockingGenerator = new DefaultStaticFactoryMethodGenerator(options, typeNameResolver, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes), StaticFactoryMethodType.BLOCKING);
    return conjureDefinition.getServices().stream().flatMap(serviceDef -> Stream.of(endpoints.endpointsClass(serviceDef), interfaceGenerator.generateBlocking(serviceDef, blockingGenerator), interfaceGenerator.generateAsync(serviceDef, asyncGenerator)));
}
Also used : TypeMapper(com.palantir.conjure.java.types.TypeMapper) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) ClassName(com.squareup.javapoet.ClassName) TypeMapper(com.palantir.conjure.java.types.TypeMapper) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) BinaryRequestBody(com.palantir.dialogue.BinaryRequestBody) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Options(com.palantir.conjure.java.Options) JavaFile(com.squareup.javapoet.JavaFile) SafeArg(com.palantir.logsafe.SafeArg) Stream(java.util.stream.Stream) Generator(com.palantir.conjure.java.Generator) TypeName(com.palantir.conjure.spec.TypeName) Map(java.util.Map) Preconditions(com.palantir.logsafe.Preconditions) InputStream(java.io.InputStream) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) TypeName(com.palantir.conjure.spec.TypeName) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) TypeDefinition(com.palantir.conjure.spec.TypeDefinition)

Example 2 with TypeName

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

the class ConjureParserUtils method parseConjureDef.

static ConjureDefinition parseConjureDef(Map<String, AnnotatedConjureSourceFile> annotatedParsedDefs) {
    ImmutableList.Builder<ServiceDefinition> servicesBuilder = ImmutableList.builder();
    ImmutableList.Builder<ErrorDefinition> errorsBuilder = ImmutableList.builder();
    ImmutableList.Builder<TypeDefinition> typesBuilder = ImmutableList.builder();
    annotatedParsedDefs.values().forEach(annotatedParsed -> {
        ConjureSourceFile parsed = annotatedParsed.conjureSourceFile();
        try {
            ConjureTypeParserVisitor.ReferenceTypeResolver typeResolver = new ConjureTypeParserVisitor.ByParsedRepresentationTypeNameResolver(parsed.types(), annotatedParsed.importProviders(), annotatedParsedDefs);
            // Resolve objects first, so we can use them in service validations
            Map<TypeName, TypeDefinition> objects = parseObjects(parsed.types(), typeResolver);
            Map<TypeName, TypeDefinition> importedObjects = parseImportObjects(parsed.types().conjureImports(), annotatedParsedDefs);
            Map<TypeName, TypeDefinition> allObjects = new HashMap<>();
            allObjects.putAll(objects);
            allObjects.putAll(importedObjects);
            DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(allObjects);
            parsed.services().forEach((serviceName, service) -> {
                servicesBuilder.add(parseService(service, TypeName.of(serviceName.name(), parseConjurePackage(service.conjurePackage())), typeResolver, dealiasingVisitor));
            });
            typesBuilder.addAll(objects.values());
            errorsBuilder.addAll(parseErrors(parsed.types().definitions(), typeResolver));
        } catch (RuntimeException e) {
            throw new ConjureRuntimeException(String.format("Encountered error trying to parse file '%s'", annotatedParsed.sourceFile()), e);
        }
    });
    ConjureDefinition definition = ConjureDefinition.builder().version(Conjure.SUPPORTED_IR_VERSION).types(typesBuilder.build()).errors(errorsBuilder.build()).services(servicesBuilder.build()).build();
    ConjureDefinitionValidator.validateAll(definition);
    return definition;
}
Also used : ErrorDefinition(com.palantir.conjure.spec.ErrorDefinition) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) TypeName(com.palantir.conjure.spec.TypeName) ReferenceTypeResolver(com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition)

Example 3 with TypeName

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

the class ConjureParserUtils method createTypeName.

public static TypeName createTypeName(String name, com.palantir.conjure.parser.types.BaseObjectTypeDefinition def, Optional<String> defaultPackage) {
    TypeName type = TypeName.of(name, parsePackageOrElseThrow(def.conjurePackage(), defaultPackage));
    TypeNameValidator.validate(type);
    return type;
}
Also used : TypeName(com.palantir.conjure.spec.TypeName)

Example 4 with TypeName

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

the class ConjureParserUtils method innerParseImportObjects.

private static Map<TypeName, TypeDefinition> innerParseImportObjects(Map<Namespace, ConjureImports> conjureImports, Map<String, AnnotatedConjureSourceFile> externalTypes, Set<String> loadedFiles) {
    Map<TypeName, TypeDefinition> allDefinitions = new HashMap<>();
    conjureImports.values().forEach(conjureImport -> {
        String pathKey = conjureImport.absoluteFile().orElseThrow(() -> new SafeIllegalStateException("Absolute file MUST be resolved as part of parsing stage")).getAbsolutePath();
        // These structures are potentially recursive; load in any given conjure file once
        if (loadedFiles.contains(pathKey)) {
            return;
        }
        loadedFiles.add(pathKey);
        AnnotatedConjureSourceFile annotatedConjureSourceFile = externalTypes.get(pathKey);
        Preconditions.checkNotNull(annotatedConjureSourceFile, "Couldn't find import", UnsafeArg.of("file", conjureImport.file()));
        ConjureSourceFile conjureDef = annotatedConjureSourceFile.conjureSourceFile();
        Map<Namespace, String> importProviders = annotatedConjureSourceFile.importProviders();
        ReferenceTypeResolver importTypeResolver = new ConjureTypeParserVisitor.ByParsedRepresentationTypeNameResolver(conjureDef.types(), importProviders, externalTypes);
        allDefinitions.putAll(parseObjects(conjureDef.types(), importTypeResolver));
        allDefinitions.putAll(innerParseImportObjects(conjureDef.types().conjureImports(), externalTypes, loadedFiles));
    });
    return allDefinitions;
}
Also used : TypeName(com.palantir.conjure.spec.TypeName) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) ReferenceTypeResolver(com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver) HashMap(java.util.HashMap) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) PathString(com.palantir.conjure.parser.services.PathString) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) Namespace(com.palantir.conjure.parser.types.names.Namespace) ErrorNamespace(com.palantir.conjure.spec.ErrorNamespace) TypeDefinition(com.palantir.conjure.spec.TypeDefinition)

Example 5 with TypeName

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

the class ObjectDefinitionValidatorTest method testUniqueFieldNameValidator.

private void testUniqueFieldNameValidator(String fieldName1, String fieldName2) {
    FieldDefinition field1 = FieldDefinition.builder().fieldName(FieldName.of(fieldName1)).type(Type.primitive(PrimitiveType.STRING)).build();
    FieldDefinition field2 = FieldDefinition.builder().fieldName(FieldName.of(fieldName2)).type(Type.primitive(PrimitiveType.STRING)).build();
    TypeName name = TypeName.of("Foo", "package");
    ObjectDefinition definition = ObjectDefinition.builder().typeName(name).fields(field1).fields(field2).build();
    assertThatThrownBy(() -> ObjectDefinitionValidator.validate(definition)).isInstanceOf(IllegalArgumentException.class).hasMessage(String.format("ObjectDefinition must not contain duplicate field names " + "(modulo case normalization): %s vs %s", fieldName2, fieldName1));
}
Also used : TypeName(com.palantir.conjure.spec.TypeName) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition)

Aggregations

TypeName (com.palantir.conjure.spec.TypeName)13 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)6 ImmutableList (com.google.common.collect.ImmutableList)4 DealiasingTypeVisitor (com.palantir.conjure.visitor.DealiasingTypeVisitor)4 ReferenceTypeResolver (com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver)3 AnnotatedConjureSourceFile (com.palantir.conjure.parser.AnnotatedConjureSourceFile)3 ConjureSourceFile (com.palantir.conjure.parser.ConjureSourceFile)3 ConjureDefinition (com.palantir.conjure.spec.ConjureDefinition)3 ErrorDefinition (com.palantir.conjure.spec.ErrorDefinition)3 HashMap (java.util.HashMap)3 ConjureRuntimeException (com.palantir.conjure.exceptions.ConjureRuntimeException)2 DefaultClassNameVisitor (com.palantir.conjure.java.types.DefaultClassNameVisitor)2 SpecializeBinaryClassNameVisitor (com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor)2 TypeMapper (com.palantir.conjure.java.types.TypeMapper)2 PathString (com.palantir.conjure.parser.services.PathString)2 EndpointDefinition (com.palantir.conjure.spec.EndpointDefinition)2 ExternalReference (com.palantir.conjure.spec.ExternalReference)2 FieldDefinition (com.palantir.conjure.spec.FieldDefinition)2 ObjectDefinition (com.palantir.conjure.spec.ObjectDefinition)2 TypeDefinitionVisitor (com.palantir.conjure.visitor.TypeDefinitionVisitor)2