Search in sources :

Example 1 with AvscParser

use of com.linkedin.avroutil1.parser.avsc.AvscParser in project avro-util by linkedin.

the class SpecificRecordClassGeneratorTest method testSimpleFixedWithHugeDoc.

@Test
public void testSimpleFixedWithHugeDoc() throws Exception {
    String avsc = TestUtil.load("schemas/SimpleFixedWithHugeDoc.avsc");
    SpecificRecordClassGenerator generator = new SpecificRecordClassGenerator();
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroFixedSchema fixedSchema = (AvroFixedSchema) result.getTopLevelSchema();
    Assert.assertNotNull(fixedSchema);
    JavaFileObject javaSourceFile = generator.generateSpecificRecordClass(fixedSchema, SpecificRecordGenerationConfig.BROAD_COMPATIBILITY);
    CompilerHelper.assertCompiles(javaSourceFile);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) AvscParser(com.linkedin.avroutil1.parser.avsc.AvscParser) AvroFixedSchema(com.linkedin.avroutil1.model.AvroFixedSchema) AvscParseResult(com.linkedin.avroutil1.parser.avsc.AvscParseResult) Test(org.testng.annotations.Test)

Example 2 with AvscParser

use of com.linkedin.avroutil1.parser.avsc.AvscParser in project avro-util by linkedin.

the class SpecificRecordClassGeneratorTest method testSimpleFixed.

@Test
public void testSimpleFixed() throws Exception {
    String avsc = TestUtil.load("schemas/SimpleFixed.avsc");
    SpecificRecordClassGenerator generator = new SpecificRecordClassGenerator();
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroFixedSchema fixedSchema = (AvroFixedSchema) result.getTopLevelSchema();
    Assert.assertNotNull(fixedSchema);
    JavaFileObject javaSourceFile = generator.generateSpecificRecordClass(fixedSchema, SpecificRecordGenerationConfig.BROAD_COMPATIBILITY);
    CompilerHelper.assertCompiles(javaSourceFile);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) AvscParser(com.linkedin.avroutil1.parser.avsc.AvscParser) AvroFixedSchema(com.linkedin.avroutil1.model.AvroFixedSchema) AvscParseResult(com.linkedin.avroutil1.parser.avsc.AvscParseResult) Test(org.testng.annotations.Test)

Example 3 with AvscParser

use of com.linkedin.avroutil1.parser.avsc.AvscParser in project avro-util by linkedin.

the class SpecificRecordClassGeneratorTest method testSimpleEnum.

@Test
public void testSimpleEnum() throws Exception {
    String avsc = TestUtil.load("schemas/SimpleEnum.avsc");
    SpecificRecordClassGenerator generator = new SpecificRecordClassGenerator();
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroEnumSchema enumSchema = (AvroEnumSchema) result.getTopLevelSchema();
    Assert.assertNotNull(enumSchema);
    JavaFileObject javaSourceFile = generator.generateSpecificRecordClass(enumSchema, SpecificRecordGenerationConfig.BROAD_COMPATIBILITY);
    CompilerHelper.assertCompiles(javaSourceFile);
}
Also used : AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) JavaFileObject(javax.tools.JavaFileObject) AvscParser(com.linkedin.avroutil1.parser.avsc.AvscParser) AvscParseResult(com.linkedin.avroutil1.parser.avsc.AvscParseResult) Test(org.testng.annotations.Test)

Example 4 with AvscParser

use of com.linkedin.avroutil1.parser.avsc.AvscParser 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 5 with AvscParser

use of com.linkedin.avroutil1.parser.avsc.AvscParser in project avro-util by linkedin.

the class AvscParserTest method testSimpleParse.

