Skip to content

Commit d27eecf

Browse files
committed
Support @comment on class
1 parent 4ab0870 commit d27eecf

File tree

2 files changed

+63
-31
lines changed

2 files changed

+63
-31
lines changed

core/src/main/kotlin/io/github/rothes/esu/core/configuration/ConfigLoader.kt

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import io.github.rothes.esu.core.configuration.serializer.*
88
import io.github.rothes.esu.core.module.configuration.EmptyConfiguration
99
import io.github.rothes.esu.core.util.extension.ClassExt.jarFile
1010
import io.github.rothes.esu.core.util.tree.TreeNode
11-
import io.github.rothes.esu.lib.configurate.BasicConfigurationNode
12-
import io.github.rothes.esu.lib.configurate.CommentedConfigurationNode
13-
import io.github.rothes.esu.lib.configurate.CommentedConfigurationNodeIntermediary
14-
import io.github.rothes.esu.lib.configurate.ConfigurationNode
11+
import io.github.rothes.esu.lib.configurate.*
1512
import io.github.rothes.esu.lib.configurate.loader.HeaderMode
13+
import io.github.rothes.esu.lib.configurate.objectmapping.ConfigSerializable
1614
import io.github.rothes.esu.lib.configurate.objectmapping.ObjectMapper
1715
import io.github.rothes.esu.lib.configurate.objectmapping.meta.NodeResolver
1816
import io.github.rothes.esu.lib.configurate.objectmapping.meta.Processor
1917
import io.github.rothes.esu.lib.configurate.serialize.ScalarSerializer
18+
import io.github.rothes.esu.lib.configurate.serialize.TypeSerializer
2019
import io.github.rothes.esu.lib.configurate.util.MapFactories
2120
import io.github.rothes.esu.lib.configurate.yaml.NodeStyle
2221
import io.github.rothes.esu.lib.configurate.yaml.YamlConfigurationLoader
@@ -219,32 +218,11 @@ object ConfigLoader {
219218
.lineLength(150)
220219
.commentsEnabled(true)
221220
.defaultOptions { options ->
221+
val commentProcesser = CommentProcesser(resourceNode)
222222
val factory: ObjectMapper.Factory = ObjectMapper.factoryBuilder()
223223
.addProcessor(Comment::class.java) { data, _ ->
224224
Processor { _, destination ->
225-
if (destination is CommentedConfigurationNodeIntermediary<*>) {
226-
val resourceComment = (resourceNode?.node(destination.path()) as? CommentedConfigurationNodeIntermediary<*>)?.comment()
227-
if (resourceComment != null && destination.comment() == data.value.trimIndent()) {
228-
destination.comment(resourceComment)
229-
return@Processor
230-
}
231-
val resourceOld = resourceNode?.node("_oc_")?.node(destination.path()) // _oc_ = _old_comments_
232-
val allOld = (
233-
if (resourceOld != null && resourceOld.isList)
234-
resourceOld.getList(String::class.java)!!.plus(data.overrideOld)
235-
else
236-
data.overrideOld.toList()
237-
).map { it.toString().trimIndent() }
238-
239-
if (allOld.isEmpty() || destination.comment() == null) {
240-
destination.commentIfAbsent(resourceComment ?: data.value.trimIndent())
241-
} else if (data.overrideOld.firstOrNull() == OVERRIDE_ALWAYS) {
242-
destination.comment(resourceComment ?: data.value.trimIndent())
243-
} else if (allOld.contains(destination.comment())) {
244-
destination.comment(resourceComment ?: data.value.trimIndent())
245-
}
246-
247-
}
225+
commentProcesser.process(data, destination)
248226
}
249227
}
250228
.addProcessor(MoveToTop::class.java) { _, _ ->
@@ -314,6 +292,7 @@ object ConfigLoader {
314292
} else null
315293
}
316294
.build()
295+
val objectSerializer = ClassProceedSerializer(commentProcesser, factory.asTypeSerializer())
317296
options.mapFactory(MapFactories.insertionOrdered())
318297
.serializers {
319298
if (serverAdventure) {
@@ -328,8 +307,12 @@ object ConfigLoader {
328307
GenericTypeReflector.erase(type).let { clazz ->
329308
ConfigurationPart::class.java.isAssignableFrom(clazz)
330309
}
331-
}, factory.asTypeSerializer())
332-
.registerAnnotatedObjects(factory)
310+
}, objectSerializer)
311+
.register( // registerAnnotatedObjects(factory)
312+
{ type ->
313+
GenericTypeReflector.annotate(type).isAnnotationPresent(ConfigSerializable::class.java)
314+
}, objectSerializer
315+
)
333316
.register(TypeToken.get(List::class.java), ListSerializer)
334317
.register(TypeToken.get(Map::class.java), MapSerializer)
335318
.register(TypeToken.get(Optional::class.java), OptionalSerializer)
@@ -365,7 +348,7 @@ object ConfigLoader {
365348
false
366349
}
367350
}
368-
}, factory.asTypeSerializer()
351+
}, objectSerializer
369352
)
370353
}.let {
371354
if (resourceNode != null) {
@@ -625,4 +608,53 @@ object ConfigLoader {
625608
}
626609
}
627610

611+
private class CommentProcesser(val resourceNode: ConfigurationNode?) {
612+
613+
fun process(comment: Comment, node: ConfigurationNode) {
614+
if (node is CommentedConfigurationNodeIntermediary<*>) {
615+
val resourceComment =
616+
(resourceNode?.node(node.path()) as? CommentedConfigurationNodeIntermediary<*>)?.comment()
617+
if (resourceComment != null && node.comment() == comment.value.trimIndent()) {
618+
node.comment(resourceComment)
619+
return
620+
}
621+
val resourceOld = resourceNode?.node("_oc_")?.node(node.path()) // _oc_ = _old_comments_
622+
val allOld = (if (resourceOld != null && resourceOld.isList) resourceOld.getList(String::class.java)!!
623+
.plus(comment.overrideOld)
624+
else comment.overrideOld.toList()).map { it.toString().trimIndent() }
625+
626+
if (allOld.isEmpty() || node.comment() == null) {
627+
node.commentIfAbsent(resourceComment ?: comment.value.trimIndent())
628+
} else if (comment.overrideOld.firstOrNull() == OVERRIDE_ALWAYS) {
629+
node.comment(resourceComment ?: comment.value.trimIndent())
630+
} else if (allOld.contains(node.comment())) {
631+
node.comment(resourceComment ?: comment.value.trimIndent())
632+
}
633+
}
634+
}
635+
}
636+
637+
private class ClassProceedSerializer(
638+
private val commentProcesser: CommentProcesser,
639+
private val factory: TypeSerializer<Any>,
640+
): TypeSerializer<Any> {
641+
642+
override fun deserialize(type: Type?, node: ConfigurationNode?): Any? {
643+
return factory.deserialize(type, node)
644+
}
645+
646+
override fun serialize(type: Type?, obj: Any?, node: ConfigurationNode?) {
647+
factory.serialize(type, obj, node)
648+
649+
node ?: return
650+
val comment = obj?.javaClass?.getAnnotation(Comment::class.java) ?: return
651+
commentProcesser.process(comment, node)
652+
}
653+
654+
override fun emptyValue(specificType: Type?, options: ConfigurationOptions?): Any? {
655+
return factory.emptyValue(specificType, options)
656+
}
657+
658+
}
659+
628660
}

core/src/main/kotlin/io/github/rothes/esu/core/configuration/meta/Comment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package io.github.rothes.esu.core.configuration.meta
22

33
const val OVERRIDE_ALWAYS = "<ESU_ALWAYS_OVERRIDE>"
44

5-
@Target(AnnotationTarget.FIELD)
5+
@Target(AnnotationTarget.FIELD, AnnotationTarget.CLASS)
66
annotation class Comment(
77
val value: String = "",
88
val overrideOld: Array<String> = [],

0 commit comments

Comments
 (0)