use of com.linkedin.avroutil1.parser.exceptions.ParseException in project avro-util by linkedin.
the class CodeGenerator method parseSchemas.
private Map<String, SchemaDetails> parseSchemas(Set<File> toParse, Map<String, SchemaDetails> importableSchemas, Map<String, SchemaDetails> externalSchemas) {
Collection<File> toTry = toParse;
Map<String, SchemaDetails> successfullyParsed = new HashMap<>();
int passNumber = 0;
while (!toTry.isEmpty()) {
boolean madeProgress = false;
passNumber++;
Map<File, FileParseIssue> failedFiles = new HashMap<>();
for (File schemaFile : toTry) {
String fileContents = readFile(schemaFile);
SchemaParseResult result;
try {
// build up a collection of all known schemas to hand to avro
List<Schema> allKnownSchemas = buildUpKnownSchemaLists(successfullyParsed, importableSchemas, externalSchemas);
// call avro to parse our file given all the known schemas we've built up above
result = AvroCompatibilityHelper.parse(fileContents, SchemaParseConfiguration.STRICT, allKnownSchemas);
madeProgress = true;
} catch (SchemaParseException parseException) {
failedFiles.put(schemaFile, new FileParseIssue(schemaFile, parseException));
// to next file
continue;
} catch (Exception other) {
throw new IllegalStateException("while trying to parse file " + schemaFile, other);
}
// go over avro results and determine what schemas are new in the file parsed
Map<String, Schema> actuallyNewSchemas = processParseResults(schemaFile, result, successfullyParsed, importableSchemas, externalSchemas);
// store new schemas
actuallyNewSchemas.forEach((fullName, schema) -> {
if (successfullyParsed.put(fullName, SchemaDetails.fromFile(schema, schemaFile, schema == result.getMainSchema())) != null) {
throw new IllegalStateException();
}
});
}
if (!madeProgress) {
// classify our issues
failedFiles.forEach((file, issue) -> issue.setClassification(classifyIssue(issue.getException())));
// terminate for any issues that we cant handle
throwForFatalErrors(failedFiles, successfullyParsed, importableSchemas);
// if we got here none of the issues are fatal
if (allowClasspathLookup) {
ClasspathFishingResults fishingResults = goFishingOnTheClasspath(failedFiles);
Map<String, SchemaDetails> loot = fishingResults.getFqcnsFound();
if (!loot.isEmpty()) {
// yay, we live to iterate another day!
externalSchemas.putAll(loot);
} else {
throw new IllegalArgumentException("unable to find records " + fishingResults.getFqcnsNotFound() + " used in " + failedFiles.keySet() + ". not even on the classpath");
}
} else {
// TODO - improve on this error msg?
throw new IllegalArgumentException("cannot make progress. files left: " + failedFiles + ". did not examine classpath");
}
}
toTry = failedFiles.keySet();
}
LOG.info("parsed {} schemas out of {} files in {} passes", successfullyParsed.size(), toParse.size(), passNumber);
return successfullyParsed;
}
Aggregations