mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
style: Reformatted code with .editorconfig
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
# configured-HOCON
|
||||
|
||||
HOCON file-based implementation, compatible with all Java environments.
|
||||
HOCON
|
||||
file-based
|
||||
implementation,
|
||||
compatible
|
||||
with
|
||||
all
|
||||
Java
|
||||
environments.
|
||||
|
||||
## Dependencies
|
||||
|
||||
|
||||
@@ -74,4 +74,4 @@
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+30
-27
@@ -9,15 +9,18 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class HOCONSource
|
||||
extends FileConfigSource<SourcedSection, Map<String, Object>, HOCONSource> {
|
||||
extends FileConfigSource<SourcedSection, Map<String, Object>, HOCONSource> {
|
||||
protected @Nullable SourcedSection rootSection;
|
||||
|
||||
protected HOCONSource(
|
||||
@NotNull ConfigurationHolder<? extends HOCONSource> holder,
|
||||
@NotNull File file, @Nullable String resourcePath
|
||||
@NotNull ConfigurationHolder<? extends HOCONSource> holder,
|
||||
@NotNull File file, @Nullable String resourcePath
|
||||
) {
|
||||
super(holder, 0, file, resourcePath);
|
||||
|
||||
@@ -54,32 +57,32 @@ public class HOCONSource
|
||||
// accumulator: 将 Section 中的信息为 typesafe config 添加并返回
|
||||
// combiner: 合并两个配置文件
|
||||
Config config = this.getValues(true).entrySet().stream().reduce(
|
||||
ConfigFactory.empty(),
|
||||
(cfg, entry) -> {
|
||||
String key = entry.getKey(); // 源数据 key
|
||||
Object value = entry.getValue(); // 源数据 value
|
||||
ConfigFactory.empty(),
|
||||
(cfg, entry) -> {
|
||||
String key = entry.getKey(); // 源数据 key
|
||||
Object value = entry.getValue(); // 源数据 value
|
||||
|
||||
ConfigValue result; // 最终转换为 typesafe 的 ConfigValue 类型
|
||||
if (value == null || value instanceof Boolean || value instanceof String || value instanceof Number) {
|
||||
result = ConfigValueFactory.fromAnyRef(value); // 原始数据类型
|
||||
} else if (value instanceof Iterator) {
|
||||
result = ConfigValueFactory.fromIterable((Iterable<?>) value);
|
||||
} else if (value instanceof Map) {
|
||||
//noinspection unchecked
|
||||
result = ConfigValueFactory.fromMap((Map<String, ?>) value);
|
||||
} else {
|
||||
result = ConfigValueFactory.fromAnyRef(String.valueOf(value));
|
||||
}
|
||||
List<String> headerComments = HOCONSource.this.getHeaderComments(key); // 获取其注释
|
||||
result = result.withOrigin(result.origin().withComments(headerComments)); // 赋予其注释
|
||||
return cfg.withValue(key, result); // 将其添加到根 config 中
|
||||
},
|
||||
Config::withFallback
|
||||
ConfigValue result; // 最终转换为 typesafe 的 ConfigValue 类型
|
||||
if (value == null || value instanceof Boolean || value instanceof String || value instanceof Number) {
|
||||
result = ConfigValueFactory.fromAnyRef(value); // 原始数据类型
|
||||
} else if (value instanceof Iterator) {
|
||||
result = ConfigValueFactory.fromIterable((Iterable<?>) value);
|
||||
} else if (value instanceof Map) {
|
||||
//noinspection unchecked
|
||||
result = ConfigValueFactory.fromMap((Map<String, ?>) value);
|
||||
} else {
|
||||
result = ConfigValueFactory.fromAnyRef(String.valueOf(value));
|
||||
}
|
||||
List<String> headerComments = HOCONSource.this.getHeaderComments(key); // 获取其注释
|
||||
result = result.withOrigin(result.origin().withComments(headerComments)); // 赋予其注释
|
||||
return cfg.withValue(key, result); // 将其添加到根 config 中
|
||||
},
|
||||
Config::withFallback
|
||||
);
|
||||
return config.root().render(
|
||||
ConfigRenderOptions.defaults()
|
||||
.setJson(false)
|
||||
.setOriginComments(false)
|
||||
ConfigRenderOptions.defaults()
|
||||
.setJson(false)
|
||||
.setOriginComments(false)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,11 +18,11 @@ public interface SampleConfig extends Configuration {
|
||||
|
||||
@HeaderComments({"[ UUID >-----------------------------------", "A lot of UUIDs"})
|
||||
ConfiguredList<UUID> UUIDS = ConfiguredList.builderOf(UUID.class).fromString()
|
||||
.parse(UUID::fromString).serialize(UUID::toString)
|
||||
.defaults(
|
||||
UUID.fromString("00000000-0000-0000-0000-000000000000"),
|
||||
UUID.fromString("00000000-0000-0000-0000-000000000001")
|
||||
).build();
|
||||
.parse(UUID::fromString).serialize(UUID::toString)
|
||||
.defaults(
|
||||
UUID.fromString("00000000-0000-0000-0000-000000000000"),
|
||||
UUID.fromString("00000000-0000-0000-0000-000000000001")
|
||||
).build();
|
||||
|
||||
@ConfigPath("info") // Custom path
|
||||
interface INFO extends Configuration {
|
||||
|
||||
@@ -4,14 +4,14 @@ import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import cc.carm.lib.configuration.source.hocon.HOCONConfigFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SampleTest {
|
||||
public class SampleTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
// 1. Make a configuration provider from a file.
|
||||
ConfigurationHolder<?> holder = HOCONConfigFactory.from("target/config.conf")
|
||||
.resourcePath("configs/sample.conf")
|
||||
.build();
|
||||
.resourcePath("configs/sample.conf")
|
||||
.build();
|
||||
|
||||
// 2. Initialize the configuration classes or instances.
|
||||
holder.initialize(SampleConfig.class);
|
||||
|
||||
Reference in New Issue
Block a user