use of com.palantir.conjure.parser.ConjureSourceFile 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.parser.ConjureSourceFile 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.parser.ConjureSourceFile 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.parser.ConjureSourceFile in project conjure by palantir.
the class ServiceDefinitionTests method testParseConjureFile.
@Test
@SuppressWarnings("deprecation")
public void testParseConjureFile() throws IOException {
ConjureSourceFile def = ConjureParser.parse(new File("src/test/resources/test-service.yml"));
assertThat(def).isEqualTo(ConjureSourceFile.builder().types(TypesDefinition.builder().putImports(TypeName.of("ResourceIdentifier"), ExternalTypeDefinition.javaType("com.palantir.ri.ResourceIdentifier", PrimitiveType.STRING)).definitions(NamedTypesDefinition.builder().defaultConjurePackage(ConjurePackage.of("test.api")).putObjects(TypeName.of("SimpleObject"), ObjectTypeDefinition.builder().putFields(FieldName.of("stringField"), FieldDefinition.of(PrimitiveType.STRING)).build()).putObjects(TypeName.of("StringAlias"), AliasTypeDefinition.builder().alias(PrimitiveType.STRING).build()).build()).build()).putServices(TypeName.of("MyTestService"), ServiceDefinition.builder().doNotUseName("Test Service").conjurePackage(ConjurePackage.of("test.api")).putEndpoints("get", EndpointDefinition.builder().http(RequestLineDefinition.of("GET", PathString.of("/get"))).build()).putEndpoints("post", EndpointDefinition.builder().http(RequestLineDefinition.of("POST", PathString.of("/post"))).addTags("test-tag").args(ImmutableMap.of(ParameterName.of("foo"), ArgumentDefinition.builder().paramId(ParameterName.of("Foo")).paramType(ArgumentDefinition.ParamType.HEADER).type(LocalReferenceType.of(TypeName.of("StringAlias"))).addTags("safe").build())).build()).build()).build());
}
Aggregations