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;
}
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;
}
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();
}
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;
}
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;
}
Aggregations