Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 67 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The **Kotlin Object Multiplatform Mapper** provides you a possibility to generat
* [Disable AutoCast](#disable-autocast)
* [Change Convert Function Name](#change-convert-function-name)
* [@MapName](#mapname-annotation)
* [@MapEmbedded](#mapembedded-annotation)
* [@MapConverter](#use-converter)
* [@MapDefault](#use-resolver)
* [@NullSubstitute](#use-nullsubstitute)
Expand Down Expand Up @@ -83,7 +84,7 @@ plugins {
id("com.google.devtools.ksp") version "2.3.9"
}

val kommVersion = "0.50.9"
val kommVersion = "0.60.0"

depensencies {
implementation("com.ucasoft.komm:komm-annotations:$kommVersion")
Expand All @@ -96,7 +97,7 @@ plugins {
id("com.google.devtools.ksp") version "2.3.9"
}

val kommVersion = "0.50.9"
val kommVersion = "0.60.0"

kotlin {
jvm {
Expand Down Expand Up @@ -264,6 +265,64 @@ fun SourceObject.toDestinationObject(): DestinationObject = DestinationObject(
}
```

### @MapEmbedded annotation
Use `@MapEmbedded` when several destination properties should be mapped from the same nested source property.
KOMM checks only the first nested level. Direct source properties have priority over embedded properties.
If two embedded properties can provide the same destination property, generation fails and the mapping should be made explicit.

#### Classes declaration
```kotlin
data class Account(
val id: Long,
val name: String
)

data class AccountWithCurrencies(
val account: Account,
val currencies: List<AccountCurrency>
)

@KOMMMap(from = [AccountWithCurrencies::class])
@MapEmbedded("account")
data class AccountDto(
val name: String,
val currencies: List<AccountCurrencyDto>
) {
var id: Long = 0L
}
```
#### Generated extension function
```kotlin
fun AccountWithCurrencies.toAccountDto(): AccountDto = AccountDto(
name = account.name,
currencies = currencies.map { it.toAccountCurrencyDto() }
).also {
it.id = account.id
}
```
#### Nullable embedded source
```kotlin
data class AccountWithCurrencies(
val account: Account?,
val currencies: List<AccountCurrency>
)

@KOMMMap(from = [AccountWithCurrencies::class])
@MapEmbedded("account")
data class AccountDto(
@NullSubstitute(MapDefault(StringResolver::class))
val name: String,
val currencies: List<AccountCurrencyDto>
)
```
#### Generated extension function
```kotlin
fun AccountWithCurrencies.toAccountDto(): AccountDto = AccountDto(
name = account?.name ?: StringResolver(null).resolve(),
currencies = currencies.map { it.toAccountCurrencyDto() }
)
```

### Use Converter
#### Converter declaration
```kotlin
Expand Down Expand Up @@ -469,7 +528,7 @@ plugins {
id("com.google.devtools.ksp") version "2.3.9"
}

val kommVersion = "0.50.9"
val kommVersion = "0.60.0"

depensencies {
implementation("com.ucasoft.komm:komm-annotations:$kommVersion")
Expand All @@ -483,7 +542,7 @@ plugins {
id("com.google.devtools.ksp") version "2.3.9"
}

val kommVersion = "0.50.9"
val kommVersion = "0.60.0"

//...

Expand Down Expand Up @@ -543,7 +602,7 @@ plugins {
id("com.google.devtools.ksp") version "2.3.9"
}

val kommVersion = "0.50.9"
val kommVersion = "0.60.0"

depensencies {
implementation("com.ucasoft.komm:komm-annotations:$kommVersion")
Expand Down Expand Up @@ -587,7 +646,7 @@ plugins {
id("com.google.devtools.ksp") version "2.3.9"
}

val kommVersion = "0.50.9"
val kommVersion = "0.60.0"

depensencies {
implementation("com.ucasoft.komm:komm-annotations:$kommVersion")
Expand All @@ -601,7 +660,7 @@ plugins {
id("com.google.devtools.ksp") version "2.3.9"
}

val kommVersion = "0.50.9"
val kommVersion = "0.60.0"

//...

Expand Down Expand Up @@ -701,4 +760,4 @@ fun SourceObject.toDestinationObject(): toDestinationObject = toDestinationObjec
(DestinationObject.DestinationEnum.entries.any { it.name == direction.name }) direction.name else "OTHER")
else null) ?: DirectionResolver(null).resolve()
)
```
```
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tasks.wrapper {
allprojects {
group = "com.ucasoft.komm"

version = "0.50.9"
version = "0.60.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ucasoft.komm.annotations

import kotlin.reflect.KClass

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
@Repeatable
annotation class MapEmbedded(val name: String, val `for`: Array<KClass<*>> = [])
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ class KOMMAnnotationFinder(private val forClass: KSType) {
fun getSuitedNamedAnnotation(member: KSPropertyDeclaration) =
filterAnnotationsByClass(forClass.toClassName(), getSuitedNamedAnnotationsForClass(member), member)

fun getSuitedEmbeddedAnnotations(classDeclaration: KSClassDeclaration): List<KSAnnotation> =
classDeclaration.annotations
.filter { it.shortName.asString() == MapEmbedded::class.simpleName }
.associateWith(::associateWithFor)
.filter { it.value.isEmpty() || it.value.contains(forClass.toClassName()) }
.keys
.toList()

private fun getSuitedNamedAnnotationsForClass(member: KSPropertyDeclaration) =
member.annotations.filter { it.shortName.asString() in namedAnnotations }
.associateWith(::associateWithFor)
Expand Down
Loading