use of com.linkedin.avroutil1.parser.avsc.AvroParseContext in project avro-util by linkedin.
the class AvroUtilCodeGenOp method run.
@Override
public void run() throws Exception {
// mkdir any output folders that dont exist
if (!config.outputSpecificRecordClassesRoot.exists() && !config.outputSpecificRecordClassesRoot.mkdirs()) {
throw new IllegalStateException("unable to create destination folder " + config.outputSpecificRecordClassesRoot);
}
List<Path> avscFiles = new ArrayList<>();
for (File inputRoot : config.inputRoots) {
Files.walk(inputRoot.toPath()).filter(path -> path.getFileName().toString().endsWith("." + BuilderConsts.AVSC_EXTENSION)).forEach(avscFiles::add);
}
if (avscFiles.isEmpty()) {
LOGGER.warn("no input schema files were found under roots " + config.inputRoots);
return;
}
LOGGER.info("found " + avscFiles.size() + " avsc schema files");
AvroParseContext context = new AvroParseContext();
AvscParser parser = new AvscParser();
for (Path p : avscFiles) {
AvscParseResult fileParseResult = parser.parse(p);
Throwable parseError = fileParseResult.getParseError();
if (parseError != null) {
throw new IllegalArgumentException("failed to parse file " + p.toAbsolutePath(), parseError);
}
context.add(fileParseResult);
}
// resolve any references across files that are part of this op (anything left would be external)
context.resolveReferences();
if (context.hasExternalReferences()) {
// TODO - better formatting
throw new UnsupportedOperationException("unresolved referenced to external schemas: " + context.getExternalReferences());
}
throw new UnsupportedOperationException("TBD");
}
use of com.linkedin.avroutil1.parser.avsc.AvroParseContext in project avro-util by linkedin.
the class AvscParserTest method testParsingExternalReferences.
@Test
public void testParsingExternalReferences() throws Exception {
String referencingAvsc = TestUtil.load("schemas/TestRecordWithExternalReference.avsc");
String referencedAvsc = TestUtil.load("schemas/TestRecord.avsc");
AvscParser parser = new AvscParser();
AvscParseResult result1 = parser.parse(referencingAvsc);
Assert.assertEquals(result1.getExternalReferences().stream().map(SchemaOrRef::getRef).collect(Collectors.toList()), Collections.singletonList("com.acme.TestRecord"));
AvroRecordSchema outerSchema = (AvroRecordSchema) result1.getTopLevelSchema();
try {
outerSchema.getField("testRecordField").getSchema();
Assert.fail("accessing unresolved ref expected to fail");
} catch (UnresolvedReferenceException expected) {
Assert.assertTrue(expected.getMessage().contains("unresolved"));
Assert.assertTrue(expected.getMessage().contains("com.acme.TestRecord"));
}
AvscParseResult result2 = parser.parse(referencedAvsc);
AvroParseContext overall = new AvroParseContext();
overall.add(result1);
overall.add(result2);
overall.resolveReferences();
// now reference is resolved just fine
Assert.assertNotNull(outerSchema.getField("testRecordField").getSchema());
}
use of com.linkedin.avroutil1.parser.avsc.AvroParseContext in project avro-util by linkedin.
the class AvscParserTest method testParsingExternalReferencesWithNoNamespace.
@Test
public void testParsingExternalReferencesWithNoNamespace() throws Exception {
String referencingAvsc = TestUtil.load("schemas/TestRecordWithExternalReferenceAndInferredNamespace.avsc");
String referencedAvsc = TestUtil.load("schemas/TestRecord.avsc");
AvscParser parser = new AvscParser();
AvscParseResult result1 = parser.parse(referencingAvsc);
AvroRecordSchema outerSchema = (AvroRecordSchema) result1.getTopLevelSchema();
try {
outerSchema.getField("testRecordField").getSchema();
Assert.fail("accessing unresolved ref expected to fail");
} catch (UnresolvedReferenceException expected) {
Assert.assertTrue(expected.getMessage().contains("unresolved"));
Assert.assertTrue(expected.getMessage().contains("TestRecord"));
}
AvscParseResult result2 = parser.parse(referencedAvsc);
AvroParseContext overall = new AvroParseContext();
overall.add(result1);
overall.add(result2);
overall.resolveReferences();
// now reference is resolved just fine
Assert.assertNotNull(outerSchema.getField("testRecordField").getSchema());
}
Aggregations