style(config): Move IO to dedicated class

This commit is contained in:
Johannes Frohnmeyer 2023-07-18 19:20:00 +02:00
parent 3901078c52
commit 7bb0edbbb8
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 90 additions and 85 deletions

View File

@ -1,25 +1,10 @@
package io.gitlab.jfronny.libjf.config.impl.dsl;
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.gson.JsonElement;
import io.gitlab.jfronny.gson.JsonObject;
import io.gitlab.jfronny.gson.JsonParser;
import io.gitlab.jfronny.gson.stream.JsonWriter;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.config.api.v1.ConfigCategory;
import io.gitlab.jfronny.libjf.config.api.v1.ConfigInstance;
import io.gitlab.jfronny.libjf.config.api.v1.EntryInfo;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.ConfigBuilder;
import io.gitlab.jfronny.libjf.config.impl.entrypoint.JfConfigSafe;
import io.gitlab.jfronny.libjf.config.impl.watch.JfConfigWatchService;
import net.fabricmc.loader.api.FabricLoader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.Consumer;
public class ConfigBuilderImpl extends CategoryBuilderImpl<ConfigBuilderImpl> implements ConfigBuilder<ConfigBuilderImpl> {
@ -30,79 +15,11 @@ public class ConfigBuilderImpl extends CategoryBuilderImpl<ConfigBuilderImpl> im
public ConfigBuilderImpl(String id) {
super(id, "");
load = c -> {
c.getFilePath().ifPresent(path -> {
if (Files.exists(path)) {
try (BufferedReader br = Files.newBufferedReader(path)) {
JsonElement element = JsonParser.parseReader(br);
if (element.isJsonObject()) loadFrom(element.getAsJsonObject(), c);
else LibJf.LOGGER.error("Invalid config: Not a JSON object for " + id);
} catch (Exception e) {
LibJf.LOGGER.error("Could not read config for " + id, e);
}
}
c.write();
});
};
write = c -> {
c.getFilePath().ifPresent(path -> JfConfigWatchService.lock(path, () -> {
try (BufferedWriter bw = Files.newBufferedWriter(path);
JsonWriter jw = GsonHolders.CONFIG.getGson().newJsonWriter(bw)) {
writeTo(jw, c);
} catch (Exception e) {
LibJf.LOGGER.error("Could not write config for " + id, e);
}
}));
};
load = DefaultConfigIO.loader(id);
write = DefaultConfigIO.writer(id);
path = FabricLoader.getInstance().getConfigDir().resolve(id + ".json5");
}
private static void loadFrom(JsonObject source, ConfigCategory category) {
for (EntryInfo<?> entry : category.getEntries()) {
if (source.has(entry.getName())) {
try {
entry.loadFromJson(source.get(entry.getName()));
} catch (IllegalAccessException e) {
LibJf.LOGGER.error("Could not set config entry value of " + entry.getName(), e);
}
} else LibJf.LOGGER.error("Config does not contain entry for " + entry.getName());
}
for (Map.Entry<String, ConfigCategory> entry : category.getCategories().entrySet()) {
if (source.has(entry.getKey())) {
JsonElement el = source.get(entry.getKey());
if (el.isJsonObject()) loadFrom(el.getAsJsonObject(), entry.getValue());
else LibJf.LOGGER.error("Config category is not a JSON object, skipping");
} else LibJf.LOGGER.error("Config does not contain entry for subcategory " + entry.getKey());
}
if (category instanceof DslConfigCategory cat) {
for (Map.Entry<String, Consumer<JsonElement>> entry : cat.migrations.entrySet()) {
if (source.has(entry.getKey())) {
entry.getValue().accept(source.get(entry.getKey()));
}
}
}
}
private static void writeTo(JsonWriter writer, ConfigCategory category) throws IOException {
category.fix();
writer.beginObject();
String val;
for (EntryInfo<?> entry : category.getEntries()) {
try {
entry.writeTo(writer, category.getTranslationPrefix());
} catch (IllegalAccessException e) {
LibJf.LOGGER.error("Could not write entry", e);
}
}
for (Map.Entry<String, ConfigCategory> entry : category.getCategories().entrySet()) {
if ((val = JfConfigSafe.TRANSLATION_SUPPLIER.apply(category.getTranslationPrefix() + entry.getKey() + ".title")) != null)
writer.comment(val);
writer.name(entry.getKey());
writeTo(writer, entry.getValue());
}
writer.endObject();
}
@Override
public ConfigBuilderImpl setLoadMethod(Consumer<ConfigInstance> load) {
checkBuilt();

View File

@ -0,0 +1,88 @@
package io.gitlab.jfronny.libjf.config.impl.dsl;
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.gson.*;
import io.gitlab.jfronny.gson.stream.JsonWriter;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.config.api.v1.*;
import io.gitlab.jfronny.libjf.config.impl.entrypoint.JfConfigSafe;
import io.gitlab.jfronny.libjf.config.impl.watch.JfConfigWatchService;
import java.io.*;
import java.nio.file.Files;
import java.util.Map;
import java.util.function.Consumer;
public class DefaultConfigIO {
public static Consumer<ConfigInstance> loader(String id) {
return c -> c.getFilePath().ifPresent(path -> {
if (Files.exists(path)) {
try (BufferedReader br = Files.newBufferedReader(path)) {
JsonElement element = JsonParser.parseReader(br);
if (element.isJsonObject()) loadFrom(element.getAsJsonObject(), c);
else LibJf.LOGGER.error("Invalid config: Not a JSON object for " + id);
} catch (Exception e) {
LibJf.LOGGER.error("Could not read config for " + id, e);
}
}
c.write();
});
}
private static void loadFrom(JsonObject source, ConfigCategory category) {
for (EntryInfo<?> entry : category.getEntries()) {
if (source.has(entry.getName())) {
try {
entry.loadFromJson(source.get(entry.getName()));
} catch (IllegalAccessException e) {
LibJf.LOGGER.error("Could not set config entry value of " + entry.getName(), e);
}
} else LibJf.LOGGER.error("Config does not contain entry for " + entry.getName());
}
for (Map.Entry<String, ConfigCategory> entry : category.getCategories().entrySet()) {
if (source.has(entry.getKey())) {
JsonElement el = source.get(entry.getKey());
if (el.isJsonObject()) loadFrom(el.getAsJsonObject(), entry.getValue());
else LibJf.LOGGER.error("Config category is not a JSON object, skipping");
} else LibJf.LOGGER.error("Config does not contain entry for subcategory " + entry.getKey());
}
if (category instanceof DslConfigCategory cat) {
for (Map.Entry<String, Consumer<JsonElement>> entry : cat.migrations.entrySet()) {
if (source.has(entry.getKey())) {
entry.getValue().accept(source.get(entry.getKey()));
}
}
}
}
public static Consumer<ConfigInstance> writer(String id) {
return c -> c.getFilePath().ifPresent(path -> JfConfigWatchService.lock(path, () -> {
try (BufferedWriter bw = Files.newBufferedWriter(path);
JsonWriter jw = GsonHolders.CONFIG.getGson().newJsonWriter(bw)) {
writeTo(jw, c);
} catch (Exception e) {
LibJf.LOGGER.error("Could not write config for " + id, e);
}
}));
}
private static void writeTo(JsonWriter writer, ConfigCategory category) throws IOException {
category.fix();
writer.beginObject();
String val;
for (EntryInfo<?> entry : category.getEntries()) {
try {
entry.writeTo(writer, category.getTranslationPrefix());
} catch (IllegalAccessException e) {
LibJf.LOGGER.error("Could not write entry", e);
}
}
for (Map.Entry<String, ConfigCategory> entry : category.getCategories().entrySet()) {
if ((val = JfConfigSafe.TRANSLATION_SUPPLIER.apply(category.getTranslationPrefix() + entry.getKey() + ".title")) != null)
writer.comment(val);
writer.name(entry.getKey());
writeTo(writer, entry.getValue());
}
writer.endObject();
}
}