Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backward compatible support for multiple extensions in CodeGenProvider #37081

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,27 @@ public interface CodeGenProvider {

/**
* File extension that CodeGenProvider will generate code from
* Deprecated: use inputExtensions instead
*
* @return file extension
*/
@Deprecated
default String inputExtension() {
return null;
}

/**
* File extensions that CodeGenProvider will generate code from
*
* @return file extensions
*/
@NotNull
String inputExtension();
default String[] inputExtensions() {
if (inputExtension() != null) {
return new String[] { inputExtension() };
}
return new String[] {};
}

/**
* Name of the directory containing input files for a given {@link CodeGenProvider} implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ class CodeGenWatcher {
final Config config = CodeGenerator.getConfig(curatedApplication.getApplicationModel(), LaunchMode.DEVELOPMENT,
properties, deploymentClassLoader);
for (CodeGenData codeGen : codeGens) {
watchers.add(new FSWatchUtil.Watcher(codeGen.sourceDir, codeGen.provider.inputExtension(),
modifiedPaths -> {
codeGenLock.lock();
try {
CodeGenerator.trigger(deploymentClassLoader,
codeGen,
curatedApplication.getApplicationModel(), config, false);
} catch (Exception any) {
log.warn("Code generation failed", any);
} finally {
codeGenLock.unlock();
}
}));
for (String ext : codeGen.provider.inputExtensions()) {
watchers.add(new FSWatchUtil.Watcher(codeGen.sourceDir, ext,
modifiedPaths -> {
codeGenLock.lock();
try {
CodeGenerator.trigger(deploymentClassLoader,
codeGen,
curatedApplication.getApplicationModel(), config, false);
} catch (Exception any) {
log.warn("Code generation failed", any);
} finally {
codeGenLock.unlock();
}
}));
}
}
fsWatchUtil = new FSWatchUtil();
fsWatchUtil.observe(watchers, 500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.avro.generic.GenericData;
import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -86,7 +88,8 @@ private Collection<Path> gatherAllFiles(Path importPath) throws CodeGenException
}
try {
return Files.find(importPath, 20,
(path, ignored) -> Files.isRegularFile(path) && path.toString().endsWith("." + inputExtension()))
(path, ignored) -> Files.isRegularFile(path)
&& Arrays.stream(inputExtensions()).anyMatch(ext -> path.toString().endsWith("." + ext)))
.map(Path::toAbsolutePath)
.collect(Collectors.toList());
} catch (IOException e) {
Expand Down Expand Up @@ -194,9 +197,11 @@ private boolean getBooleanProperty(String propName, boolean defaultValue) {
}

public String[] getImports(Config config) {
return config.getOptionalValue("avro.codegen." + inputExtension() + ".imports", String.class)
.map(i -> i.split(","))
.orElse(EMPTY);
return Arrays.stream(inputExtensions())
.flatMap(ext -> config.getOptionalValue("avro.codegen." + ext + ".imports", String.class)
.map(i -> Arrays.stream(i.split(","))).stream())
.reduce(Stream.empty(), Stream::concat)
.toArray(String[]::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public String providerId() {
}

@Override
public String inputExtension() {
return "avdl";
public String[] inputExtensions() {
return new String[] { "avdl" };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public String providerId() {
}

@Override
public String inputExtension() {
return "avpr";
public String[] inputExtensions() {
return new String[] { "avpr" };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public String providerId() {
}

@Override
public String inputExtension() {
return "avsc";
public String[] inputExtensions() {
return new String[] { "avsc" };
}

void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public String providerId() {
}

@Override
public String inputExtension() {
return "proto";
public String[] inputExtensions() {
return new String[] { "proto" };
}

@Override
Expand Down
Loading