Skip to content

Commit

Permalink
Reuse SpecificCompiler for all Schemas (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones authored Jan 14, 2025
1 parent 4fe9ca4 commit e904bb3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
19 changes: 9 additions & 10 deletions bridge/src/main/java/com/github/sbt/avro/AvroCompilerBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void compileIdls(File[] idls, File target) throws Exception {
Protocol protocol = parser.CompilationUnit();
SpecificCompiler compiler = new SpecificCompiler(protocol);
configureCompiler(compiler);
compiler.compileToDestination(idl, target);
compiler.compileToDestination(null, target);
}
}

Expand All @@ -101,15 +101,14 @@ public void compileAvscs(File[] avscs, File target) throws Exception {
System.out.println("Compiling Avro schema: " + schema);
files.add(schema);
}
Map<File, Schema> schemas = parser.parseFiles(files);

for (Map.Entry<File, Schema> entry: schemas.entrySet()) {
File file = entry.getKey();
Schema schema = entry.getValue();
SpecificCompiler compiler = new SpecificCompiler(schema);
configureCompiler(compiler);
compiler.compileToDestination(file, target);
}
List<Schema> schemas = parser.parseFiles(files);

// This is a trick to use a single instance on the SpecificCompiler for all schemas
// only avro 1.12+ SpecificCompiler has a constructor accepting a schema collection
Schema schema = Schema.createUnion(schemas);
SpecificCompiler compiler = new SpecificCompiler(schema);
configureCompiler(compiler);
compiler.compileToDestination(null, target);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions bridge/src/main/java/com/github/sbt/avro/AvscFilesParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void addTypes(Iterable<Schema> types) {
}
}

public Map<File, Schema> parseFiles(Collection<File> files) {
public List<Schema> parseFiles(Collection<File> files) {
Set<File> unparsedFiles = new HashSet<>(files);
Map<File, Schema> parsedFiles = new HashMap<>();
Map<File, Exception> parseExceptions = new HashMap<>();
Expand Down Expand Up @@ -72,7 +72,7 @@ public Map<File, Schema> parseFiles(Collection<File> files) {
throw new SchemaGenerationException("Can not parse schema files:\n" + failedFiles);
}

return parsedFiles;
return new ArrayList<>(parsedFiles.values());
}

private void stashParser(Schema.Parser parser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AvscFilesParserSpec extends Specification {

val sourceFiles = fullyQualifiedNames ++ simpleNames
val schemas = parser.parseFiles(sourceFiles.asJava)
val names = schemas.asScala.values.map(_.getFullName)
val names = schemas.asScala.map(_.getFullName)
names must contain(
exactly(
"com.github.sbt.avro.test.A",
Expand Down Expand Up @@ -69,7 +69,7 @@ class AvscFilesParserSpec extends Specification {
val parent = new File(sourceDir, "test_records.avsc")
parser.addTypes(Seq(dependant).asJava)
val schemas = parser.parseFiles(Seq(parent).asJava)
val names = schemas.asScala.values.map(_.getFullName)
val names = schemas.asScala.map(_.getFullName)
names must contain(exactly("com.github.sbt.avro.TestSpecificRecordParent"))
}

Expand Down

0 comments on commit e904bb3

Please sign in to comment.