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