Search in sources :

Example 1 with ServiceDefinition

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

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

the class ConjureParserUtils method parseService.

static ServiceDefinition parseService(com.palantir.conjure.parser.services.ServiceDefinition parsed, TypeName serviceName, ReferenceTypeResolver typeResolver, DealiasingTypeVisitor dealiasingVisitor) {
    List<EndpointDefinition> endpoints = new ArrayList<>();
    parsed.endpoints().forEach((name, def) -> endpoints.add(ConjureParserUtils.parseEndpoint(name, def, parsed.basePath(), parseAuthType(parsed.defaultAuth()), typeResolver, dealiasingVisitor)));
    ServiceDefinition service = ServiceDefinition.builder().serviceName(serviceName).docs(parsed.docs().map(Documentation::of)).addAllEndpoints(endpoints).build();
    ServiceDefinitionValidator.validateAll(service);
    return service;
}
Also used : ArrayList(java.util.ArrayList) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition)

Example 3 with ServiceDefinition

use of com.palantir.conjure.spec.ServiceDefinition in project conjure-python by palantir.

the class ClientGenerator method generateClient.

public PythonSnippet generateClient(ServiceDefinition serviceDef) {
    ImportTypeVisitor importTypeVisitor = new ImportTypeVisitor(serviceDef.getServiceName(), implTypeNameProcessor, implPackageNameProcessor);
    ImmutableSet.Builder<Type> referencedTypesBuilder = ImmutableSet.builder();
    List<PythonEndpointDefinition> endpoints = serviceDef.getEndpoints().stream().map(endpointDef -> generateEndpoint(endpointDef, referencedTypesBuilder)).collect(Collectors.toList());
    List<PythonImport> imports = referencedTypesBuilder.build().stream().flatMap(entry -> entry.accept(importTypeVisitor).stream()).collect(Collectors.toList());
    return PythonService.builder().pythonPackage(PythonPackage.of(implPackageNameProcessor.process(serviceDef.getServiceName().getPackage()))).className(implTypeNameProcessor.process(serviceDef.getServiceName())).definitionPackage(PythonPackage.of(definitionPackageNameProcessor.process(serviceDef.getServiceName().getPackage()))).definitionName(definitionTypeNameProcessor.process(serviceDef.getServiceName())).addAllImports(PythonService.CONJURE_IMPORTS).addAllImports(imports).docs(serviceDef.getDocs()).addAllEndpointDefinitions(endpoints).build();
}
Also used : ImportTypeVisitor(com.palantir.conjure.python.types.ImportTypeVisitor) PythonPackage(com.palantir.conjure.python.poet.PythonPackage) ImmutableSet(com.google.common.collect.ImmutableSet) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) PythonSnippet(com.palantir.conjure.python.poet.PythonSnippet) Type(com.palantir.conjure.spec.Type) ImportTypeVisitor(com.palantir.conjure.python.types.ImportTypeVisitor) PythonEndpointDefinition(com.palantir.conjure.python.poet.PythonEndpointDefinition) PythonEndpointParam(com.palantir.conjure.python.poet.PythonEndpointDefinition.PythonEndpointParam) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Collectors(java.util.stream.Collectors) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) PythonService(com.palantir.conjure.python.poet.PythonService) List(java.util.List) PythonTypeNameVisitor(com.palantir.conjure.python.types.PythonTypeNameVisitor) MyPyTypeNameVisitor(com.palantir.conjure.python.types.MyPyTypeNameVisitor) PythonImport(com.palantir.conjure.python.poet.PythonImport) PackageNameProcessor(com.palantir.conjure.python.processors.packagename.PackageNameProcessor) CaseConverter(com.palantir.conjure.CaseConverter) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) TypeNameProcessor(com.palantir.conjure.python.processors.typename.TypeNameProcessor) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Type(com.palantir.conjure.spec.Type) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ImmutableSet(com.google.common.collect.ImmutableSet) PythonImport(com.palantir.conjure.python.poet.PythonImport) PythonEndpointDefinition(com.palantir.conjure.python.poet.PythonEndpointDefinition)

Example 4 with ServiceDefinition

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

the class ConjureParserUtils method parseConjureDef.

static ConjureDefinition parseConjureDef(Map<String, AnnotatedConjureSourceFile> annotatedParsedDefs, SafetyDeclarationRequirements safetyDeclarations) {
    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, safetyDeclarations);
    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 5 with ServiceDefinition

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

the class DefaultStaticFactoryMethodGenerator method generate.

