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,8 +1,24 @@
|
||||
# configured-JSON
|
||||
|
||||
JSON file-based implementation, compatible with all Java environments.
|
||||
JSON
|
||||
file-based
|
||||
implementation,
|
||||
compatible
|
||||
with
|
||||
all
|
||||
Java
|
||||
environments.
|
||||
|
||||
**Remember that JSON does not support file comments.**
|
||||
*
|
||||
*Remember
|
||||
that
|
||||
JSON
|
||||
does
|
||||
not
|
||||
support
|
||||
file
|
||||
comments.
|
||||
**
|
||||
|
||||
## Dependencies
|
||||
|
||||
|
||||
@@ -71,4 +71,4 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -17,11 +17,11 @@ import java.util.Objects;
|
||||
public class JSONSource extends FileConfigSource<SourcedSection, Map<String, Object>, JSONSource> {
|
||||
|
||||
public static final @NotNull Gson DEFAULT_GSON = new GsonBuilder()
|
||||
.serializeNulls().disableHtmlEscaping().setPrettyPrinting()
|
||||
.registerTypeAdapter(
|
||||
SourcedSection.class,
|
||||
(JsonSerializer<SourcedSection>) (src, t, c) -> c.serialize(src.data())
|
||||
).create();
|
||||
.serializeNulls().disableHtmlEscaping().setPrettyPrinting()
|
||||
.registerTypeAdapter(
|
||||
SourcedSection.class,
|
||||
(JsonSerializer<SourcedSection>) (src, t, c) -> c.serialize(src.data())
|
||||
).create();
|
||||
|
||||
protected final @NotNull Gson gson;
|
||||
protected @Nullable SourcedSection rootSection;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -63,4 +63,4 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+48
-15
@@ -1,8 +1,16 @@
|
||||
# configured-SQL
|
||||
|
||||
SQL database implementation, support for MySQL or MariaDB.
|
||||
SQL
|
||||
database
|
||||
implementation,
|
||||
support
|
||||
for
|
||||
MySQL
|
||||
or
|
||||
MariaDB.
|
||||
|
||||
## Table schema
|
||||
|
||||
```mysql
|
||||
CREATE TABLE IF NOT EXISTS conf
|
||||
(
|
||||
@@ -16,9 +24,11 @@ CREATE TABLE IF NOT EXISTS conf
|
||||
`version` MEDIUMINT UNSIGNED NOT NULL DEFAULT 0, # 配置项的版本
|
||||
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, # 创建时间
|
||||
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`namespace`, `path`)
|
||||
) ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8mb4;
|
||||
PRIMARY KEY (`namespace`,
|
||||
`path`)
|
||||
)
|
||||
ENGINE = InnoDB
|
||||
DEFAULT CHARSET = utf8mb4;
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
@@ -32,16 +42,30 @@ CREATE TABLE IF NOT EXISTS conf
|
||||
|
||||
<repository>
|
||||
<!-- Using Maven Central Repository for secure and stable updates, though synchronization might be needed. -->
|
||||
<id>maven</id>
|
||||
<name>Maven Central</name>
|
||||
<url>https://repo1.maven.org/maven2</url>
|
||||
<id>
|
||||
maven
|
||||
</id>
|
||||
<name>
|
||||
Maven
|
||||
Central
|
||||
</name>
|
||||
<url>
|
||||
https://repo1.maven.org/maven2
|
||||
</url>
|
||||
</repository>
|
||||
|
||||
<repository>
|
||||
<!-- Using GitHub dependencies for real-time updates, configuration required (recommended). -->
|
||||
<id>configured</id>
|
||||
<name>GitHub Packages</name>
|
||||
<url>https://maven.pkg.github.com/CarmJos/configured</url>
|
||||
<id>
|
||||
configured
|
||||
</id>
|
||||
<name>
|
||||
GitHub
|
||||
Packages
|
||||
</name>
|
||||
<url>
|
||||
https://maven.pkg.github.com/CarmJos/configured
|
||||
</url>
|
||||
</repository>
|
||||
|
||||
</repositories>
|
||||
@@ -53,10 +77,19 @@ CREATE TABLE IF NOT EXISTS conf
|
||||
<project>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>configured-sql</artifactId>
|
||||
<version>[LATEST RELEASE]</version>
|
||||
<scope>compile</scope>
|
||||
<groupId>
|
||||
cc.carm.lib
|
||||
</groupId>
|
||||
<artifactId>
|
||||
configured-sql
|
||||
</artifactId>
|
||||
<version>
|
||||
[LATEST
|
||||
RELEASE]
|
||||
</version>
|
||||
<scope>
|
||||
compile
|
||||
</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -80,4 +113,4 @@ repositories {
|
||||
dependencies {
|
||||
api "cc.carm.lib:configured-sql:[LATEST RELEASE]"
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -81,4 +81,4 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+9
-9
@@ -32,7 +32,7 @@ public class SQLConfigFactory extends ConfigurationFactory<SQLSource, Configurat
|
||||
}
|
||||
|
||||
protected static final @NotNull Gson DEFAULT_GSON = new GsonBuilder()
|
||||
.serializeNulls().disableHtmlEscaping().create();
|
||||
.serializeNulls().disableHtmlEscaping().create();
|
||||
|
||||
protected static final @NotNull BiConsumer<String, TableCreateBuilder> DEFAULT_TABLE_SCHEMA = (tableName, builder) -> {
|
||||
builder.addColumn("namespace", "VARCHAR(32) NOT NULL");
|
||||
@@ -48,17 +48,17 @@ public class SQLConfigFactory extends ConfigurationFactory<SQLSource, Configurat
|
||||
builder.addColumn("version", "MEDIUMINT UNSIGNED NOT NULL DEFAULT 0");
|
||||
|
||||
builder.addColumn(
|
||||
"create_time",
|
||||
"TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"
|
||||
"create_time",
|
||||
"TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"
|
||||
);
|
||||
builder.addColumn(
|
||||
"update_time",
|
||||
"TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
|
||||
"update_time",
|
||||
"TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
|
||||
);
|
||||
|
||||
builder.setIndex(
|
||||
IndexType.PRIMARY_KEY, "pk_" + tableName.toLowerCase(),
|
||||
"namespace", "path"
|
||||
IndexType.PRIMARY_KEY, "pk_" + tableName.toLowerCase(),
|
||||
"namespace", "path"
|
||||
);
|
||||
builder.setTableSettings("ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
|
||||
};
|
||||
@@ -163,8 +163,8 @@ public class SQLConfigFactory extends ConfigurationFactory<SQLSource, Configurat
|
||||
|
||||
return new ConfigurationHolder<SQLSource>(this.adapters, this.options, this.metadata, this.initializer) {
|
||||
final SQLSource source = new SQLSource(
|
||||
this, System.currentTimeMillis(),
|
||||
gson, manager, resolvers, tableName, namespace
|
||||
this, System.currentTimeMillis(),
|
||||
gson, manager, resolvers, tableName, namespace
|
||||
);
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,6 +7,6 @@ public interface SQLOptions {
|
||||
/**
|
||||
* Whether to purge the configuration's in-database data when saving.
|
||||
*/
|
||||
ConfigurationOption<Boolean> PURGE = ConfigurationOption.of( true);
|
||||
ConfigurationOption<Boolean> PURGE = ConfigurationOption.of(true);
|
||||
|
||||
}
|
||||
|
||||
@@ -104,10 +104,10 @@ public class SQLSource extends ConfigureSource<SourcedSection, Map<String, Objec
|
||||
|
||||
int version = holder().metadata(path).get(VersionedMetaTypes.VERSION, 0);
|
||||
dataValues.add(new Object[]{
|
||||
namespace, path, time, version, typeID, data,
|
||||
Commentable.getInlineComment(holder(), path),
|
||||
gson.toJson(Commentable.getHeaderComments(holder(), path)),
|
||||
gson.toJson(Commentable.getFooterComments(holder(), path))
|
||||
namespace, path, time, version, typeID, data,
|
||||
Commentable.getInlineComment(holder(), path),
|
||||
gson.toJson(Commentable.getHeaderComments(holder(), path)),
|
||||
gson.toJson(Commentable.getFooterComments(holder(), path))
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
@@ -118,18 +118,18 @@ public class SQLSource extends ConfigureSource<SourcedSection, Map<String, Objec
|
||||
purge();
|
||||
}
|
||||
this.table.createReplaceBatch()
|
||||
.setColumnNames(
|
||||
"namespace", "path", "update_time", "version", "type", "value",
|
||||
"inline_comment", "header_comments", "footer_comments"
|
||||
).setAllParams(dataValues).execute();
|
||||
.setColumnNames(
|
||||
"namespace", "path", "update_time", "version", "type", "value",
|
||||
"inline_comment", "header_comments", "footer_comments"
|
||||
).setAllParams(dataValues).execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onReload() throws Exception {
|
||||
Map<String, Object> loaded = new LinkedHashMap<>();
|
||||
try (SQLQuery query = this.table.createQuery()
|
||||
.addCondition("namespace", namespace)
|
||||
.build().execute()) {
|
||||
.addCondition("namespace", namespace)
|
||||
.build().execute()) {
|
||||
ResultSet rs = query.getResultSet();
|
||||
while (rs.next()) {
|
||||
String path = rs.getString("path");
|
||||
@@ -161,9 +161,9 @@ public class SQLSource extends ConfigureSource<SourcedSection, Map<String, Objec
|
||||
|
||||
protected int typeIdOf(@NotNull Object value) {
|
||||
return this.resolvers.entrySet().stream()
|
||||
.filter(entry -> entry.getValue().isInstance(value))
|
||||
.findFirst().map(Map.Entry::getKey)
|
||||
.orElseThrow(() -> new IllegalStateException("No resolvers for value " + value.getClass().getName()));
|
||||
.filter(entry -> entry.getValue().isInstance(value))
|
||||
.findFirst().map(Map.Entry::getKey)
|
||||
.orElseThrow(() -> new IllegalStateException("No resolvers for value " + value.getClass().getName()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
# configured-YAML
|
||||
|
||||
YAML file-based implementation, compatible with all Java environments.
|
||||
YAML
|
||||
file-based
|
||||
implementation,
|
||||
compatible
|
||||
with
|
||||
all
|
||||
Java
|
||||
environments.
|
||||
|
||||
## Dependencies
|
||||
|
||||
@@ -61,4 +68,4 @@ repositories {
|
||||
dependencies {
|
||||
api "cc.carm.lib:configured-yaml:[LATEST RELEASE]"
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -88,4 +88,4 @@
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -26,8 +26,8 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
public class YAMLSource
|
||||
extends FileConfigSource<SourcedSection, Map<String, Object>, YAMLSource>
|
||||
implements CommentedSection {
|
||||
extends FileConfigSource<SourcedSection, Map<String, Object>, YAMLSource>
|
||||
implements CommentedSection {
|
||||
|
||||
protected final @NotNull YamlConstructor yamlConstructor;
|
||||
protected final @NotNull YamlRepresenter yamlRepresenter;
|
||||
@@ -109,9 +109,9 @@ public class YAMLSource
|
||||
@Override
|
||||
public void save() throws Exception {
|
||||
CommentedYAMLWriter writer = new CommentedYAMLWriter(
|
||||
String.valueOf(this.pathSeparator()),
|
||||
dumperOptions().getIndent(),
|
||||
holder.option(CommentableOptions.COMMENT_EMPTY_VALUE)
|
||||
String.valueOf(this.pathSeparator()),
|
||||
dumperOptions().getIndent(),
|
||||
holder.option(CommentableOptions.COMMENT_EMPTY_VALUE)
|
||||
);
|
||||
try {
|
||||
fileWriter(w -> w.write(writer.saveToString(this)));
|
||||
|
||||
@@ -23,11 +23,11 @@ public interface SampleConfig extends Configuration {
|
||||
@HeaderComments({"[ UUID >-----------------------------------", "A lot of UUIDs"})
|
||||
@FooterComments("[ UUID >-----------------------------------")
|
||||
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 {
|
||||
|
||||
@@ -10,9 +10,9 @@ public class SampleTest {
|
||||
public void test() {
|
||||
// 1. Make a configuration provider from a file.
|
||||
ConfigurationHolder<?> holder = YAMLConfigFactory.from("target/config.yml")
|
||||
.resourcePath("configs/sample.yml")
|
||||
.indent(2) // Optional: Set the indentation of the configuration file.
|
||||
.build();
|
||||
.resourcePath("configs/sample.yml")
|
||||
.indent(2) // Optional: Set the indentation of the configuration file.
|
||||
.build();
|
||||
|
||||
// 2. Initialize the configuration classes or instances.
|
||||
holder.initialize(SampleConfig.class);
|
||||
|
||||
Reference in New Issue
Block a user