Skip to content

Commit

Permalink
implement config priorities
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Jul 22, 2024
1 parent 2768e05 commit 5c88061
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.swagger.codegen.v3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -122,11 +123,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,16 @@ public interface CodegenConfig {
default boolean checkAliasModel() {
return false;
}

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
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,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) {
Expand Down

0 comments on commit 5c88061

Please sign in to comment.