Search in sources :

Example 1 with AvroParseContext

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");
}
Also used : Path(java.nio.file.Path) AvscParseResult(com.linkedin.avroutil1.parser.avsc.AvscParseResult) List(java.util.List) Logger(org.slf4j.Logger) AvroParseContext(com.linkedin.avroutil1.parser.avsc.AvroParseContext) Files(java.nio.file.Files) LoggerFactory(org.slf4j.LoggerFactory) AvscParser(com.linkedin.avroutil1.parser.avsc.AvscParser) Path(java.nio.file.Path) File(java.io.File) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) AvroParseContext(com.linkedin.avroutil1.parser.avsc.AvroParseContext) AvscParser(com.linkedin.avroutil1.parser.avsc.AvscParser) File(java.io.File) AvscParseResult(com.linkedin.avroutil1.parser.avsc.AvscParseResult)

Example 2 with AvroParseContext

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());
}
Also used : SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) Test(org.testng.annotations.Test)

Example 3 with AvroParseContext

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());
}
Also used : UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) Test(org.testng.annotations.Test)

Aggregations

AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)2 UnresolvedReferenceException (com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException)2 Test (org.testng.annotations.Test)2 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)1 AvroParseContext (com.linkedin.avroutil1.parser.avsc.AvroParseContext)1 AvscParseResult (com.linkedin.avroutil1.parser.avsc.AvscParseResult)1 AvscParser (com.linkedin.avroutil1.parser.avsc.AvscParser)1 File (java.io.File)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1