Map strategy

This commit is contained in:
bskjon committed 2026-09-16 16:34:27 +02:00
1 parent 22a8761f14
commit 21556018c8
4 files changed
+38 -4

No files matched your search

@@ -0,0 +1,6 @@
package no.iktdev.ts
enum class MapStrategy {
MapAsPartialRecord,
MapAsRecord
}
@@ -1,6 +1,8 @@
package no.iktdev.ts package no.iktdev.ts
import no.iktdev.ts.anno.TsMapStrategy
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.memberProperties import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor import kotlin.reflect.full.primaryConstructor
@@ -111,10 +113,17 @@ class TsModelRenderer {
.filter { it.name !in inheritedPropNames } .filter { it.name !in inheritedPropNames }
.filter { forcedTypeLiteral == null || it.name != "type" } .filter { forcedTypeLiteral == null || it.name != "type" }
.joinToString("\n") { prop -> .joinToString("\n") { prop ->
val mapStrategy = prop.findAnnotation<TsMapStrategy>()?.value
?: MapStrategy.MapAsRecord
val tsType = if (prop.name == "type" && defaultTypeLiteral != null) { val tsType = if (prop.name == "type" && defaultTypeLiteral != null) {
"\"$defaultTypeLiteral\"" "\"$defaultTypeLiteral\""
} else { } else {
TsTypeMapper.kotlinToTsType(prop.returnType.toString(), typeParams) TsTypeMapper.kotlinToTsType(
prop.returnType.toString(),
typeParams,
mapStrategy
)
} }
" ${prop.name}: $tsType;" " ${prop.name}: $tsType;"
} }
+13 -3
View File
@@ -1,7 +1,11 @@
package no.iktdev.ts package no.iktdev.ts
object TsTypeMapper { object TsTypeMapper {
fun kotlinToTsType(kotlinType: String, genericParams: List<String> = emptyList()): String { fun kotlinToTsType(
kotlinType: String,
genericParams: List<String> = emptyList(),
mapStrategy: MapStrategy = MapStrategy.MapAsRecord,
): String {
val isNullable = kotlinType.endsWith("?") val isNullable = kotlinType.endsWith("?")
// Fjerner eventuelle backticks som Kotlin legger rundt lokale/test-klasser, og trimmet // Fjerner eventuelle backticks som Kotlin legger rundt lokale/test-klasser, og trimmet
val clean = kotlinType.removeSuffix("?").replace("`", "").trim() val clean = kotlinType.removeSuffix("?").replace("`", "").trim()
@@ -26,7 +30,7 @@ object TsTypeMapper {
return if (isNullable) "string | null" else "string" return if (isNullable) "string | null" else "string"
} }
// Map<K, V> → Record<K, V> // Map<K, V> → Record<K, V> / Partial<Record<K, V>>
if (clean.startsWith("kotlin.collections.Map") || if (clean.startsWith("kotlin.collections.Map") ||
clean.startsWith("kotlin.collections.MutableMap")) { clean.startsWith("kotlin.collections.MutableMap")) {
@@ -36,7 +40,13 @@ object TsTypeMapper {
val tsKey = kotlinToTsType(key, genericParams) val tsKey = kotlinToTsType(key, genericParams)
val tsValue = kotlinToTsType(value, genericParams) val tsValue = kotlinToTsType(value, genericParams)
val base = "Record<$tsKey, $tsValue>" val record = "Record<$tsKey, $tsValue>"
val base = when (mapStrategy) {
MapStrategy.MapAsRecord -> record
MapStrategy.MapAsPartialRecord -> "Partial<$record>"
}
return if (isNullable) "$base | null" else base return if (isNullable) "$base | null" else base
} }
@@ -0,0 +1,9 @@
package no.iktdev.ts.anno
import no.iktdev.ts.MapStrategy
@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
annotation class TsMapStrategy(
val value: MapStrategy
)