From 7bb0edbbb8af5af361567e74787abc396b4687f2 Mon Sep 17 00:00:00 2001 From: JFronny Date: Tue, 18 Jul 2023 19:20:00 +0200 Subject: [PATCH] style(config): Move IO to dedicated class --- .../config/impl/dsl/ConfigBuilderImpl.java | 87 +----------------- .../config/impl/dsl/DefaultConfigIO.java | 88 +++++++++++++++++++ 2 files changed, 90 insertions(+), 85 deletions(-) create mode 100644 libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/DefaultConfigIO.java diff --git a/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/ConfigBuilderImpl.java b/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/ConfigBuilderImpl.java index d5709cd..b395e19 100644 --- a/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/ConfigBuilderImpl.java +++ b/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/ConfigBuilderImpl.java @@ -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 implements ConfigBuilder { @@ -30,79 +15,11 @@ public class ConfigBuilderImpl extends CategoryBuilderImpl 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 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> 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 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 load) { checkBuilt(); diff --git a/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/DefaultConfigIO.java b/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/DefaultConfigIO.java new file mode 100644 index 0000000..53dadd9 --- /dev/null +++ b/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/DefaultConfigIO.java @@ -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 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 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> entry : cat.migrations.entrySet()) { + if (source.has(entry.getKey())) { + entry.getValue().accept(source.get(entry.getKey())); + } + } + } + } + + public static Consumer 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 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(); + } +}