Skip to content

Commit

Permalink
Add Kotlin extensions and helpers around ObjectMapper and Processors (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DenWav authored Jul 5, 2023
1 parent 8a6b543 commit 3ad6a69
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.spongepowered.configurate.kotlin

import org.spongepowered.configurate.CommentedConfigurationNodeIntermediary
import org.spongepowered.configurate.ConfigurationNode
import org.spongepowered.configurate.objectmapping.meta.Comment
import org.spongepowered.configurate.objectmapping.meta.Processor
import java.lang.reflect.Type

/**
* Apply comments from [Comment] annotation on save. This will first call the [String.trimIndent] method on the strings,
* allowing multi-line Kotlin strings in [Comment] annotations without worrying about indentation.
*
* @return a new processor factory
* @since 4.2.0
*/
fun kotlinCommentsProcessor(): Processor.Factory<Comment, Any?> {
return Processor.Factory { data: Comment, _: Type? ->
Processor { _: Any?, destination: ConfigurationNode? ->
if (destination is CommentedConfigurationNodeIntermediary<*>) {
val comment = data.value.trimIndent()
if (data.override) {
destination.comment(comment)
} else {
destination.commentIfAbsent(comment)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package org.spongepowered.configurate.kotlin.extensions
import org.spongepowered.configurate.kotlin.typeTokenOf
import org.spongepowered.configurate.objectmapping.ObjectMapper
import org.spongepowered.configurate.objectmapping.ObjectMapper.Factory
import org.spongepowered.configurate.objectmapping.ObjectMapper.Factory.Builder
import org.spongepowered.configurate.objectmapping.meta.Constraint
import org.spongepowered.configurate.objectmapping.meta.Processor
import org.spongepowered.configurate.serialize.TypeSerializer
import org.spongepowered.configurate.serialize.TypeSerializerCollection
import kotlin.reflect.KClass
Expand Down Expand Up @@ -55,3 +58,69 @@ inline fun <reified T> TypeSerializerCollection.get(): TypeSerializer<T>? {
fun <T : Any> TypeSerializerCollection.get(type: KClass<T>): TypeSerializer<T>? {
return get(type.java)
}

// Processors
/**
* Register a [Processor] that will process fields after write, accepting Kotlin types.
* @see Builder.addProcessor
*/
fun <A : Annotation> Builder.addProcessor(definition: KClass<A>, factory: Processor.Factory<A, Any?>): Builder =
addProcessor(definition.java, factory)

/**
* Register a [Processor] that will process fields after write, accepting Kotlin types.
* @see Builder.addProcessor
*/
fun <A : Annotation, T : Any> Builder.addProcessor(
definition: KClass<A>,
valueType: KClass<T>,
factory: Processor.Factory<A, T?>,
): Builder = addProcessor(definition.java, valueType.java, factory)

/**
* Register a [Processor] that will process fields after write, accepting parameterized types.
* @see Builder.addProcessor
*/
inline fun <reified A : Annotation> Builder.addProcessor(factory: Processor.Factory<A, Any?>) =
addProcessor(A::class, factory)

/**
* Register a [Processor] that will process fields after write, accepting parameterized types.
* @see Builder.addProcessor
*/
@JvmName("addProcessorFull")
inline fun <reified A : Annotation, reified T : Any> Builder.addProcessor(factory: Processor.Factory<A, T?>) =
addProcessor(A::class, T::class, factory)

// Constraints
/**
* Register a [Constraint] that will be used to validate fields, accepting Kotlin types.
* @see Builder.addConstraint
*/
fun <A : Annotation> Builder.addConstraint(definition: KClass<A>, factory: Constraint.Factory<A, Any?>): Builder =
addConstraint(definition.java, factory)

/**
* Register a [Constraint] that will be used to validate fields, accepting Kotlin types.
* @see Builder.addConstraint
*/
fun <A : Annotation, T : Any> Builder.addConstraint(
definition: KClass<A>,
valueType: KClass<T>,
factory: Constraint.Factory<A, T?>,
): Builder = addConstraint(definition.java, valueType.java, factory)

/**
* Register a [Constraint] that will be used to validate fields, accepting parameterized types.
* @see Builder.addConstraint
*/
inline fun <reified A : Annotation> Builder.addConstraint(factory: Constraint.Factory<A, Any?>) =
addConstraint(A::class, factory)

/**
* Register a [Constraint] that will be used to validate fields, accepting parameterized types.
* @see Builder.addConstraint
*/
@JvmName("addConstraintFull")
inline fun <reified A : Annotation, reified T : Any> Builder.addConstraint(factory: Constraint.Factory<A, T?>) =
addConstraint(A::class, T::class, factory)

0 comments on commit 3ad6a69

Please sign in to comment.