[+] Generalize data import for chusan

This commit is contained in:
Azalea
2024-03-30 23:52:29 -04:00
parent d4178c85a9
commit 3a8616e225
59 changed files with 390 additions and 333 deletions

View File

@@ -33,6 +33,7 @@ typealias RB = RequestBody
typealias RH = RequestHeader
typealias PV = PathVariable
typealias API = RequestMapping
typealias Var<T, V> = KMutableProperty1<T, V>
typealias Str = String
typealias Bool = Boolean
typealias JavaSerializable = java.io.Serializable
@@ -50,13 +51,13 @@ annotation class SettingField(val name: Str, val desc: Str)
// Reflection
@Suppress("UNCHECKED_CAST")
fun <T : Any> KClass<T>.vars() = memberProperties.mapNotNull { it as? KMutableProperty1<T, Any> }
fun <T : Any> KClass<T>.vars() = memberProperties.mapNotNull { it as? Var<T, Any> }
fun <T : Any> KClass<T>.varsMap() = vars().associateBy { it.name }
fun <T : Any> KClass<T>.getters() = java.methods.filter { it.name.startsWith("get") }
fun <T : Any> KClass<T>.gettersMap() = getters().associateBy { it.name.removePrefix("get").decapitalize() }
infix fun KCallable<*>.returns(type: KClass<*>) = returnType.jvmErasure.isSubclassOf(type)
@Suppress("UNCHECKED_CAST")
fun <C, T: Any> KMutableProperty1<C, T>.setCast(obj: C, value: String) = set(obj, when (returnType.classifier) {
fun <C, T: Any> Var<C, T>.setCast(obj: C, value: String) = set(obj, when (returnType.classifier) {
String::class -> value
Int::class -> value.toInt()
Boolean::class -> value.toBoolean()

View File

@@ -35,11 +35,11 @@ val JACKSON = jacksonObjectMapper().apply {
inline fun <reified T> ObjectMapper.parse(str: Str) = readValue(str, T::class.java)
inline fun <reified T> ObjectMapper.parse(map: Map<*, *>) = convertValue(map, T::class.java)
// TODO: https://stackoverflow.com/q/78197784/7346633
inline fun <reified T> Str.parseJackson() = if (contains("null")) {
fun <T> Str.parseJackson(cls: Class<T>) = if (contains("null")) {
val map = JACKSON.parse<MutableMap<String, Any>>(this)
JACKSON.convertValue(map.recursiveNotNull(), T::class.java)
JACKSON.convertValue(map.recursiveNotNull(), cls)
}
else JACKSON.readValue(this, T::class.java)
else JACKSON.readValue(this, cls)
fun <T> T.toJson() = JACKSON.writeValueAsString(this)
inline fun <reified T> String.json() = try {