122 lines
3.7 KiB
Java
122 lines
3.7 KiB
Java
package com.aicheck.config;
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class AppConfig {
|
|
private boolean enabled = true;
|
|
private boolean dtoEntityConversionEnabled = true;
|
|
private List<String> modelDirs = new ArrayList<>();
|
|
private List<String> controllerScanDirs = new ArrayList<>();
|
|
private List<String> feignScanDirs = new ArrayList<>();
|
|
private List<String> conversionScanDirs = new ArrayList<>();
|
|
private String wecomWebhookUrl = "";
|
|
private boolean onlyOnChange = true;
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public static AppConfig load(Path configPath) throws IOException {
|
|
Yaml yaml = new Yaml();
|
|
Map<String, Object> root;
|
|
try (InputStream in = Files.newInputStream(configPath)) {
|
|
root = yaml.load(in);
|
|
}
|
|
if (root == null) {
|
|
root = Map.of();
|
|
}
|
|
|
|
AppConfig config = new AppConfig();
|
|
Map<String, Object> classCheck = mapOrEmpty(root.get("class_check"));
|
|
config.enabled = boolOrDefault(classCheck.get("enabled"), true);
|
|
|
|
Map<String, Object> conversion = mapOrEmpty(classCheck.get("dto_entity_conversion"));
|
|
config.dtoEntityConversionEnabled = boolOrDefault(conversion.get("enabled"), true);
|
|
|
|
config.modelDirs = stringList(classCheck.get("model_dirs"));
|
|
Map<String, Object> endpointScan = mapOrEmpty(classCheck.get("endpoint_scan"));
|
|
config.controllerScanDirs = stringList(endpointScan.get("controllers"));
|
|
config.feignScanDirs = stringList(endpointScan.get("feign_apis"));
|
|
config.conversionScanDirs = stringList(classCheck.get("conversion_scan"));
|
|
|
|
Map<String, Object> wecom = mapOrEmpty(root.get("wecom"));
|
|
config.wecomWebhookUrl = stringOrEmpty(wecom.get("webhook_url"));
|
|
|
|
Map<String, Object> notify = mapOrEmpty(root.get("notify"));
|
|
config.onlyOnChange = boolOrDefault(notify.get("only_on_change"), true);
|
|
|
|
return config;
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
private static Map<String, Object> mapOrEmpty(Object value) {
|
|
if (value instanceof Map) {
|
|
return (Map<String, Object>) value;
|
|
}
|
|
return Map.of();
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
private static List<String> stringList(Object value) {
|
|
if (value instanceof List) {
|
|
List<?> list = (List<?>) value;
|
|
List<String> result = new ArrayList<>();
|
|
for (Object item : list) {
|
|
if (item != null) {
|
|
result.add(item.toString());
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
return new ArrayList<>();
|
|
}
|
|
|
|
private static boolean boolOrDefault(Object value, boolean defaultValue) {
|
|
if (value instanceof Boolean) {
|
|
return (Boolean) value;
|
|
}
|
|
return defaultValue;
|
|
}
|
|
|
|
private static String stringOrEmpty(Object value) {
|
|
return value == null ? "" : value.toString();
|
|
}
|
|
|
|
public boolean isEnabled() {
|
|
return enabled;
|
|
}
|
|
|
|
public boolean isDtoEntityConversionEnabled() {
|
|
return dtoEntityConversionEnabled;
|
|
}
|
|
|
|
public List<String> getModelDirs() {
|
|
return modelDirs;
|
|
}
|
|
|
|
public List<String> getControllerScanDirs() {
|
|
return controllerScanDirs;
|
|
}
|
|
|
|
public List<String> getFeignScanDirs() {
|
|
return feignScanDirs;
|
|
}
|
|
|
|
public List<String> getConversionScanDirs() {
|
|
return conversionScanDirs;
|
|
}
|
|
|
|
public String getWecomWebhookUrl() {
|
|
return wecomWebhookUrl;
|
|
}
|
|
|
|
public boolean isOnlyOnChange() {
|
|
return onlyOnChange;
|
|
}
|
|
}
|