From 7b4769c394d67e8a564157cbb263c699131f8027 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:46:33 -0400 Subject: [PATCH] InterfaceDefaultOptions#addTo with ObjectMapper modification --- .../interfaces/InterfaceDefaultOptions.java | 23 ++++++++++++++++++- .../interfaces/InterfaceMiddleware.java | 5 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/extra/interface/src/main/java/org/spongepowered/configurate/interfaces/InterfaceDefaultOptions.java b/extra/interface/src/main/java/org/spongepowered/configurate/interfaces/InterfaceDefaultOptions.java index b6806109..b99f3d82 100644 --- a/extra/interface/src/main/java/org/spongepowered/configurate/interfaces/InterfaceDefaultOptions.java +++ b/extra/interface/src/main/java/org/spongepowered/configurate/interfaces/InterfaceDefaultOptions.java @@ -17,8 +17,11 @@ package org.spongepowered.configurate.interfaces; import org.spongepowered.configurate.ConfigurationOptions; +import org.spongepowered.configurate.objectmapping.ObjectMapper; import org.spongepowered.configurate.serialize.TypeSerializerCollection; +import java.util.function.Consumer; + /** * This class has the default {@link ConfigurationOptions} * with {@link InterfaceTypeSerializer} added to the serializers. @@ -30,7 +33,7 @@ public final class InterfaceDefaultOptions { private static final TypeSerializerCollection DEFAULTS = TypeSerializerCollection.builder() .registerAnnotated(InterfaceTypeSerializer::applicable, InterfaceTypeSerializer.INSTANCE) - .registerAnnotatedObjects(InterfaceMiddleware.buildObjectMapperWithMiddleware()) + .registerAnnotatedObjects(InterfaceMiddleware.buildObjectMapperWithMiddleware().build()) .build(); private InterfaceDefaultOptions() { @@ -60,4 +63,22 @@ public static ConfigurationOptions addTo(final ConfigurationOptions options) { return options.serializers(serializers -> serializers.registerAll(DEFAULTS)); } + /** + * {@link #addTo(ConfigurationOptions)} with an option to customize the {@link ObjectMapper.Factory}. + * + * @param options to transform the existing default options + * @param objectMapperConsumer to transform the ObjectMapper factory + * @return the default options with the applied changes + * @since 4.2.0 + */ + public static ConfigurationOptions addTo(final ConfigurationOptions options, + final Consumer objectMapperConsumer) { + return options.serializers(serializers -> { + final ObjectMapper.Factory.Builder builder = InterfaceMiddleware.buildObjectMapperWithMiddleware(); + objectMapperConsumer.accept(builder); + serializers.registerAnnotated(InterfaceTypeSerializer::applicable, InterfaceTypeSerializer.INSTANCE) + .registerAnnotatedObjects(builder.build()); + }); + } + } diff --git a/extra/interface/src/main/java/org/spongepowered/configurate/interfaces/InterfaceMiddleware.java b/extra/interface/src/main/java/org/spongepowered/configurate/interfaces/InterfaceMiddleware.java index 9b238155..6b744188 100644 --- a/extra/interface/src/main/java/org/spongepowered/configurate/interfaces/InterfaceMiddleware.java +++ b/extra/interface/src/main/java/org/spongepowered/configurate/interfaces/InterfaceMiddleware.java @@ -37,13 +37,12 @@ final class InterfaceMiddleware { private InterfaceMiddleware() { } - static ObjectMapper.Factory buildObjectMapperWithMiddleware() { + static ObjectMapper.Factory.Builder buildObjectMapperWithMiddleware() { return ObjectMapper.factoryBuilder() .addConstraint(DecimalRange.class, Number.class, decimalRange()) .addConstraint(NumericRange.class, Number.class, numericRange()) .addConstraint(StringRange.class, String.class, stringRange()) - .addProcessor(Hidden.class, hiddenProcessor()) - .build(); + .addProcessor(Hidden.class, hiddenProcessor()); } private static Constraint.Factory decimalRange() {