-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlin extensions and helpers around ObjectMapper and Processors (#…
…409)
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
extra/kotlin/src/main/kotlin/org/spongepowered/configurate/kotlin/Processors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters