aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorisXander <xander@isxander.dev>2023-04-04 11:19:15 +0100
committerisXander <xander@isxander.dev>2023-04-04 11:19:15 +0100
commit305a723374e2bd730a181798dfd90f7eb5fb70ac (patch)
treecaffd5bac96bda5570a6b20acdf945bc02a16f56 /src
parentb98f9b6ec98a69955fc45cd6d1b8796f14ee9cb5 (diff)
downloadYetAnotherConfigLib-305a723374e2bd730a181798dfd90f7eb5fb70ac.tar.gz
YetAnotherConfigLib-305a723374e2bd730a181798dfd90f7eb5fb70ac.tar.bz2
YetAnotherConfigLib-305a723374e2bd730a181798dfd90f7eb5fb70ac.zip
📦 New builder API for GsonConfigInstance + deprecation of old constructors
Diffstat (limited to 'src')
-rw-r--r--src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java106
-rw-r--r--src/testmod/java/dev/isxander/yacl/test/config/ExampleConfig.java4
2 files changed, 109 insertions, 1 deletions
diff --git a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java
index c607606..ad7f550 100644
--- a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java
+++ b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java
@@ -26,18 +26,22 @@ public class GsonConfigInstance<T> extends ConfigInstance<T> {
private final Gson gson;
private final Path path;
+ @Deprecated
public GsonConfigInstance(Class<T> configClass, Path path) {
this(configClass, path, new GsonBuilder());
}
+ @Deprecated
public GsonConfigInstance(Class<T> configClass, Path path, Gson gson) {
this(configClass, path, gson.newBuilder());
}
+ @Deprecated
public GsonConfigInstance(Class<T> configClass, Path path, UnaryOperator<GsonBuilder> builder) {
this(configClass, path, builder.apply(new GsonBuilder()));
}
+ @Deprecated
public GsonConfigInstance(Class<T> configClass, Path path, GsonBuilder builder) {
super(configClass);
this.path = path;
@@ -51,6 +55,12 @@ public class GsonConfigInstance<T> extends ConfigInstance<T> {
.create();
}
+ private GsonConfigInstance(Class<T> configClass, Path path, Gson gson, boolean fromBuilder) {
+ super(configClass);
+ this.path = path;
+ this.gson = gson;
+ }
+
@Override
public void save() {
try {
@@ -103,4 +113,100 @@ public class GsonConfigInstance<T> extends ConfigInstance<T> {
return new JsonPrimitive(color.getRGB());
}
}
+
+ /**
+ * Creates a builder for a GSON config instance.
+ * @param configClass the config class
+ * @return a new builder
+ * @param <T> the config type
+ */
+ public static <T> Builder<T> createBuilder(Class<T> configClass) {
+ return new Builder<>(configClass);
+ }
+
+ public static class Builder<T> {
+ private final Class<T> configClass;
+ private Path path;
+ private UnaryOperator<GsonBuilder> gsonBuilder = builder -> builder
+ .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
+ .serializeNulls()
+ .registerTypeHierarchyAdapter(Component.class, new Component.Serializer())
+ .registerTypeHierarchyAdapter(Style.class, new Style.Serializer())
+ .registerTypeHierarchyAdapter(Color.class, new ColorTypeAdapter());
+
+ private Builder(Class<T> configClass) {
+ this.configClass = configClass;
+ }
+
+ /**
+ * Sets the file path to save and load the config from.
+ */
+ public Builder<T> setPath(Path path) {
+ this.path = path;
+ return this;
+ }
+
+ /**
+ * Sets the GSON instance to use. Overrides all YACL defaults such as:
+ * <ul>
+ * <li>lower_camel_case field naming policy</li>
+ * <li>null serialization</li>
+ * <li>{@link Component}, {@link Style} and {@link Color} type adapters</li>
+ * </ul>
+ * Still respects the exclusion strategy to only serialize {@link ConfigEntry}
+ * but these can be added to with setExclusionStrategies.
+ *
+ * @param gsonBuilder gson builder to use
+ */
+ public Builder<T> overrideGsonBuilder(GsonBuilder gsonBuilder) {
+ this.gsonBuilder = builder -> gsonBuilder;
+ return this;
+ }
+
+ /**
+ * Sets the GSON instance to use. Overrides all YACL defaults such as:
+ * <ul>
+ * <li>lower_camel_case field naming policy</li>
+ * <li>null serialization</li>
+ * <li>{@link Component}, {@link Style} and {@link Color} type adapters</li>
+ * </ul>
+ * Still respects the exclusion strategy to only serialize {@link ConfigEntry}
+ * but these can be added to with setExclusionStrategies.
+ *
+ * @param gson gson instance to be converted to a builder
+ */
+ public Builder<T> overrideGsonBuilder(Gson gson) {
+ return this.overrideGsonBuilder(gson.newBuilder());
+ }
+
+ /**
+ * Appends extra configuration to a GSON builder.
+ * This is the intended way to add functionality to the GSON instance.
+ * <p>
+ * By default, YACL sets the GSON with the following options:
+ * <ul>
+ * <li>lower_camel_case field naming policy</li>
+ * <li>null serialization</li>
+ * <li>{@link Component}, {@link Style} and {@link Color} type adapters</li>
+ * </ul>
+ *
+ * @param gsonBuilder the function to apply to the builder
+ */
+ public Builder<T> appendGsonBuilder(UnaryOperator<GsonBuilder> gsonBuilder) {
+ this.gsonBuilder = builder -> gsonBuilder.apply(this.gsonBuilder.apply(builder));
+ return this;
+ }
+
+ /**
+ * Builds the config instance.
+ * @return the built config instance
+ */
+ public GsonConfigInstance<T> build() {
+ UnaryOperator<GsonBuilder> gsonBuilder = builder -> this.gsonBuilder.apply(builder)
+ .addSerializationExclusionStrategy(new ConfigExclusionStrategy())
+ .addDeserializationExclusionStrategy(new ConfigExclusionStrategy());
+
+ return new GsonConfigInstance<>(configClass, path, gsonBuilder.apply(new GsonBuilder()).create(), true);
+ }
+ }
}
diff --git a/src/testmod/java/dev/isxander/yacl/test/config/ExampleConfig.java b/src/testmod/java/dev/isxander/yacl/test/config/ExampleConfig.java
index 5d864da..717132b 100644
--- a/src/testmod/java/dev/isxander/yacl/test/config/ExampleConfig.java
+++ b/src/testmod/java/dev/isxander/yacl/test/config/ExampleConfig.java
@@ -9,7 +9,9 @@ import java.util.List;
import java.nio.file.Path;
public class ExampleConfig {
- public static final ConfigInstance<ExampleConfig> INSTANCE = new GsonConfigInstance<>(ExampleConfig.class, Path.of("./config/yacl-test.json"));
+ public static final ConfigInstance<ExampleConfig> INSTANCE = GsonConfigInstance.createBuilder(ExampleConfig.class)
+ .setPath(Path.of("./config/yacl-test.json"))
+ .build();
@ConfigEntry public boolean booleanToggle = false;
@ConfigEntry public boolean customBooleanToggle = false;