@Test
public void testSimpleParse() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecord.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroSchema schema = result.getTopLevelSchema();
    Assert.assertNotNull(schema);
    Assert.assertEquals(schema.type(), AvroType.RECORD);
    AvroRecordSchema recordSchema = (AvroRecordSchema) schema;
    Assert.assertEquals(recordSchema.getFullName(), "com.acme.TestRecord");
    List<AvroSchemaField> fields = recordSchema.getFields();
    Assert.assertNotNull(fields);
    Assert.assertEquals(fields.size(), 11);
    Assert.assertEquals(fields.get(0).getPosition(), 0);
    Assert.assertEquals(fields.get(0).getName(), "booleanField");
    Assert.assertEquals(fields.get(0).getSchema().type(), AvroType.BOOLEAN);
    Assert.assertEquals(fields.get(1).getPosition(), 1);
    Assert.assertEquals(fields.get(1).getName(), "intField");
    Assert.assertEquals(fields.get(1).getSchema().type(), AvroType.INT);
    Assert.assertEquals(fields.get(2).getPosition(), 2);
    Assert.assertEquals(fields.get(2).getName(), "longField");
    Assert.assertEquals(fields.get(2).getSchema().type(), AvroType.LONG);
    Assert.assertEquals(fields.get(3).getPosition(), 3);
    Assert.assertEquals(fields.get(3).getName(), "floatField");
    Assert.assertEquals(fields.get(3).getSchema().type(), AvroType.FLOAT);
    Assert.assertEquals(fields.get(4).getPosition(), 4);
    Assert.assertEquals(fields.get(4).getName(), "doubleField");
    Assert.assertEquals(fields.get(4).getSchema().type(), AvroType.DOUBLE);
    Assert.assertEquals(fields.get(5).getPosition(), 5);
    Assert.assertEquals(fields.get(5).getName(), "bytesField");
    Assert.assertEquals(fields.get(5).getSchema().type(), AvroType.BYTES);
    Assert.assertEquals(fields.get(6).getPosition(), 6);
    Assert.assertEquals(fields.get(6).getName(), "stringField");
    Assert.assertEquals(fields.get(6).getSchema().type(), AvroType.STRING);
    Assert.assertEquals(fields.get(7).getPosition(), 7);
    Assert.assertEquals(fields.get(7).getName(), "enumField");
    Assert.assertEquals(fields.get(7).getSchema().type(), AvroType.ENUM);
    AvroEnumSchema simpleEnumSchema = (AvroEnumSchema) fields.get(7).getSchema();
    Assert.assertEquals(simpleEnumSchema.getFullName(), "innerNamespace.SimpleEnum");
    Assert.assertEquals(simpleEnumSchema.getSymbols(), Arrays.asList("A", "B", "C"));
    Assert.assertEquals(fields.get(8).getPosition(), 8);
    Assert.assertEquals(fields.get(8).getName(), "fixedField");
    Assert.assertEquals(fields.get(8).getSchema().type(), AvroType.FIXED);
    Assert.assertEquals(((AvroFixedSchema) fields.get(8).getSchema()).getFullName(), "com.acme.SimpleFixed");
    Assert.assertEquals(((AvroFixedSchema) fields.get(8).getSchema()).getSize(), 7);
    Assert.assertEquals(fields.get(9).getPosition(), 9);
    Assert.assertEquals(fields.get(9).getName(), "strArrayField");
    Assert.assertEquals(fields.get(9).getSchema().type(), AvroType.ARRAY);
    Assert.assertEquals(((AvroArraySchema) fields.get(9).getSchema()).getValueSchema().type(), AvroType.NULL);
    Assert.assertEquals(fields.get(10).getPosition(), 10);
    Assert.assertEquals(fields.get(10).getName(), "enumMapField");
    Assert.assertEquals(fields.get(10).getSchema().type(), AvroType.MAP);
    AvroSchema mapValueSchema = ((AvroMapSchema) fields.get(10).getSchema()).getValueSchema();
    Assert.assertSame(mapValueSchema, simpleEnumSchema);
}
Also used : AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)18 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)14 AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)9 AvscParseResult (com.linkedin.avroutil1.parser.avsc.AvscParseResult)6 AvscParser (com.linkedin.avroutil1.parser.avsc.AvscParser)6 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)5 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)5 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)4 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)4 UnresolvedReferenceException (com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException)4 JavaFileObject (javax.tools.JavaFileObject)4 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)3 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)3 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)3 AvroUnionSchema (com.linkedin.avroutil1.model.AvroUnionSchema)3 BigDecimal (java.math.BigDecimal)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Schema (org.apache.avro.Schema)3 AvroJavaStringRepresentation (com.linkedin.avroutil1.model.AvroJavaStringRepresentation)2