mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-05 02:58:20 +08:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8ee074474c | |||
| 27a68ead7c | |||
| c6cce5208f | |||
| bc67de06f6 | |||
| b668794f5d | |||
| 07424284b7 | |||
| 81e024e309 | |||
| 763fc7c758 | |||
| 56557221a4 |
+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.7.0</version>
|
<version>3.8.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
+12
-2
@@ -1,9 +1,10 @@
|
|||||||
package cc.carm.lib.configuration.core.builder.list;
|
package cc.carm.lib.configuration.core.builder.list;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.annotation.InlineComment;
|
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
public class ConfigListBuilder<V> {
|
public class ConfigListBuilder<V> {
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ public class ConfigListBuilder<V> {
|
|||||||
this.valueClass = valueClass;
|
this.valueClass = valueClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull <S> SourceListBuilder<S, V> from(@NotNull Class<S> sourceClass,
|
public @NotNull <S> SourceListBuilder<S, V> from(@NotNull Class<? super S> sourceClass,
|
||||||
@NotNull ConfigDataFunction<Object, S> sourceParser,
|
@NotNull ConfigDataFunction<Object, S> sourceParser,
|
||||||
@NotNull ConfigDataFunction<S, V> valueParser,
|
@NotNull ConfigDataFunction<S, V> valueParser,
|
||||||
@NotNull ConfigDataFunction<V, S> valueSerializer,
|
@NotNull ConfigDataFunction<V, S> valueSerializer,
|
||||||
@@ -45,4 +46,13 @@ public class ConfigListBuilder<V> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @NotNull SourceListBuilder<Map<String, Object>, V> fromMap() {
|
||||||
|
return from(
|
||||||
|
Map.class, obj -> (Map<String, Object>) obj,
|
||||||
|
ConfigDataFunction.required(),
|
||||||
|
ConfigDataFunction.required(),
|
||||||
|
ConfigDataFunction.toObject()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -12,7 +12,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, SourceListBuilder<S, V>> {
|
public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, SourceListBuilder<S, V>> {
|
||||||
|
|
||||||
protected final @NotNull Class<S> sourceClass;
|
protected final @NotNull Class<? super S> sourceClass;
|
||||||
protected @NotNull ConfigDataFunction<Object, S> sourceParser;
|
protected @NotNull ConfigDataFunction<Object, S> sourceParser;
|
||||||
|
|
||||||
protected final @NotNull Class<V> valueClass;
|
protected final @NotNull Class<V> valueClass;
|
||||||
@@ -21,7 +21,7 @@ public class SourceListBuilder<S, V> extends CommonConfigBuilder<List<V>, Source
|
|||||||
protected @NotNull ConfigDataFunction<V, S> valueSerializer;
|
protected @NotNull ConfigDataFunction<V, S> valueSerializer;
|
||||||
protected @NotNull ConfigDataFunction<S, Object> sourceSerializer;
|
protected @NotNull ConfigDataFunction<S, Object> sourceSerializer;
|
||||||
|
|
||||||
public SourceListBuilder(@NotNull Class<S> sourceClass, @NotNull ConfigDataFunction<Object, S> sourceParser,
|
public SourceListBuilder(@NotNull Class<? super S> sourceClass, @NotNull ConfigDataFunction<Object, S> sourceParser,
|
||||||
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<S, V> valueParser,
|
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<S, V> valueParser,
|
||||||
@NotNull ConfigDataFunction<V, S> valueSerializer,
|
@NotNull ConfigDataFunction<V, S> valueSerializer,
|
||||||
@NotNull ConfigDataFunction<S, Object> sourceSerializer) {
|
@NotNull ConfigDataFunction<S, Object> sourceSerializer) {
|
||||||
|
|||||||
@@ -48,6 +48,14 @@ public class ConfigMapBuilder<M extends Map<K, V>, K, V> {
|
|||||||
return fromString(ConfigDataFunction.castFromString(this.valueClass));
|
return fromString(ConfigDataFunction.castFromString(this.valueClass));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SectionMapBuilder<M, K, V> fromSection() {
|
||||||
|
return new SectionMapBuilder<>(
|
||||||
|
supplier,
|
||||||
|
keyClass, ConfigDataFunction.castFromString(keyClass),
|
||||||
|
valueClass, ConfigDataFunction.required(),
|
||||||
|
ConfigDataFunction.castToString(), ConfigDataFunction.required());
|
||||||
|
}
|
||||||
|
|
||||||
public SourceMapBuilder<M, Object, K, V> fromObject(@NotNull ConfigDataFunction<Object, V> valueParser) {
|
public SourceMapBuilder<M, Object, K, V> fromObject(@NotNull ConfigDataFunction<Object, V> valueParser) {
|
||||||
return from(Object.class, valueParser, ConfigDataFunction.toObject());
|
return from(Object.class, valueParser, ConfigDataFunction.toObject());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
package cc.carm.lib.configuration.core.builder.map;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.builder.CommonConfigBuilder;
|
||||||
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
|
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredSectionMap;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class SectionMapBuilder<M extends Map<K, V>, K, V> extends CommonConfigBuilder<M, SectionMapBuilder<M, K, V>> {
|
||||||
|
|
||||||
|
protected final @NotNull Supplier<@NotNull M> supplier;
|
||||||
|
|
||||||
|
protected final @NotNull Class<K> keyClass;
|
||||||
|
protected @NotNull ConfigDataFunction<String, K> keyParser;
|
||||||
|
|
||||||
|
protected final @NotNull Class<V> valueClass;
|
||||||
|
protected @NotNull ConfigDataFunction<ConfigurationWrapper<?>, V> valueParser;
|
||||||
|
|
||||||
|
protected @NotNull ConfigDataFunction<K, String> keySerializer;
|
||||||
|
protected @NotNull ConfigDataFunction<V, ? extends Map<String, Object>> valueSerializer;
|
||||||
|
|
||||||
|
public SectionMapBuilder(@NotNull Supplier<@NotNull M> supplier,
|
||||||
|
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
|
||||||
|
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<ConfigurationWrapper<?>, V> valueParser,
|
||||||
|
@NotNull ConfigDataFunction<K, String> keySerializer,
|
||||||
|
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> valueSerializer) {
|
||||||
|
this.supplier = supplier;
|
||||||
|
this.keyClass = keyClass;
|
||||||
|
this.keyParser = keyParser;
|
||||||
|
this.valueClass = valueClass;
|
||||||
|
this.valueParser = valueParser;
|
||||||
|
this.keySerializer = keySerializer;
|
||||||
|
this.valueSerializer = valueSerializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <MAP extends Map<K, V>> SectionMapBuilder<MAP, K, V> supplier(@NotNull Supplier<MAP> supplier) {
|
||||||
|
return new SectionMapBuilder<>(supplier,
|
||||||
|
keyClass, keyParser, valueClass, valueParser, keySerializer, valueSerializer
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull SectionMapBuilder<M, K, V> defaults(@NotNull Consumer<M> factory) {
|
||||||
|
M map = supplier.get();
|
||||||
|
factory.accept(map);
|
||||||
|
return defaults(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull SectionMapBuilder<M, K, V> parseKey(@NotNull ConfigDataFunction<String, K> parser) {
|
||||||
|
this.keyParser = parser;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull SectionMapBuilder<M, K, V> parseValue(@NotNull ConfigDataFunction<ConfigurationWrapper<?>, V> parser) {
|
||||||
|
this.valueParser = parser;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull SectionMapBuilder<M, K, V> serializeKey(@NotNull ConfigDataFunction<K, String> serializer) {
|
||||||
|
this.keySerializer = serializer;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull SectionMapBuilder<M, K, V> serializeValue(@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> serializer) {
|
||||||
|
this.valueSerializer = serializer;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull SectionMapBuilder<M, K, V> serializeValue(@NotNull BiConsumer<V, Map<String, Object>> serializer) {
|
||||||
|
return serializeValue(v -> {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
serializer.accept(v, map);
|
||||||
|
return map;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull SectionMapBuilder<M, K, V> getThis() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull ConfiguredSectionMap<K, V> build() {
|
||||||
|
return new ConfiguredSectionMap<>(
|
||||||
|
new ValueManifest<>(provider, path, headerComments, inlineComment, defaultValue),
|
||||||
|
this.supplier, this.keyClass, this.keyParser,
|
||||||
|
this.valueClass, this.valueParser,
|
||||||
|
this.keySerializer, this.valueSerializer
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+4
@@ -32,6 +32,10 @@ public class SectionValueBuilder<V>
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @NotNull SectionValueBuilder<V> parseValue(ConfigDataFunction<ConfigurationWrapper<?>, V> valueParser) {
|
||||||
|
return parseValue((section, path) -> valueParser.parse(section));
|
||||||
|
}
|
||||||
|
|
||||||
public @NotNull SectionValueBuilder<V> parseValue(ConfigValueParser<ConfigurationWrapper<?>, V> valueParser) {
|
public @NotNull SectionValueBuilder<V> parseValue(ConfigValueParser<ConfigurationWrapper<?>, V> valueParser) {
|
||||||
this.parser = valueParser;
|
this.parser = valueParser;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
+4
@@ -39,6 +39,10 @@ public class SourceValueBuilder<S, V> extends CommonConfigBuilder<V, SourceValue
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @NotNull SourceValueBuilder<S, V> parseValue(ConfigDataFunction<S, V> valueParser) {
|
||||||
|
return parseValue((section, path) -> valueParser.parse(section));
|
||||||
|
}
|
||||||
|
|
||||||
public @NotNull SourceValueBuilder<S, V> parseValue(@NotNull ConfigValueParser<S, V> valueParser) {
|
public @NotNull SourceValueBuilder<S, V> parseValue(@NotNull ConfigValueParser<S, V> valueParser) {
|
||||||
this.valueParser = valueParser;
|
this.valueParser = valueParser;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
+16
-16
@@ -5,9 +5,13 @@ import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public abstract class FileConfigProvider<W extends ConfigurationWrapper<?>> extends ConfigurationProvider<W> {
|
public abstract class FileConfigProvider<W extends ConfigurationWrapper<?>> extends ConfigurationProvider<W> {
|
||||||
@@ -37,39 +41,35 @@ public abstract class FileConfigProvider<W extends ConfigurationWrapper<?>> exte
|
|||||||
if (sourcePath != null) {
|
if (sourcePath != null) {
|
||||||
try {
|
try {
|
||||||
saveResource(sourcePath, true);
|
saveResource(sourcePath, true);
|
||||||
} catch (Exception ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveResource(@NotNull String resourcePath, boolean replace)
|
public void saveResource(@NotNull String resourcePath, boolean replace)
|
||||||
throws NullPointerException, IOException, IllegalArgumentException {
|
throws IOException, IllegalArgumentException {
|
||||||
Objects.requireNonNull(resourcePath, "ResourcePath cannot be null");
|
Objects.requireNonNull(resourcePath, "ResourcePath cannot be null");
|
||||||
if (resourcePath.equals("")) throw new IllegalArgumentException("ResourcePath cannot be empty");
|
if (resourcePath.isEmpty()) throw new IllegalArgumentException("ResourcePath cannot be empty");
|
||||||
|
|
||||||
resourcePath = resourcePath.replace('\\', '/');
|
resourcePath = resourcePath.replace('\\', '/');
|
||||||
InputStream in = getResource(resourcePath);
|
|
||||||
if (in == null) throw new IllegalArgumentException("The resource '" + resourcePath + "' not exists");
|
|
||||||
|
|
||||||
|
URL url = this.getClass().getClassLoader().getResource(resourcePath);
|
||||||
|
if (url == null) throw new IllegalArgumentException("The resource '" + resourcePath + "' not exists");
|
||||||
|
|
||||||
int lastIndex = resourcePath.lastIndexOf('/');
|
File outDir = file.getParentFile();
|
||||||
File outDir = new File(file, resourcePath.substring(0, Math.max(lastIndex, 0)));
|
|
||||||
|
|
||||||
if (!outDir.exists() && !outDir.mkdirs()) throw new IOException("Failed to create directory " + outDir);
|
if (!outDir.exists() && !outDir.mkdirs()) throw new IOException("Failed to create directory " + outDir);
|
||||||
if (!file.exists() || replace) {
|
if (!file.exists() || replace) {
|
||||||
try {
|
try (OutputStream out = Files.newOutputStream(file.toPath())) {
|
||||||
|
URLConnection connection = url.openConnection();
|
||||||
OutputStream out = new FileOutputStream(file);
|
connection.setUseCaches(false);
|
||||||
|
try (InputStream in = connection.getInputStream()) {
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
int len;
|
int len;
|
||||||
while ((len = in.read(buf)) > 0) {
|
while ((len = in.read(buf)) > 0) {
|
||||||
out.write(buf, 0, len);
|
out.write(buf, 0, len);
|
||||||
}
|
}
|
||||||
out.close();
|
}
|
||||||
in.close();
|
|
||||||
|
|
||||||
} catch (IOException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,214 @@
|
|||||||
|
package cc.carm.lib.configuration.core.value.impl;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.builder.map.ConfigMapCreator;
|
||||||
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
|
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.annotations.Unmodifiable;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public abstract class ConfigValueMap<K, V, S> extends CachedConfigValue<Map<K, V>> implements Map<K, V> {
|
||||||
|
|
||||||
|
public static <K, V> @NotNull ConfigMapCreator<K, V> builderOf(@NotNull Class<K> keyClass,
|
||||||
|
@NotNull Class<V> valueClass) {
|
||||||
|
return builder().asMap(keyClass, valueClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final @NotNull Supplier<? extends Map<K, V>> supplier;
|
||||||
|
|
||||||
|
protected final @NotNull Class<? super S> sourceClass;
|
||||||
|
protected final @NotNull Class<K> keyClass;
|
||||||
|
protected final @NotNull Class<V> valueClass;
|
||||||
|
|
||||||
|
protected final @NotNull ConfigDataFunction<String, K> keyParser;
|
||||||
|
protected final @NotNull ConfigDataFunction<S, V> valueParser;
|
||||||
|
|
||||||
|
protected final @NotNull ConfigDataFunction<K, String> keySerializer;
|
||||||
|
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
|
||||||
|
|
||||||
|
|
||||||
|
protected ConfigValueMap(@NotNull ValueManifest<Map<K, V>> manifest, @NotNull Class<? super S> sourceClass,
|
||||||
|
@NotNull Supplier<? extends Map<K, V>> mapObjSupplier,
|
||||||
|
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
|
||||||
|
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<S, V> valueParser,
|
||||||
|
@NotNull ConfigDataFunction<K, String> keySerializer,
|
||||||
|
@NotNull ConfigDataFunction<V, Object> valueSerializer) {
|
||||||
|
super(manifest);
|
||||||
|
this.supplier = mapObjSupplier;
|
||||||
|
this.sourceClass = sourceClass;
|
||||||
|
this.keyClass = keyClass;
|
||||||
|
this.valueClass = valueClass;
|
||||||
|
this.keyParser = keyParser;
|
||||||
|
this.valueParser = valueParser;
|
||||||
|
this.keySerializer = keySerializer;
|
||||||
|
this.valueSerializer = valueSerializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Class<? super S> getSourceClass() {
|
||||||
|
return sourceClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Class<K> getKeyClass() {
|
||||||
|
return keyClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Class<V> getValueClass() {
|
||||||
|
return valueClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ConfigDataFunction<String, K> getKeyParser() {
|
||||||
|
return keyParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ConfigDataFunction<S, V> getValueParser() {
|
||||||
|
return valueParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ConfigDataFunction<K, String> getKeySerializer() {
|
||||||
|
return keySerializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull ConfigDataFunction<V, Object> getValueSerializer() {
|
||||||
|
return valueSerializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract S getSource(ConfigurationWrapper<?> section, String dataKey);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Map<K, V> get() {
|
||||||
|
if (!isExpired()) return getCachedOrDefault(supplier.get());
|
||||||
|
|
||||||
|
// 已过时的数据,需要重新解析一次。
|
||||||
|
Map<K, V> map = supplier.get();
|
||||||
|
|
||||||
|
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
|
||||||
|
if (section == null) return getDefaultFirst(map);
|
||||||
|
|
||||||
|
Set<String> keys = section.getKeys(false);
|
||||||
|
if (keys.isEmpty()) return getDefaultFirst(map);
|
||||||
|
|
||||||
|
for (String dataKey : keys) {
|
||||||
|
S dataVal = getSource(section, dataKey);
|
||||||
|
if (dataVal == null) continue;
|
||||||
|
try {
|
||||||
|
K key = keyParser.parse(dataKey);
|
||||||
|
V value = valueParser.parse(dataVal);
|
||||||
|
map.put(key, value);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return updateCache(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V get(Object key) {
|
||||||
|
return get().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public V getNotNull(Object key) {
|
||||||
|
return Objects.requireNonNull(get(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(@Nullable Map<K, V> value) {
|
||||||
|
updateCache(value);
|
||||||
|
if (value == null) setValue(null);
|
||||||
|
else {
|
||||||
|
Map<String, Object> data = new LinkedHashMap<>();
|
||||||
|
for (Map.Entry<K, V> entry : value.entrySet()) {
|
||||||
|
try {
|
||||||
|
data.put(
|
||||||
|
keySerializer.parse(entry.getKey()),
|
||||||
|
valueSerializer.parse(entry.getValue())
|
||||||
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setValue(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> @NotNull T modifyValue(Function<Map<K, V>, T> function) {
|
||||||
|
Map<K, V> m = get();
|
||||||
|
T result = function.apply(m);
|
||||||
|
set(m);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull Map<K, V> modifyMap(Consumer<Map<K, V>> consumer) {
|
||||||
|
Map<K, V> m = get();
|
||||||
|
consumer.accept(m);
|
||||||
|
set(m);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return get().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return get().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsKey(Object key) {
|
||||||
|
return get().containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsValue(Object value) {
|
||||||
|
return get().containsValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public V put(K key, V value) {
|
||||||
|
return modifyValue(m -> m.put(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V remove(Object key) {
|
||||||
|
return modifyValue(m -> m.remove(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
|
||||||
|
modifyMap(map -> map.putAll(m));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
modifyMap(Map::clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Set<K> keySet() {
|
||||||
|
return get().keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Collection<V> values() {
|
||||||
|
return get().values();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
@Unmodifiable
|
||||||
|
public Set<Entry<K, V>> entrySet() {
|
||||||
|
return get().entrySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -65,6 +65,10 @@ public class ConfiguredList<V> extends CachedConfigValue<List<V>> implements Lis
|
|||||||
return get().get(index);
|
return get().get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @NotNull List<V> copy() {
|
||||||
|
return new ArrayList<>(get());
|
||||||
|
}
|
||||||
|
|
||||||
public <T> @NotNull T handle(Function<List<V>, T> function) {
|
public <T> @NotNull T handle(Function<List<V>, T> function) {
|
||||||
List<V> list = get();
|
List<V> list = get();
|
||||||
T result = function.apply(list);
|
T result = function.apply(list);
|
||||||
|
|||||||
@@ -1,40 +1,15 @@
|
|||||||
package cc.carm.lib.configuration.core.value.type;
|
package cc.carm.lib.configuration.core.value.type;
|
||||||
|
|
||||||
import cc.carm.lib.configuration.core.builder.map.ConfigMapCreator;
|
|
||||||
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
import cc.carm.lib.configuration.core.value.ValueManifest;
|
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||||
import cc.carm.lib.configuration.core.value.impl.CachedConfigValue;
|
import cc.carm.lib.configuration.core.value.impl.ConfigValueMap;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.annotations.Unmodifiable;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements Map<K, V> {
|
public class ConfiguredMap<K, V> extends ConfigValueMap<K, V, Object> {
|
||||||
|
|
||||||
public static <K, V> @NotNull ConfigMapCreator<K, V> builderOf(@NotNull Class<K> keyClass,
|
|
||||||
@NotNull Class<V> valueClass) {
|
|
||||||
return builder().asMap(keyClass, valueClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final @NotNull Supplier<? extends Map<K, V>> supplier;
|
|
||||||
|
|
||||||
protected final @NotNull Class<K> keyClass;
|
|
||||||
protected final @NotNull Class<V> valueClass;
|
|
||||||
|
|
||||||
protected final @NotNull ConfigDataFunction<String, K> keyParser;
|
|
||||||
protected final @NotNull ConfigDataFunction<Object, V> valueParser;
|
|
||||||
|
|
||||||
protected final @NotNull ConfigDataFunction<K, String> keySerializer;
|
|
||||||
protected final @NotNull ConfigDataFunction<V, Object> valueSerializer;
|
|
||||||
|
|
||||||
|
|
||||||
public ConfiguredMap(@NotNull ValueManifest<Map<K, V>> manifest,
|
public ConfiguredMap(@NotNull ValueManifest<Map<K, V>> manifest,
|
||||||
@NotNull Supplier<? extends Map<K, V>> mapObjSupplier,
|
@NotNull Supplier<? extends Map<K, V>> mapObjSupplier,
|
||||||
@@ -42,165 +17,12 @@ public class ConfiguredMap<K, V> extends CachedConfigValue<Map<K, V>> implements
|
|||||||
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser,
|
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<Object, V> valueParser,
|
||||||
@NotNull ConfigDataFunction<K, String> keySerializer,
|
@NotNull ConfigDataFunction<K, String> keySerializer,
|
||||||
@NotNull ConfigDataFunction<V, Object> valueSerializer) {
|
@NotNull ConfigDataFunction<V, Object> valueSerializer) {
|
||||||
super(manifest);
|
super(manifest, Object.class, mapObjSupplier, keyClass, keyParser, valueClass, valueParser, keySerializer, valueSerializer);
|
||||||
this.supplier = mapObjSupplier;
|
|
||||||
this.keyClass = keyClass;
|
|
||||||
this.valueClass = valueClass;
|
|
||||||
this.keyParser = keyParser;
|
|
||||||
this.valueParser = valueParser;
|
|
||||||
this.keySerializer = keySerializer;
|
|
||||||
this.valueSerializer = valueSerializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Class<K> getKeyClass() {
|
|
||||||
return keyClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Class<V> getValueClass() {
|
|
||||||
return valueClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ConfigDataFunction<String, K> getKeyParser() {
|
|
||||||
return keyParser;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ConfigDataFunction<Object, V> getValueParser() {
|
|
||||||
return valueParser;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ConfigDataFunction<K, String> getKeySerializer() {
|
|
||||||
return keySerializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull ConfigDataFunction<V, Object> getValueSerializer() {
|
|
||||||
return valueSerializer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Map<K, V> get() {
|
public Object getSource(ConfigurationWrapper<?> section, String dataKey) {
|
||||||
if (!isExpired()) return getCachedOrDefault(supplier.get());
|
return section.get(dataKey);
|
||||||
|
|
||||||
// 已过时的数据,需要重新解析一次。
|
|
||||||
Map<K, V> map = supplier.get();
|
|
||||||
|
|
||||||
ConfigurationWrapper<?> section = getConfiguration().getConfigurationSection(getConfigPath());
|
|
||||||
if (section == null) return getDefaultFirst(map);
|
|
||||||
|
|
||||||
Set<String> keys = section.getKeys(false);
|
|
||||||
if (keys.isEmpty()) return getDefaultFirst(map);
|
|
||||||
|
|
||||||
for (String dataKey : keys) {
|
|
||||||
Object dataVal = section.get(dataKey);
|
|
||||||
if (dataVal == null) continue;
|
|
||||||
try {
|
|
||||||
K key = keyParser.parse(dataKey);
|
|
||||||
V value = valueParser.parse(dataVal);
|
|
||||||
map.put(key, value);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return updateCache(map);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public V get(Object key) {
|
|
||||||
return get().get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void set(@Nullable Map<K, V> value) {
|
|
||||||
updateCache(value);
|
|
||||||
if (value == null) setValue(null);
|
|
||||||
else {
|
|
||||||
Map<String, Object> data = new LinkedHashMap<>();
|
|
||||||
for (Map.Entry<K, V> entry : value.entrySet()) {
|
|
||||||
try {
|
|
||||||
data.put(
|
|
||||||
keySerializer.parse(entry.getKey()),
|
|
||||||
valueSerializer.parse(entry.getValue())
|
|
||||||
);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setValue(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T> @NotNull T modifyValue(Function<Map<K, V>, T> function) {
|
|
||||||
Map<K, V> m = get();
|
|
||||||
T result = function.apply(m);
|
|
||||||
set(m);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull Map<K, V> modifyMap(Consumer<Map<K, V>> consumer) {
|
|
||||||
Map<K, V> m = get();
|
|
||||||
consumer.accept(m);
|
|
||||||
set(m);
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
return get().size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return get().isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean containsKey(Object key) {
|
|
||||||
return get().containsKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean containsValue(Object value) {
|
|
||||||
return get().containsValue(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public V put(K key, V value) {
|
|
||||||
return modifyValue(m -> m.put(key, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public V remove(Object key) {
|
|
||||||
return modifyValue(m -> m.remove(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
|
|
||||||
modifyMap(map -> map.putAll(m));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clear() {
|
|
||||||
modifyMap(Map::clear);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Set<K> keySet() {
|
|
||||||
return get().keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Collection<V> values() {
|
|
||||||
return get().values();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
@Unmodifiable
|
|
||||||
public Set<Entry<K, V>> entrySet() {
|
|
||||||
return get().entrySet();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
package cc.carm.lib.configuration.core.value.type;
|
||||||
|
|
||||||
|
import cc.carm.lib.configuration.core.function.ConfigDataFunction;
|
||||||
|
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
|
||||||
|
import cc.carm.lib.configuration.core.value.ValueManifest;
|
||||||
|
import cc.carm.lib.configuration.core.value.impl.ConfigValueMap;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class ConfiguredSectionMap<K, V> extends ConfigValueMap<K, V, ConfigurationWrapper<?>> {
|
||||||
|
|
||||||
|
public ConfiguredSectionMap(@NotNull ValueManifest<Map<K, V>> manifest,
|
||||||
|
@NotNull Supplier<? extends Map<K, V>> mapObjSupplier,
|
||||||
|
@NotNull Class<K> keyClass, @NotNull ConfigDataFunction<String, K> keyParser,
|
||||||
|
@NotNull Class<V> valueClass, @NotNull ConfigDataFunction<ConfigurationWrapper<?>, V> valueParser,
|
||||||
|
@NotNull ConfigDataFunction<K, String> keySerializer,
|
||||||
|
@NotNull ConfigDataFunction<V, ? extends Map<String, Object>> valueSerializer) {
|
||||||
|
super(
|
||||||
|
manifest, ConfigurationWrapper.class, mapObjSupplier,
|
||||||
|
keyClass, keyParser, valueClass, valueParser,
|
||||||
|
keySerializer, valueSerializer.andThen(s -> (Object) s)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConfigurationWrapper<?> getSource(ConfigurationWrapper<?> section, String dataKey) {
|
||||||
|
return section.getConfigurationSection(dataKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+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.7.0</version>
|
<version>3.8.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyconfiguration-parent</artifactId>
|
<artifactId>easyconfiguration-parent</artifactId>
|
||||||
<version>3.7.0</version>
|
<version>3.8.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
+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.7.0</version>
|
<version>3.8.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
+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.7.0</version>
|
<version>3.8.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
CREATE TABLE IF NOT EXISTS conf
|
CREATE TABLE IF NOT EXISTS conf
|
||||||
(
|
(
|
||||||
`namespace` VARCHAR(255) NOT NULL, # 命名空间
|
`namespace` VARCHAR(255) NOT NULL, # 命名空间
|
||||||
`section` VARCHAR(255) NOT NULL, # 配置路径 (ConfigPath)
|
`path` VARCHAR(255) NOT NULL, # 配置路径 (ConfigPath)
|
||||||
`type` VARCHAR(255) NOT NULL, # 数据类型 (Integer/Byte/List/Map/...)
|
`type` TINYINT UNSIGNED NOT NULL DEFAULT 0, # 数据类型 (Integer/Byte/List/Map/...)
|
||||||
`value` MEDIUMTEXT, # 配置项的值 (可能为JSON格式)
|
`value` MEDIUMTEXT, # 配置项的值 (可能为JSON格式)
|
||||||
`inline_comments` TINYTEXT, # 行内注释
|
`inline_comments` TEXT, # 行内注释
|
||||||
`header_comments` TEXT, # 顶部注释
|
`header_comments` MEDIUMTEXT, # 顶部注释
|
||||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, # 创建时间
|
||||||
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (`namespace`, `section`)
|
PRIMARY KEY (`namespace`, `path`)
|
||||||
) ENGINE = InnoDB
|
) ENGINE = InnoDB
|
||||||
DEFAULT CHARSET = utf8mb4;
|
DEFAULT CHARSET = utf8mb4;
|
||||||
|
|
||||||
|
|||||||
+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.7.0</version>
|
<version>3.8.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import config.source.ModelConfiguration;
|
|||||||
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
|
import org.bspfsystems.yamlconfiguration.serialization.ConfigurationSerialization;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class DemoConfigTest {
|
public class DemoConfigTest {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@@ -17,7 +19,7 @@ public class DemoConfigTest {
|
|||||||
ConfigurationSerialization.registerClass(AnyModel.class);
|
ConfigurationSerialization.registerClass(AnyModel.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final YAMLConfigProvider provider = EasyConfiguration.from("target/config.yml", "config.yml");
|
protected final YAMLConfigProvider provider = EasyConfiguration.from("target/config.yml", "test/test2/config.yml");
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onTest() {
|
public void onTest() {
|
||||||
@@ -42,6 +44,8 @@ public class DemoConfigTest {
|
|||||||
if (anyModel != null) System.out.println(anyModel.getName());
|
if (anyModel != null) System.out.println(anyModel.getName());
|
||||||
|
|
||||||
ModelConfiguration.MODELS.forEach(System.out::println);
|
ModelConfiguration.MODELS.forEach(System.out::println);
|
||||||
|
ModelConfiguration.MODEL_MAP.forEach((v, anyModel1) -> System.out.println(v + " -> " + anyModel1.toString()));
|
||||||
|
|
||||||
|
|
||||||
System.out.println("----------------------------------------------------");
|
System.out.println("----------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,13 @@ import cc.carm.lib.configuration.core.annotation.ConfigPath;
|
|||||||
import cc.carm.lib.configuration.core.annotation.HeaderComment;
|
import cc.carm.lib.configuration.core.annotation.HeaderComment;
|
||||||
import cc.carm.lib.configuration.core.value.ConfigValue;
|
import cc.carm.lib.configuration.core.value.ConfigValue;
|
||||||
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
|
||||||
|
import cc.carm.lib.configuration.core.value.type.ConfiguredSectionMap;
|
||||||
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
|
import cc.carm.lib.configuration.demo.tests.model.AbstractModel;
|
||||||
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
import cc.carm.lib.configuration.yaml.value.ConfiguredSerializable;
|
||||||
import config.model.AnyModel;
|
import config.model.AnyModel;
|
||||||
import config.model.SomeModel;
|
import config.model.SomeModel;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@HeaderComment("以下内容用于测试序列化")
|
@HeaderComment("以下内容用于测试序列化")
|
||||||
@ConfigPath("model-test")
|
@ConfigPath("model-test")
|
||||||
public class ModelConfiguration extends ConfigurationRoot {
|
public class ModelConfiguration extends ConfigurationRoot {
|
||||||
@@ -25,10 +25,19 @@ public class ModelConfiguration extends ConfigurationRoot {
|
|||||||
);
|
);
|
||||||
|
|
||||||
public static final ConfiguredList<AnyModel> MODELS = ConfiguredList.builderOf(AnyModel.class)
|
public static final ConfiguredList<AnyModel> MODELS = ConfiguredList.builderOf(AnyModel.class)
|
||||||
.fromObject()
|
.fromMap()
|
||||||
.parseValue((section) -> AnyModel.deserialize((Map<String, ?>) section))
|
.parseValue(AnyModel::deserialize).serializeValue(AnyModel::serialize)
|
||||||
.serializeValue(AnyModel::serialize)
|
|
||||||
.defaults(AnyModel.random(), AnyModel.random(), AnyModel.random())
|
.defaults(AnyModel.random(), AnyModel.random(), AnyModel.random())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
public static final ConfiguredSectionMap<String, AnyModel> MODEL_MAP = ConfiguredMap.builderOf(String.class, AnyModel.class)
|
||||||
|
.asLinkedMap().fromSection()
|
||||||
|
.parseValue(v -> new AnyModel(v.getString("name", "EMPTY"), v.getBoolean("state", false)))
|
||||||
|
.serializeValue(AnyModel::serialize)
|
||||||
|
.defaults(m -> {
|
||||||
|
m.put("a", AnyModel.random());
|
||||||
|
m.put("b", AnyModel.random());
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
version: 1.0
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
version: 1.0
|
||||||
|
test-save: false
|
||||||
@@ -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.7.0</version>
|
<version>3.8.0</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>core</module>
|
<module>core</module>
|
||||||
<module>demo</module>
|
<module>demo</module>
|
||||||
|
|||||||
Reference in New Issue
Block a user