-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Handle multiple avro compiler versions in the same build * Fix test and solidify implementation * Cleanup interface * Introduce custom compiler option * Consistent settings * Disable JMX to avoid traces from avro class loader * Review comments * Rework optional getters option * Fix warning * Fix compiler bridge * set latest sbt version
- Loading branch information
1 parent
1406e60
commit 19ada60
Showing
119 changed files
with
834 additions
and
886 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.sbt.avro; | ||
|
||
import java.io.File; | ||
|
||
public interface AvroCompiler { | ||
|
||
void setStringType(String stringType); | ||
void setFieldVisibility(String fieldVisibility); | ||
void setUseNamespace(boolean useNamespace); | ||
void setEnableDecimalLogicalType(boolean enableDecimalLogicalType); | ||
void setCreateSetters(boolean createSetters); | ||
void setOptionalGetters(boolean optionalGetters); | ||
|
||
void recompile(Class<?>[] records, File target) throws Exception; | ||
void compileIdls(File[] idls, File target) throws Exception; | ||
void compileAvscs(AvroFileRef[] avscs, File target) throws Exception; | ||
void compileAvprs(File[] avprs, File target) throws Exception; | ||
} |
2 changes: 1 addition & 1 deletion
2
...com/github/sbt/avro/mojo/AvroFileRef.java → ...java/com/github/sbt/avro/AvroFileRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.github.sbt.avro.mojo; | ||
package com.github.sbt.avro; | ||
|
||
import java.io.File; | ||
import java.util.Objects; | ||
|
152 changes: 152 additions & 0 deletions
152
bridge/src/main/java/com/github/sbt/avro/AvroCompilerBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.github.sbt.avro; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.specific.SpecificRecord; | ||
|
||
import org.apache.avro.Protocol; | ||
import org.apache.avro.compiler.idl.Idl; | ||
import org.apache.avro.compiler.specific.SpecificCompiler; | ||
import org.apache.avro.compiler.specific.SpecificCompiler.FieldVisibility; | ||
import org.apache.avro.generic.GenericData.StringType; | ||
|
||
import java.io.File; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class AvroCompilerBridge implements AvroCompiler { | ||
|
||
private static final AvroVersion AVRO_1_9_0 = new AvroVersion(1, 9, 0); | ||
private static final AvroVersion AVRO_1_10_0 = new AvroVersion(1, 10, 0); | ||
|
||
private final AvroVersion avroVersion = AvroVersion.getRuntimeVersion(); | ||
|
||
private StringType stringType; | ||
private FieldVisibility fieldVisibility; | ||
private boolean useNamespace; | ||
private boolean enableDecimalLogicalType; | ||
private boolean createSetters; | ||
private boolean optionalGetters; | ||
|
||
protected Schema.Parser createParser() { | ||
return new Schema.Parser(); | ||
} | ||
|
||
@Override | ||
public void setStringType(String stringType) { | ||
this.stringType = StringType.valueOf(stringType); | ||
} | ||
|
||
@Override | ||
public void setFieldVisibility(String fieldVisibility) { | ||
this.fieldVisibility = FieldVisibility.valueOf(fieldVisibility); | ||
} | ||
|
||
@Override | ||
public void setUseNamespace(boolean useNamespace) { | ||
this.useNamespace = useNamespace; | ||
} | ||
|
||
@Override | ||
public void setEnableDecimalLogicalType(boolean enableDecimalLogicalType) { | ||
this.enableDecimalLogicalType = enableDecimalLogicalType; | ||
} | ||
|
||
@Override | ||
public void setCreateSetters(boolean createSetters) { | ||
this.createSetters = createSetters; | ||
} | ||
|
||
@Override | ||
public void setOptionalGetters(boolean optionalGetters) { | ||
this.optionalGetters = optionalGetters; | ||
} | ||
|
||
@Override | ||
public void recompile(Class<?>[] records, File target) throws Exception { | ||
AvscFilesCompiler compiler = new AvscFilesCompiler(this::createParser); | ||
compiler.setStringType(stringType); | ||
compiler.setFieldVisibility(fieldVisibility); | ||
compiler.setUseNamespace(useNamespace); | ||
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType); | ||
compiler.setCreateSetters(createSetters); | ||
if (avroVersion.compareTo(AVRO_1_9_0) >= 0) { | ||
compiler.setGettersReturnOptional(optionalGetters); | ||
} | ||
if (avroVersion.compareTo(AVRO_1_10_0) >= 0) { | ||
compiler.setOptionalGettersForNullableFieldsOnly(optionalGetters); | ||
} | ||
compiler.setTemplateDirectory("/org/apache/avro/compiler/specific/templates/java/classic/"); | ||
|
||
Set<Class<? extends SpecificRecord>> classes = new HashSet<>(); | ||
for (Class<?> record : records) { | ||
System.out.println("Recompiling Avro record: " + record.getName()); | ||
classes.add((Class<? extends SpecificRecord>) record); | ||
} | ||
compiler.compileClasses(classes, target); | ||
} | ||
|
||
@Override | ||
public void compileIdls(File[] idls, File target) throws Exception { | ||
for (File idl : idls) { | ||
System.out.println("Compiling Avro IDL: " + idl); | ||
Idl parser = new Idl(idl); | ||
Protocol protocol = parser.CompilationUnit(); | ||
SpecificCompiler compiler = new SpecificCompiler(protocol); | ||
compiler.setStringType(stringType); | ||
compiler.setFieldVisibility(fieldVisibility); | ||
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType); | ||
compiler.setCreateSetters(createSetters); | ||
if (avroVersion.compareTo(AVRO_1_9_0) >= 0) { | ||
compiler.setGettersReturnOptional(optionalGetters); | ||
} | ||
if (avroVersion.compareTo(AVRO_1_10_0) >= 0) { | ||
compiler.setOptionalGettersForNullableFieldsOnly(optionalGetters); | ||
} | ||
compiler.compileToDestination(null, target); | ||
} | ||
} | ||
|
||
@Override | ||
public void compileAvscs(AvroFileRef[] avscs, File target) throws Exception { | ||
AvscFilesCompiler compiler = new AvscFilesCompiler(this::createParser); | ||
compiler.setStringType(stringType); | ||
compiler.setFieldVisibility(fieldVisibility); | ||
compiler.setUseNamespace(useNamespace); | ||
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType); | ||
compiler.setCreateSetters(createSetters); | ||
if (avroVersion.compareTo(AVRO_1_9_0) >= 0) { | ||
compiler.setGettersReturnOptional(optionalGetters); | ||
} | ||
if (avroVersion.compareTo(AVRO_1_10_0) >= 0) { | ||
compiler.setOptionalGettersForNullableFieldsOnly(optionalGetters); | ||
} | ||
compiler.setTemplateDirectory("/org/apache/avro/compiler/specific/templates/java/classic/"); | ||
|
||
Set<AvroFileRef> files = new HashSet<>(); | ||
for (AvroFileRef ref : avscs) { | ||
System.out.println("Compiling Avro schema: " + ref.getFile()); | ||
files.add(ref); | ||
} | ||
compiler.compileFiles(Set.of(avscs), target); | ||
} | ||
|
||
@Override | ||
public void compileAvprs(File[] avprs, File target) throws Exception { | ||
for (File avpr : avprs) { | ||
System.out.println("Compiling Avro protocol: " + avpr); | ||
Protocol protocol = Protocol.parse(avpr); | ||
SpecificCompiler compiler = new SpecificCompiler(protocol); | ||
compiler.setStringType(stringType); | ||
compiler.setFieldVisibility(fieldVisibility); | ||
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType); | ||
compiler.setCreateSetters(createSetters); | ||
if (avroVersion.compareTo(AVRO_1_9_0) >= 0) { | ||
compiler.setGettersReturnOptional(optionalGetters); | ||
} | ||
if (avroVersion.compareTo(AVRO_1_10_0) >= 0) { | ||
compiler.setOptionalGettersForNullableFieldsOnly(optionalGetters); | ||
} | ||
compiler.compileToDestination(null, target); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.github.sbt.avro; | ||
|
||
import org.apache.avro.Schema; | ||
|
||
public class AvroVersion implements Comparable<AvroVersion> { | ||
|
||
private final int major; | ||
private final int minor; | ||
private final int patch; | ||
|
||
public AvroVersion(int major, int minor, int patch) { | ||
this.major = major; | ||
this.minor = minor; | ||
this.patch = patch; | ||
} | ||
|
||
int getMajor() { | ||
return major; | ||
} | ||
|
||
int getMinor() { | ||
return minor; | ||
} | ||
|
||
int getPatch() { | ||
return patch; | ||
} | ||
|
||
static AvroVersion getRuntimeVersion() { | ||
String[] parts = Schema.class.getPackage().getImplementationVersion().split("\\.", 3); | ||
int major = Integer.parseInt(parts[0]); | ||
int minor = Integer.parseInt(parts[1]); | ||
int patch = Integer.parseInt(parts[2]); | ||
return new AvroVersion(major, minor, patch); | ||
} | ||
|
||
|
||
@Override | ||
public int compareTo(AvroVersion o) { | ||
if (major != o.major) { | ||
return major - o.major; | ||
} else if (minor != o.minor) { | ||
return minor - o.minor; | ||
} else { | ||
return patch - o.patch; | ||
} | ||
} | ||
} |
Oops, something went wrong.