mapkt

Annotation-driven compile-time code generation of type-safe, bidirectional conversion functions between similar data classes, producing zero-overhead mapping methods like .toOtherType() via an annotation processor.

JVMKotlin/Native
GitHub stars4
Dependents0
LicenseApache License 2.0
Creation dateover 1 year ago

Last activity4 days ago
Latest release1.0.0 (4 months ago)

MapKt

A Kotlin Multiplatform library that automatically generates type-safe mapping/conversion code between similar data classes using KSP (Kotlin Symbol Processing).

This started as a fork of KConMapper but evolved into its own library, including multiplatform support.

What is MapKt?

MapKt enables you to define simple @MapKt annotations on source data classes and automatically generates conversion functions like .toOtherType() without writing manual mapTo boilerplate code.

Key Features

  • Automatic Mapping Generation - KSP processor generates all mapping functions at compile-time
  • Kotlin Multiplatform - Works across JVM, Android, iOS, and desktop platforms
  • Type-Safe Mappings - Compile-time checked conversions between similar types
  • Zero Runtime Overhead - Generated code is pure Kotlin with no reflection

Quick Start

1. Add Dependencies

Gradle (Kotlin):

// build.gradle.kts
plugins {
    id("com.google.devtools.ksp") version "2.3.6"
}

dependencies {
    val mapktVersion = "1.1.0" // or use latest
    implementation("com.jakala.mapkt:mapkt-ksp:$mapktVersion")
    ksp("com.jakala.mapkt:mapkt-ksp:$mapktVersion")
}

2. Define Your Data Classes

Define two or more similar data classes with a shared mapping annotation:

@MapKt(
    mapTo = [RemoteModel::class], // Map FROM Dog TO Cat
)
data class SharedModel(val name: String, val age: Int)

@MapKt(
    mapTo = [SharedModel::class], // Map FROM Cat TO Dog  
)
data class RemoteModel(val name: String, val age: Int)

3. Generate and Use Mapping Functions

val shared = SharedModel("Fido", 5)
val remote = shared.toRemote()    
println(remote.name)                  // Output: Fido
println(remote.age)                   // Output: 5

// Works in reverse!
val convertedDog = remote.toShared() 

Annotations Reference

@MapKt - Map Generation Annotation

Define mappings on data classes that should generate conversion functions.

/**
 * Generates mapping functions from this class to the specified types.
 */
annotation class MapKt(
    val mapTo: Array<KClass<*>> // Types this class maps TO
)

Project Structure

mapkt-annotations/     # Annotation definitions (no dependencies)
mapkt-ksp/             # KSP processor that generates mapping code
sample/                # Sample project demonstrating usage

Development & Building

Look at the Provided Justfile, here, it contains tasks for building, testing, and publishing the library. You don't need to use Just, you can run the equivalent Gradle commands directly.

Publishing Locally

# Build and publish artifacts to your local Maven repo
./gradlew clean publishToMavenLocal

# Verify installation by building sample again
./gradlew :sample:build

Releasing a new version

Releases are published automatically to Maven Central via GitHub Actions.

  1. Update the version in the root build.gradle.kts.
  2. Commit the change with the format: release version x.y.z
  3. Create a git tag git tag v<version> and git push origin v<version>
JVMKotlin/Native
GitHub stars4
Dependents0
LicenseApache License 2.0
Creation dateover 1 year ago

Last activity4 days ago
Latest release1.0.0 (4 months ago)

MapKt

A Kotlin Multiplatform library that automatically generates type-safe mapping/conversion code between similar data classes using KSP (Kotlin Symbol Processing).

This started as a fork of KConMapper but evolved into its own library, including multiplatform support.

What is MapKt?

MapKt enables you to define simple @MapKt annotations on source data classes and automatically generates conversion functions like .toOtherType() without writing manual mapTo boilerplate code.

Key Features

  • Automatic Mapping Generation - KSP processor generates all mapping functions at compile-time
  • Kotlin Multiplatform - Works across JVM, Android, iOS, and desktop platforms
  • Type-Safe Mappings - Compile-time checked conversions between similar types
  • Zero Runtime Overhead - Generated code is pure Kotlin with no reflection

Quick Start

1. Add Dependencies

Gradle (Kotlin):

// build.gradle.kts
plugins {
    id("com.google.devtools.ksp") version "2.3.6"
}

dependencies {
    val mapktVersion = "1.1.0" // or use latest
    implementation("com.jakala.mapkt:mapkt-ksp:$mapktVersion")
    ksp("com.jakala.mapkt:mapkt-ksp:$mapktVersion")
}

2. Define Your Data Classes

Define two or more similar data classes with a shared mapping annotation:

@MapKt(
    mapTo = [RemoteModel::class], // Map FROM Dog TO Cat
)
data class SharedModel(val name: String, val age: Int)

@MapKt(
    mapTo = [SharedModel::class], // Map FROM Cat TO Dog  
)
data class RemoteModel(val name: String, val age: Int)

3. Generate and Use Mapping Functions

val shared = SharedModel("Fido", 5)
val remote = shared.toRemote()    
println(remote.name)                  // Output: Fido
println(remote.age)                   // Output: 5

// Works in reverse!
val convertedDog = remote.toShared() 

Annotations Reference

@MapKt - Map Generation Annotation

Define mappings on data classes that should generate conversion functions.

/**
 * Generates mapping functions from this class to the specified types.
 */
annotation class MapKt(
    val mapTo: Array<KClass<*>> // Types this class maps TO
)

Project Structure

mapkt-annotations/     # Annotation definitions (no dependencies)
mapkt-ksp/             # KSP processor that generates mapping code
sample/                # Sample project demonstrating usage

Development & Building

Look at the Provided Justfile, here, it contains tasks for building, testing, and publishing the library. You don't need to use Just, you can run the equivalent Gradle commands directly.

Publishing Locally

# Build and publish artifacts to your local Maven repo
./gradlew clean publishToMavenLocal

# Verify installation by building sample again
./gradlew :sample:build

Releasing a new version

Releases are published automatically to Maven Central via GitHub Actions.

  1. Update the version in the root build.gradle.kts.
  2. Commit the change with the format: release version x.y.z
  3. Create a git tag git tag v<version> and git push origin v<version>