Skip to content

Commit

Permalink
implement config priorities
Browse files Browse the repository at this point in the history
frantuma committed Jul 29, 2024
1 parent d51e61d commit 0d30fac
Showing 5 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -122,11 +122,20 @@ public static void main(String[] args) {

public static List<CodegenConfig> getExtensions() {
ServiceLoader<CodegenConfig> loader = ServiceLoader.load(CodegenConfig.class);
List<CodegenConfig> output = new ArrayList<CodegenConfig>();
for (CodegenConfig aLoader : loader) {
output.add(aLoader);
Map<String, CodegenConfig> output = new HashMap<>();

for (CodegenConfig config : loader) {
if (output.get(config.getName()) == null) {
output.put(config.getName(), config);
} else if (config.isPrivileged() && !output.get(config.getName()).isPrivileged()) {
output.put(config.getName(), config);
} else if (output.get(config.getName()).isPrivileged() && !config.isPrivileged()) {
// skip
} else if (config.getPriority() > output.get(config.getName()).getPriority()) {
output.put(config.getName(), config);
}
}
return output;
return new ArrayList<>(output.values());
}

static void usage(Options options) {
Original file line number Diff line number Diff line change
@@ -239,4 +239,16 @@ public interface CodegenConfig {
boolean defaultIgnoreImportMappingOption();

boolean isUsingFlattenSpec();

default boolean isPrivileged() {
return false;
}

default int getPriority() {
return Integer.MIN_VALUE;
}

default String getCodeName() {
return getName();
}
}
Original file line number Diff line number Diff line change
@@ -15,14 +15,25 @@ public static CodegenConfig forName(String name) {
ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class);

StringBuilder availableConfigs = new StringBuilder();

CodegenConfig current = null;
for (CodegenConfig config : loader) {

if (config.getName().equals(name)) {
return config;
if (current == null) {
current = config;
} else if (config.isPrivileged() && !current.isPrivileged()) {
current = config;
} else if (current.isPrivileged() && !config.isPrivileged()) {
// skip
} else if (config.getPriority() > current.getPriority()) {
current = config;
}
}

availableConfigs.append(config.getName()).append("\n");
}
if (current != null) {
return current;
}

// else try to load directly
try {
Original file line number Diff line number Diff line change
@@ -39,11 +39,20 @@ public static void main(String[] args) {

public static List<CodegenConfig> getExtensions() {
ServiceLoader<CodegenConfig> loader = ServiceLoader.load(CodegenConfig.class);
List<CodegenConfig> output = new ArrayList<CodegenConfig>();
Map<String, CodegenConfig> output = new HashMap<>();

for (CodegenConfig config : loader) {
output.add(config);
if (output.get(config.getName()) == null) {
output.put(config.getName(), config);
} else if (config.isPrivileged() && !output.get(config.getName()).isPrivileged()) {
output.put(config.getName(), config);
} else if (output.get(config.getName()).isPrivileged() && !config.isPrivileged()) {
// skip
} else if (config.getPriority() > output.get(config.getName()).getPriority()) {
output.put(config.getName(), config);
}
}
return output;
return new ArrayList<>(output.values());
}

static void usage(Options options) {
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1058,7 +1058,7 @@
</repositories>
<properties>
<maven.compiler.release>8</maven.compiler.release>
<swagger-parser-version>1.0.70</swagger-parser-version>
<swagger-parser-version>1.0.71</swagger-parser-version>
<scala-version>2.11.1</scala-version>
<felix-version>3.3.0</felix-version>
<swagger-core-version>1.6.14</swagger-core-version>
@@ -1075,7 +1075,7 @@
Codegen support of Java 8 should be removed if updating to 1.16+
-->
<jmustache-version>1.15</jmustache-version>
<testng-version>7.10.1</testng-version>
<testng-version>7.10.2</testng-version>
<surefire-version>3.0.0</surefire-version>
<jmockit-version>1.49</jmockit-version>
<mockito-version>5.11.0</mockito-version>

0 comments on commit 0d30fac

Please sign in to comment.