Skip to content

Commit

Permalink
Merge remote-tracking branch 'tim/feature/interfaces'
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Sep 10, 2024
2 parents fffd285 + 7b4769c commit f5fb9d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() {
Expand Down Expand Up @@ -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<ObjectMapper.Factory.Builder> objectMapperConsumer) {
return options.serializers(serializers -> {
final ObjectMapper.Factory.Builder builder = InterfaceMiddleware.buildObjectMapperWithMiddleware();
objectMapperConsumer.accept(builder);
serializers.registerAnnotated(InterfaceTypeSerializer::applicable, InterfaceTypeSerializer.INSTANCE)
.registerAnnotatedObjects(builder.build());
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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, Number> decimalRange() {
Expand Down

0 comments on commit f5fb9d3

Please sign in to comment.