From 305a723374e2bd730a181798dfd90f7eb5fb70ac Mon Sep 17 00:00:00 2001 From: isXander Date: Tue, 4 Apr 2023 11:19:15 +0100 Subject: 📦 New builder API for GsonConfigInstance + deprecation of old constructors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../isxander/yacl/config/GsonConfigInstance.java | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) (limited to 'src/main/java/dev/isxander/yacl') 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 extends ConfigInstance { private final Gson gson; private final Path path; + @Deprecated public GsonConfigInstance(Class configClass, Path path) { this(configClass, path, new GsonBuilder()); } + @Deprecated public GsonConfigInstance(Class configClass, Path path, Gson gson) { this(configClass, path, gson.newBuilder()); } + @Deprecated public GsonConfigInstance(Class configClass, Path path, UnaryOperator builder) { this(configClass, path, builder.apply(new GsonBuilder())); } + @Deprecated public GsonConfigInstance(Class configClass, Path path, GsonBuilder builder) { super(configClass); this.path = path; @@ -51,6 +55,12 @@ public class GsonConfigInstance extends ConfigInstance { .create(); } + private GsonConfigInstance(Class 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 extends ConfigInstance { return new JsonPrimitive(color.getRGB()); } } + + /** + * Creates a builder for a GSON config instance. + * @param configClass the config class + * @return a new builder + * @param the config type + */ + public static Builder createBuilder(Class configClass) { + return new Builder<>(configClass); + } + + public static class Builder { + private final Class configClass; + private Path path; + private UnaryOperator 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 configClass) { + this.configClass = configClass; + } + + /** + * Sets the file path to save and load the config from. + */ + public Builder setPath(Path path) { + this.path = path; + return this; + } + + /** + * Sets the GSON instance to use. Overrides all YACL defaults such as: + *
    + *
  • lower_camel_case field naming policy
  • + *
  • null serialization
  • + *
  • {@link Component}, {@link Style} and {@link Color} type adapters
  • + *
+ * 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 overrideGsonBuilder(GsonBuilder gsonBuilder) { + this.gsonBuilder = builder -> gsonBuilder; + return this; + } + + /** + * Sets the GSON instance to use. Overrides all YACL defaults such as: + *
    + *
  • lower_camel_case field naming policy
  • + *
  • null serialization
  • + *
  • {@link Component}, {@link Style} and {@link Color} type adapters
  • + *
+ * 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 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. + *

+ * By default, YACL sets the GSON with the following options: + *

    + *
  • lower_camel_case field naming policy
  • + *
  • null serialization
  • + *
  • {@link Component}, {@link Style} and {@link Color} type adapters
  • + *
+ * + * @param gsonBuilder the function to apply to the builder + */ + public Builder appendGsonBuilder(UnaryOperator gsonBuilder) { + this.gsonBuilder = builder -> gsonBuilder.apply(this.gsonBuilder.apply(builder)); + return this; + } + + /** + * Builds the config instance. + * @return the built config instance + */ + public GsonConfigInstance build() { + UnaryOperator gsonBuilder = builder -> this.gsonBuilder.apply(builder) + .addSerializationExclusionStrategy(new ConfigExclusionStrategy()) + .addDeserializationExclusionStrategy(new ConfigExclusionStrategy()); + + return new GsonConfigInstance<>(configClass, path, gsonBuilder.apply(new GsonBuilder()).create(), true); + } + } } -- cgit