@Override
public MethodSpec generate(ServiceDefinition def) {
    ClassName className = getClassName(def);
    TypeSpec.Builder impl = TypeSpec.anonymousClassBuilder("").addSuperinterface(className);
    impl.addField(FieldSpec.builder(PlainSerDe.class, PLAIN_SER_DE).addModifiers(Modifier.PRIVATE, Modifier.FINAL).initializer(CodeBlock.of("$L.plainSerDe()", StaticFactoryMethodGenerator.RUNTIME)).build());
    def.getEndpoints().forEach(endpoint -> {
        endpoint.getArgs().stream().filter(arg -> arg.getParamType().accept(ParameterTypeVisitor.IS_BODY)).findAny().flatMap(body -> serializer(endpoint.getEndpointName(), body.getType())).ifPresent(impl::addField);
        impl.addField(bindEndpointChannel(def, endpoint));
        deserializer(endpoint.getEndpointName(), endpoint.getReturns()).ifPresent(impl::addField);
        impl.addMethod(clientImpl(endpoint));
    });
    impl.addMethod(DefaultStaticFactoryMethodGenerator.toStringMethod(className));
    String javadoc = methodType.switchBy("Creates a synchronous/blocking client for a $L service.", "Creates an " + "asynchronous/non-blocking client for a $L service.");
    MethodSpec method = MethodSpec.methodBuilder("of").addModifiers(Modifier.STATIC, Modifier.PUBLIC).addJavadoc(javadoc, def.getServiceName().getName()).returns(className).addParameter(EndpointChannelFactory.class, StaticFactoryMethodGenerator.ENDPOINT_CHANNEL_FACTORY).addParameter(ConjureRuntime.class, StaticFactoryMethodGenerator.RUNTIME).addCode(CodeBlock.builder().add("return $L;", impl.build()).build()).build();
    return method;
}
Also used : HeaderAuthType(com.palantir.conjure.spec.HeaderAuthType) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) Modifier(javax.lang.model.element.Modifier) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) ClassName(com.squareup.javapoet.ClassName) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Options(com.palantir.conjure.java.Options) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) MapType(com.palantir.conjure.spec.MapType) OptionalType(com.palantir.conjure.spec.OptionalType) ParameterSpec(com.squareup.javapoet.ParameterSpec) ImmutableMap(com.google.common.collect.ImmutableMap) AuthType(com.palantir.conjure.spec.AuthType) Deserializer(com.palantir.dialogue.Deserializer) BodyParameterType(com.palantir.conjure.spec.BodyParameterType) ParameterType(com.palantir.conjure.spec.ParameterType) Objects(java.util.Objects) List(java.util.List) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) TypeMarker(com.palantir.dialogue.TypeMarker) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) ListType(com.palantir.conjure.spec.ListType) Auth(com.palantir.conjure.java.services.Auth) ExternalReference(com.palantir.conjure.spec.ExternalReference) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) PathParameterType(com.palantir.conjure.spec.PathParameterType) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) Serializer(com.palantir.dialogue.Serializer) PlainSerDe(com.palantir.dialogue.PlainSerDe) SafeArg(com.palantir.logsafe.SafeArg) ConjureRuntime(com.palantir.dialogue.ConjureRuntime) SetType(com.palantir.conjure.spec.SetType) Request(com.palantir.dialogue.Request) CodeBlock(com.squareup.javapoet.CodeBlock) EndpointChannelFactory(com.palantir.dialogue.EndpointChannelFactory) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) CookieAuthType(com.palantir.conjure.spec.CookieAuthType) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) EndpointName(com.palantir.conjure.spec.EndpointName) EndpointChannel(com.palantir.dialogue.EndpointChannel) QueryParameterType(com.palantir.conjure.spec.QueryParameterType) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) HeaderParameterType(com.palantir.conjure.spec.HeaderParameterType) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) EndpointChannelFactory(com.palantir.dialogue.EndpointChannelFactory) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ServiceDefinition (com.palantir.conjure.spec.ServiceDefinition)5 EndpointDefinition (com.palantir.conjure.spec.EndpointDefinition)3 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)3 DealiasingTypeVisitor (com.palantir.conjure.visitor.DealiasingTypeVisitor)3 ImmutableList (com.google.common.collect.ImmutableList)2 ReferenceTypeResolver (com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver)2 ConjureRuntimeException (com.palantir.conjure.exceptions.ConjureRuntimeException)2 AnnotatedConjureSourceFile (com.palantir.conjure.parser.AnnotatedConjureSourceFile)2 ConjureSourceFile (com.palantir.conjure.parser.ConjureSourceFile)2 ConjureDefinition (com.palantir.conjure.spec.ConjureDefinition)2 ErrorDefinition (com.palantir.conjure.spec.ErrorDefinition)2 PrimitiveType (com.palantir.conjure.spec.PrimitiveType)2 Type (com.palantir.conjure.spec.Type)2 TypeName (com.palantir.conjure.spec.TypeName)2 ParameterTypeVisitor (com.palantir.conjure.visitor.ParameterTypeVisitor)2 TypeVisitor (com.palantir.conjure.visitor.TypeVisitor)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CaseConverter (com.palantir.conjure.CaseConverter)1 Options (com.palantir.conjure.java.Options)1