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);
}
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);
}
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);
}
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");
}
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);
}
Aggregations