mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 18:48:20 +08:00
feat(value): 支持对Enum对象的快速解析
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>3.6.0</version>
|
<version>3.7.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
+4
-1
@@ -46,7 +46,10 @@ public class ConfigValueBuilder<V> {
|
|||||||
return from(
|
return from(
|
||||||
Object.class, ConfigDataFunction.identity(),
|
Object.class, ConfigDataFunction.identity(),
|
||||||
ConfigValueParser.castObject(valueClass),
|
ConfigValueParser.castObject(valueClass),
|
||||||
ConfigDataFunction.toObject(), ConfigDataFunction.toObject()
|
s -> {
|
||||||
|
if (s instanceof Enum<?>) return ((Enum<?>) s).name();
|
||||||
|
else return s;
|
||||||
|
}, ConfigDataFunction.toObject()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+18
-17
@@ -13,34 +13,34 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
default <V> @NotNull ConfigDataFunction<T, V> andThen(@NotNull ConfigDataFunction<? super R, V> after) {
|
default <V> @NotNull ConfigDataFunction<T, V> andThen(@NotNull ConfigDataFunction<? super R, V> after) {
|
||||||
Objects.requireNonNull(after);
|
Objects.requireNonNull(after);
|
||||||
return ((data) -> after.parse(parse(data)));
|
return data -> after.parse(parse(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <T> @NotNull ConfigDataFunction<T, T> identity() {
|
static <T> @NotNull ConfigDataFunction<T, T> identity() {
|
||||||
return (input) -> input;
|
return input -> input;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <T> @NotNull ConfigDataFunction<T, T> identity(Class<T> type) {
|
static <T> @NotNull ConfigDataFunction<T, T> identity(Class<T> type) {
|
||||||
return (input) -> input;
|
return input -> input;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <T, V> @NotNull ConfigDataFunction<T, V> required() {
|
static <T, V> @NotNull ConfigDataFunction<T, V> required() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
throw new IllegalArgumentException("Please specify the value parser.");
|
throw new IllegalArgumentException("Please specify the value parser.");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <T> @NotNull ConfigDataFunction<T, Object> toObject() {
|
static <T> @NotNull ConfigDataFunction<T, Object> toObject() {
|
||||||
return (input) -> input;
|
return input -> input;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <V> @NotNull ConfigDataFunction<Object, V> castObject(Class<V> valueClass) {
|
static <V> @NotNull ConfigDataFunction<Object, V> castObject(Class<V> valueClass) {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
||||||
else throw new IllegalArgumentException("Cannot cast value to " + valueClass.getName());
|
else throw new IllegalArgumentException("Cannot cast value to " + valueClass.getName());
|
||||||
};
|
};
|
||||||
@@ -48,7 +48,7 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <V> @NotNull ConfigDataFunction<String, V> castFromString(Class<V> valueClass) {
|
static <V> @NotNull ConfigDataFunction<String, V> castFromString(Class<V> valueClass) {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
||||||
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
|
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
|
||||||
};
|
};
|
||||||
@@ -56,15 +56,16 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <T> @NotNull ConfigDataFunction<T, String> castToString() {
|
static <T> @NotNull ConfigDataFunction<T, String> castToString() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (input instanceof String) return (String) input;
|
if (input instanceof String) return (String) input;
|
||||||
|
else if (input instanceof Enum<?>) return ((Enum<?>) input).name();
|
||||||
else return input.toString();
|
else return input.toString();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static <V> @NotNull ConfigDataFunction<String, V> parseString(Class<V> valueClass) {
|
static <V> @NotNull ConfigDataFunction<String, V> parseString(Class<V> valueClass) {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
||||||
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
|
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
|
||||||
};
|
};
|
||||||
@@ -72,7 +73,7 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static @NotNull ConfigDataFunction<Object, Integer> intValue() {
|
static @NotNull ConfigDataFunction<Object, Integer> intValue() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (input instanceof Integer) {
|
if (input instanceof Integer) {
|
||||||
return (Integer) input;
|
return (Integer) input;
|
||||||
} else if (input instanceof Number) {
|
} else if (input instanceof Number) {
|
||||||
@@ -83,7 +84,7 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static @NotNull ConfigDataFunction<Object, Short> shortValue() {
|
static @NotNull ConfigDataFunction<Object, Short> shortValue() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (input instanceof Short) {
|
if (input instanceof Short) {
|
||||||
return (Short) input;
|
return (Short) input;
|
||||||
} else if (input instanceof Number) {
|
} else if (input instanceof Number) {
|
||||||
@@ -94,7 +95,7 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static @NotNull ConfigDataFunction<Object, Double> doubleValue() {
|
static @NotNull ConfigDataFunction<Object, Double> doubleValue() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (input instanceof Double) {
|
if (input instanceof Double) {
|
||||||
return (Double) input;
|
return (Double) input;
|
||||||
} else if (input instanceof Number) {
|
} else if (input instanceof Number) {
|
||||||
@@ -105,7 +106,7 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static @NotNull ConfigDataFunction<Object, Byte> byteValue() {
|
static @NotNull ConfigDataFunction<Object, Byte> byteValue() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (input instanceof Byte) {
|
if (input instanceof Byte) {
|
||||||
return (Byte) input;
|
return (Byte) input;
|
||||||
} else if (input instanceof Number) {
|
} else if (input instanceof Number) {
|
||||||
@@ -116,7 +117,7 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static @NotNull ConfigDataFunction<Object, Float> floatValue() {
|
static @NotNull ConfigDataFunction<Object, Float> floatValue() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (input instanceof Float) {
|
if (input instanceof Float) {
|
||||||
return (Float) input;
|
return (Float) input;
|
||||||
} else if (input instanceof Number) {
|
} else if (input instanceof Number) {
|
||||||
@@ -127,7 +128,7 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static @NotNull ConfigDataFunction<Object, Long> longValue() {
|
static @NotNull ConfigDataFunction<Object, Long> longValue() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (input instanceof Long) {
|
if (input instanceof Long) {
|
||||||
return (Long) input;
|
return (Long) input;
|
||||||
} else if (input instanceof Number) {
|
} else if (input instanceof Number) {
|
||||||
@@ -138,14 +139,14 @@ public interface ConfigDataFunction<T, R> {
|
|||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
static @NotNull ConfigDataFunction<Object, Boolean> booleanValue() {
|
static @NotNull ConfigDataFunction<Object, Boolean> booleanValue() {
|
||||||
return (input) -> {
|
return input -> {
|
||||||
if (input instanceof Boolean) {
|
if (input instanceof Boolean) {
|
||||||
return (Boolean) input;
|
return (Boolean) input;
|
||||||
} else if (input instanceof String) {
|
} else if (input instanceof String) {
|
||||||
String s = (String) input;
|
String s = (String) input;
|
||||||
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
|
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
|
||||||
} else if (input instanceof Integer) {
|
} else if (input instanceof Integer) {
|
||||||
return ((Integer) input) == 1;
|
return (Integer) input == 1;
|
||||||
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
|
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,9 @@ public interface ConfigValueParser<T, R> {
|
|||||||
}
|
}
|
||||||
} else if (Boolean.class.isAssignableFrom(valueClass)) {
|
} else if (Boolean.class.isAssignableFrom(valueClass)) {
|
||||||
input = booleanValue().parse(input, (Boolean) defaultValue);
|
input = booleanValue().parse(input, (Boolean) defaultValue);
|
||||||
|
} else if (Enum.class.isAssignableFrom(valueClass) && input instanceof String) {
|
||||||
|
String enumName = (String) input;
|
||||||
|
input = valueClass.getDeclaredMethod("valueOf", String.class).invoke(null, enumName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
if (valueClass.isInstance(input)) return valueClass.cast(input);
|
||||||
@@ -125,6 +128,21 @@ public interface ConfigValueParser<T, R> {
|
|||||||
return (input, defaultValue) -> ConfigDataFunction.booleanValue().parse(input);
|
return (input, defaultValue) -> ConfigDataFunction.booleanValue().parse(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Contract(pure = true)
|
||||||
|
static @NotNull <E extends Enum<E>> ConfigValueParser<Object, E> enumValue(Class<E> enumClass) {
|
||||||
|
return (input, defaultValue) -> {
|
||||||
|
if (input instanceof Enum) {
|
||||||
|
return enumClass.cast(input);
|
||||||
|
} else if (input instanceof String) {
|
||||||
|
return Enum.valueOf(enumClass, (String) input);
|
||||||
|
} else if (input instanceof Number) {
|
||||||
|
return enumClass.getEnumConstants()[((Number) input).intValue()];
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Cannot cast value to " + enumClass.getName());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>3.6.0</version>
|
<version>3.7.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import cc.carm.lib.configuration.core.value.type.ConfiguredSection;
|
|||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
|
||||||
import cc.carm.lib.configuration.demo.tests.model.TestModel;
|
import cc.carm.lib.configuration.demo.tests.model.TestModel;
|
||||||
|
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -25,6 +26,8 @@ public class DemoConfiguration extends ConfigurationRoot {
|
|||||||
@ConfigPath(root = true)
|
@ConfigPath(root = true)
|
||||||
public static final ConfigValue<Long> TEST_NUMBER = ConfiguredValue.of(Long.class, 1000000L);
|
public static final ConfigValue<Long> TEST_NUMBER = ConfiguredValue.of(Long.class, 1000000L);
|
||||||
|
|
||||||
|
public static final ConfigValue<ChronoUnit> TEST_ENUM = ConfiguredValue.of(ChronoUnit.class, ChronoUnit.DAYS);
|
||||||
|
|
||||||
// 支持通过 Class<?> 变量标注子配置,一并注册。
|
// 支持通过 Class<?> 变量标注子配置,一并注册。
|
||||||
// 注意: 若对应类也有注解,则优先使用类的注解。
|
// 注意: 若对应类也有注解,则优先使用类的注解。
|
||||||
@ConfigPath("other-class-config") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。
|
@ConfigPath("other-class-config") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。
|
||||||
|
|||||||
+26
-26
@@ -6,40 +6,17 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<version>3.6.0</version>
|
<version>3.7.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>easyconfiguration-hocon</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
<artifactId>easyconfiguration-hocon</artifactId>
|
||||||
<build>
|
<packaging>jar</packaging>
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -63,4 +40,27 @@
|
|||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -6,6 +6,10 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class EasyConfiguration {
|
public class EasyConfiguration {
|
||||||
|
|
||||||
|
private EasyConfiguration() {
|
||||||
|
}
|
||||||
|
|
||||||
public static HOCONFileConfigProvider from(File file, String source) {
|
public static HOCONFileConfigProvider from(File file, String source) {
|
||||||
HOCONFileConfigProvider provider = new HOCONFileConfigProvider(file);
|
HOCONFileConfigProvider provider = new HOCONFileConfigProvider(file);
|
||||||
try {
|
try {
|
||||||
@@ -28,4 +32,5 @@ public class EasyConfiguration {
|
|||||||
public static HOCONFileConfigProvider from(String fileName, String source) {
|
public static HOCONFileConfigProvider from(String fileName, String source) {
|
||||||
return from(new File(fileName), source);
|
return from(new File(fileName), source);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.hocon;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public interface CommentedHOCON {
|
|
||||||
default @NotNull Set<String> getKeys() {
|
|
||||||
return getKeys(null, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
String serializeValue(@NotNull String key, @NotNull Object value);
|
|
||||||
|
|
||||||
@Contract("null,_ -> !null;!null,_ -> _")
|
|
||||||
@Nullable Set<String> getKeys(@Nullable String sectionKey, boolean deep);
|
|
||||||
|
|
||||||
@Nullable Object getValue(@NotNull String key);
|
|
||||||
|
|
||||||
@Nullable String getInlineComment(@NotNull String key);
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
List<String> getHeaderComments(@Nullable String key);
|
|
||||||
}
|
|
||||||
+10
-7
@@ -6,6 +6,7 @@ import cc.carm.lib.configuration.core.source.impl.FileConfigProvider;
|
|||||||
import cc.carm.lib.configuration.hocon.exception.HOCONGetValueException;
|
import cc.carm.lib.configuration.hocon.exception.HOCONGetValueException;
|
||||||
import cc.carm.lib.configuration.hocon.util.HOCONUtils;
|
import cc.carm.lib.configuration.hocon.util.HOCONUtils;
|
||||||
import com.typesafe.config.*;
|
import com.typesafe.config.*;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ import java.nio.file.Files;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class HOCONFileConfigProvider extends FileConfigProvider<HOCONConfigWrapper> implements CommentedHOCON {
|
public class HOCONFileConfigProvider extends FileConfigProvider<HOCONConfigWrapper> {
|
||||||
protected final @NotNull ConfigurationComments comments = new ConfigurationComments();
|
protected final @NotNull ConfigurationComments comments = new ConfigurationComments();
|
||||||
protected HOCONConfigWrapper configuration;
|
protected HOCONConfigWrapper configuration;
|
||||||
protected ConfigInitializer<HOCONFileConfigProvider> initializer;
|
protected ConfigInitializer<HOCONFileConfigProvider> initializer;
|
||||||
@@ -48,9 +49,10 @@ public class HOCONFileConfigProvider extends FileConfigProvider<HOCONConfigWrapp
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onReload() throws ConfigException {
|
protected void onReload() throws ConfigException {
|
||||||
this.configuration = new HOCONConfigWrapper(ConfigFactory.parseFile(this.file, ConfigParseOptions.defaults()
|
ConfigObject conf = ConfigFactory.parseFile(this.file, ConfigParseOptions.defaults()
|
||||||
.setSyntax(ConfigSyntax.CONF)
|
.setSyntax(ConfigSyntax.CONF)
|
||||||
.setAllowMissing(false)).root());
|
.setAllowMissing(false)).root();
|
||||||
|
this.configuration = new HOCONConfigWrapper(conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -63,7 +65,6 @@ public class HOCONFileConfigProvider extends FileConfigProvider<HOCONConfigWrapp
|
|||||||
return this.initializer;
|
return this.initializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String serializeValue(@NotNull String key, @NotNull Object value) {
|
public String serializeValue(@NotNull String key, @NotNull Object value) {
|
||||||
// 带有 key=value 的新空对象
|
// 带有 key=value 的新空对象
|
||||||
return ConfigFactory.empty()
|
return ConfigFactory.empty()
|
||||||
@@ -71,7 +72,11 @@ public class HOCONFileConfigProvider extends FileConfigProvider<HOCONConfigWrapp
|
|||||||
.root().render();
|
.root().render();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public @NotNull Set<String> getKeys() {
|
||||||
|
return getKeys(null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Contract("null,_->!null")
|
||||||
public @Nullable Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
|
public @Nullable Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
|
||||||
if (sectionKey == null) { // 当前路径
|
if (sectionKey == null) { // 当前路径
|
||||||
return HOCONUtils.getKeysFromObject(this.configuration, deep, "");
|
return HOCONUtils.getKeysFromObject(this.configuration, deep, "");
|
||||||
@@ -91,12 +96,10 @@ public class HOCONFileConfigProvider extends FileConfigProvider<HOCONConfigWrapp
|
|||||||
return HOCONUtils.getKeysFromObject(section, deep, "");
|
return HOCONUtils.getKeysFromObject(section, deep, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable Object getValue(@NotNull String key) {
|
public @Nullable Object getValue(@NotNull String key) {
|
||||||
return this.configuration.get(key);
|
return this.configuration.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable List<String> getHeaderComments(@Nullable String key) {
|
public @Nullable List<String> getHeaderComments(@Nullable String key) {
|
||||||
return this.comments.getHeaderComment(key);
|
return this.comments.getHeaderComment(key);
|
||||||
}
|
}
|
||||||
|
|||||||
-23
@@ -1,23 +0,0 @@
|
|||||||
package cc.carm.lib.configuration.hocon.source;
|
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
|
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 暂时未实现,原因是如果要实现,就需要修改部分代码
|
|
||||||
* @see ConfigurationProvider#save()
|
|
||||||
* @see ConfigurationProvider#reload()
|
|
||||||
* 等一系列代码
|
|
||||||
*/
|
|
||||||
public abstract class StringConfigProvider<W extends ConfigurationWrapper<?>> extends ConfigurationProvider<W> {
|
|
||||||
protected final @NotNull String source;
|
|
||||||
|
|
||||||
protected StringConfigProvider(@NotNull String source) {
|
|
||||||
this.source = source;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull String getSource() {
|
|
||||||
return this.source;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,9 +8,14 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public abstract class HOCONUtils {
|
public class HOCONUtils {
|
||||||
|
|
||||||
|
private HOCONUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
public static String getSimplePath(String path, char separator) {
|
public static String getSimplePath(String path, char separator) {
|
||||||
int index = path.lastIndexOf(separator);
|
int index = path.lastIndexOf(separator);
|
||||||
return (index == -1) ? path : path.substring(index + 1);
|
return (index == -1) ? path : path.substring(index + 1);
|
||||||
@@ -49,7 +54,7 @@ public abstract class HOCONUtils {
|
|||||||
* @param prefix 当前 Object 键名前缀
|
* @param prefix 当前 Object 键名前缀
|
||||||
* @return Object 中的所有键
|
* @return Object 中的所有键
|
||||||
*/
|
*/
|
||||||
public static LinkedHashSet<String> getKeysFromObject(HOCONConfigWrapper parent, boolean deep, String prefix) {
|
public static Set<String> getKeysFromObject(HOCONConfigWrapper parent, boolean deep, String prefix) {
|
||||||
return parent.getSource().entrySet().stream().collect(
|
return parent.getSource().entrySet().stream().collect(
|
||||||
LinkedHashSet::new,
|
LinkedHashSet::new,
|
||||||
(set, entry) -> {
|
(set, entry) -> {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package online.flowerinsnow.test.easyconfiguration;
|
package online.flowerinsnow.test.easyconfiguration;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.EasyConfiguration;
|
import cc.carm.lib.configuration.EasyConfiguration;
|
||||||
|
import cc.carm.lib.configuration.demo.DatabaseConfiguration;
|
||||||
|
import cc.carm.lib.configuration.demo.tests.conf.DemoConfiguration;
|
||||||
import cc.carm.lib.configuration.hocon.HOCONFileConfigProvider;
|
import cc.carm.lib.configuration.hocon.HOCONFileConfigProvider;
|
||||||
import online.flowerinsnow.test.easyconfiguration.config.Config;
|
import online.flowerinsnow.test.easyconfiguration.config.Config;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -12,6 +14,7 @@ public class HOCONTest {
|
|||||||
public void onTest() {
|
public void onTest() {
|
||||||
HOCONFileConfigProvider provider = EasyConfiguration.from(new File("target/hocon.conf"));
|
HOCONFileConfigProvider provider = EasyConfiguration.from(new File("target/hocon.conf"));
|
||||||
provider.initialize(Config.class);
|
provider.initialize(Config.class);
|
||||||
|
provider.initialize(DatabaseConfiguration.class);
|
||||||
try {
|
try {
|
||||||
provider.reload();
|
provider.reload();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>3.6.0</version>
|
<version>3.7.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public class EasyConfiguration {
|
public class EasyConfiguration {
|
||||||
|
|
||||||
|
private EasyConfiguration() {
|
||||||
|
}
|
||||||
|
|
||||||
public static JSONConfigProvider from(File file, String source) {
|
public static JSONConfigProvider from(File file, String source) {
|
||||||
JSONConfigProvider provider = new JSONConfigProvider(file);
|
JSONConfigProvider provider = new JSONConfigProvider(file);
|
||||||
try {
|
try {
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>3.6.0</version>
|
<version>3.7.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -2,5 +2,7 @@ package cc.carm.lib.configuration;
|
|||||||
|
|
||||||
public class EasyConfiguration {
|
public class EasyConfiguration {
|
||||||
|
|
||||||
|
private EasyConfiguration() {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>3.6.0</version>
|
<version>3.7.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public class EasyConfiguration {
|
public class EasyConfiguration {
|
||||||
|
|
||||||
|
private EasyConfiguration() {
|
||||||
|
}
|
||||||
|
|
||||||
public static YAMLConfigProvider from(File file, String source) {
|
public static YAMLConfigProvider from(File file, String source) {
|
||||||
YAMLConfigProvider provider = new YAMLConfigProvider(file);
|
YAMLConfigProvider provider = new YAMLConfigProvider(file);
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>3.6.0</version>
|
<version>3.7.0</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>core</module>
|
<module>core</module>
|
||||||
<module>demo</module>
|
<module>demo</module>
|
||||||
|
|||||||
Reference in New Issue
Block a user