Skip to content

Commit

Permalink
Add avro IDL code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
glefloch committed Apr 7, 2021
1 parent 41cbf43 commit ede3f90
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class QuarkusGenerateCode extends QuarkusTask {
public static final String QUARKUS_GENERATED_SOURCES = "quarkus-generated-sources";
public static final String QUARKUS_TEST_GENERATED_SOURCES = "quarkus-test-generated-sources";
// TODO dynamically load generation provider, or make them write code directly in quarkus-generated-sources
public static final String[] CODE_GENERATION_PROVIDER = new String[] { "grpc", "avpr", "avsc" };
public static final String[] CODE_GENERATION_PROVIDER = new String[] { "grpc", "avdl", "avpr", "avsc" };

public static final String INIT_AND_RUN = "initAndRun";
private Set<Path> sourcesDirectories;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.avro.deployment;

import java.io.IOException;
import java.nio.file.Path;

import org.apache.avro.Protocol;
import org.apache.avro.compiler.idl.Idl;
import org.apache.avro.compiler.idl.ParseException;
import org.apache.avro.compiler.specific.SpecificCompiler;

import io.quarkus.bootstrap.prebuild.CodeGenException;
import io.quarkus.deployment.CodeGenProvider;

public class AvroIDLCodeGenProvider extends AvroCodeGenProviderBase implements CodeGenProvider {
@Override
public String providerId() {
return "avdl";
}

@Override
public String inputExtension() {
return "avdl";
}

@Override
void init() {

}

@Override
void compileSingleFile(Path filePath, Path outputDir, AvroOptions options) throws CodeGenException {
try (Idl parser = new Idl(filePath.toFile())) {
Protocol idlProtocol = parser.CompilationUnit();
String json = idlProtocol.toString(false);
Protocol protocol = Protocol.parse(json);
final SpecificCompiler compiler = new SpecificCompiler(protocol);
compiler.setTemplateDir(templateDirectory);
compiler.setStringType(options.stringType);
compiler.setFieldVisibility(SpecificCompiler.FieldVisibility.PRIVATE);
compiler.setCreateOptionalGetters(options.createOptionalGetters);
compiler.setGettersReturnOptional(options.gettersReturnOptional);
compiler.setOptionalGettersForNullableFieldsOnly(options.optionalGettersForNullableFieldsOnly);
compiler.setCreateSetters(options.createSetters);
compiler.setEnableDecimalLogicalType(options.enableDecimalLogicalType);

compiler.setOutputCharacterEncoding("UTF-8");
compiler.compileToDestination(filePath.toFile(), outputDir.toFile());
} catch (IOException | ParseException e) {
throw new CodeGenException("Failed to compile avro IDL file: " + filePath.toString() + " to Java", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
io.quarkus.avro.deployment.AvroIDLCodeGenProvider
io.quarkus.avro.deployment.AvroSchemaCodeGenProvider
io.quarkus.avro.deployment.AvroProtocolCodeGenProvider
13 changes: 13 additions & 0 deletions integration-tests/avro-reload/src/test/avro/Hello.avdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@namespace("test")
protocol Hello {

enum Level{
LOW, MEDIUM, HIGH
}

record Message {
string name;
string desc;
Level priority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ void shouldAlterProtocol() throws InterruptedException {
assertThat(when().get("/protocol").body().print().split(",")).containsExactlyInAnyOrder("Public", "Private", "Default");
}

@Test
void shouldAlterAvdl() throws InterruptedException {
assertThat(when().get("/avdl").body().print().split(",")).containsExactlyInAnyOrder("LOW", "MEDIUM", "HIGH");

test.modifyFile("avro/Hello.avdl",
text -> text.replaceAll(Pattern.quote("LOW, MEDIUM, HIGH"),
"LOWER, MEDIUM, HIGHEST"));
Thread.sleep(5000); // to wait for eager reload for code gen sources to happen
assertThat(when().get("/avdl").body().print().split(",")).containsExactlyInAnyOrder("LOWER", "MEDIUM", "HIGHEST");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import test.Level;
import test.PrivacyImport;
import test.ProtocolPrivacy;

Expand All @@ -23,4 +24,10 @@ public String getAvailableProtocolPrivacies() {
return Arrays.stream(ProtocolPrivacy.values()).map(String::valueOf).collect(joining(","));
}

@GET
@Path("/avdl")
public String getAvailableLevel() {
return Arrays.stream(Level.values()).map(String::valueOf).collect(joining(","));
}

}

0 comments on commit ede3f90

Please sign in to comment.