{"id":27770755,"url":"https://github.com/mfarsikov/kewt","last_synced_at":"2025-10-06T14:59:29.485Z","repository":{"id":138872556,"uuid":"260893504","full_name":"mfarsikov/kewt","owner":"mfarsikov","description":"Kewt is a Kotlin tool for data class mapping","archived":false,"fork":false,"pushed_at":"2020-10-19T20:14:25.000Z","size":190,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-29T21:16:04.246Z","etag":null,"topics":["kapt","kotlin","mapstruct"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mfarsikov.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-05-03T11:18:33.000Z","updated_at":"2024-03-21T08:30:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"5906d4f9-b800-45bc-88f6-8001909ecb54","html_url":"https://github.com/mfarsikov/kewt","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/mfarsikov/kewt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfarsikov%2Fkewt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfarsikov%2Fkewt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfarsikov%2Fkewt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfarsikov%2Fkewt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mfarsikov","download_url":"https://codeload.github.com/mfarsikov/kewt/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfarsikov%2Fkewt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278629052,"owners_count":26018482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kapt","kotlin","mapstruct"],"created_at":"2025-04-29T21:16:02.802Z","updated_at":"2025-10-06T14:59:29.479Z","avatar_url":"https://github.com/mfarsikov.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Bintray](https://img.shields.io/bintray/v/farsikov-max/kewt-map/kewt-map-processor?style=flat-square\u0026color=blue)\n![Tests](https://github.com/mfarsikov/kewt/workflows/master/badge.svg)\n\u003cimg src=\"misc/kotlin-logo.svg\" alt=\"drawing\" width=\"20\"/\u003e\n\n# Kewt - Kotlin cute data-class mapping tool\n\ninspired by [Mapstruct](https://mapstruct.org/)\n\n\n\n## Quick start\n\n`build.gradle.kts`:\n```kotlin\nplugins {\n    kotlin(\"kapt\") version \"1.3.72\" //kotlinVersion\n}\nrepositories {\n    jcenter()\n}\ndependencies {\n    implementation(\"com.github.mfarsikov:kewt-annotations:0.7.0\")\n    kapt(\"com.github.mfarsikov:kewt-map-processor:0.7.0\")\n}\n```\n\nHaving data classes:\n```kotlin\ndata class Person(val id: UUID, val name: String, val age: Int)\ndata class Employee(val id: UUID, val name: String, val age: Int)\n```\n\nCreate interface \n```kotlin\nimport com.github.mfarsikov.kewt.annotations.Mapper\n\n@Mapper\ninterface PersonMapper {\n    fun toEmployee(person: Person): Employee\n}\n```\nKewt generates this code:\n```kotlin\n@Generated\nclass PersonMapperImpl : PersonMapper {\n  override fun toEmployee(person: Person) = Employee(\n      age = person.age,\n      id = person.id,\n      name = person.name\n  )\n}\n```\n\n## Mapping strategies\n\n### General concepts\n* Classes can be mapped if each target field have found a single source.\n* Mapping cannot be done if any target property can be mapped from more than one source. \n* In the same time it is allowed to have not mapped (extra) sources.\n\n### Implicitly by type, if it is not ambiguous\nType matched if:\n* Types are identical:\n  * `String =\u003e String`\n  * `Employee =\u003e Employee`\n  * `List\u003cString\u003e =\u003e List\u003cString\u003e`\n* Exist conversion function (abstract or not): \n  * `String =\u003e Int` if `(String) -\u003e Int` conversion function provided\n  * `Person =\u003e Employee` if `(Person) -\u003e Employee` conversion function provided\n  * `List\u003cString\u003e =\u003e List\u003cInt\u003e` if `(String) -\u003e Int` conversion function provided\n  * `List\u003cPerson\u003e =\u003e List\u003cEmployee\u003e` if `(Person) -\u003e Employee` conversion function provided\n#### Example\n\n##### Type match\n\n```kotlin\ndata class Person  (val personId: UUID,   val personName: String,   val personAge: Int  )\ndata class Employee(val employeeId: UUID, val employeeName: String, val employeeAge: Int)\n```\nThere is only one way to map properties by their types.\n\nGenerated code:\n```kotlin\n@Generated\nclass PersonMapperImpl : PersonMapper {\n    override fun toEmployee(person: Person) = Employee(\n        employeeId = person.personId,\n        employeeName = person.personName,\n        employeeAge = person.personAge\n    )\n}\n```\n\n##### Negative example (ambiguous mapping):\n```kotlin\ndata class Person  (val key: UUID, userId: UUID)\ndata class Employee(val id: UUID,  cardId: UUID)\n```\n\nThese classes cannot be mapped implicitly, because there is ambiguity in fields mapping, all four variants are valid:\n`key = id`, `key = cardId`, `userId = id`, `userId = cardId`\n\nTo solve this see [Explicit mapping](#explicit-mapping)\n\n##### Type match using a converter function\n\n```kotlin\ndata class Person  (val personId: String)\ndata class Employee(val employeeId: UUID)\n```\nNote that names are not matched and types are different.\n```kotlin\n@Mapper\ninterface PersonMapper{\n\n    fun toEmployee(person: Person): Employee \n    \n    fun toUuid(s: String) = UUID.fromString(s) // Non-abstract converter (String) -\u003e UUID\n}\n```\nKewt maps fields by type using conversion function:\n```kotlin\n@Generated\nclass PersonMapperImpl : PersonMapper {\n    override fun toEmployee(person: Person) = Employee(\n        employeeId = toUuid(person.personId)\n    )\n}\n```\n\n### Implicitly by name, if it is not ambiguous\nIf source property name and target property names are the same - fields match. This strategy could fail if provided more\nthan one source with the same parameter name, and the same parameter type.\n#### Example \n```kotlin\ndata class Person  (val id: UUID, userId: UUID)\ndata class Employee(val id: UUID, userId: UUID)\n```\n\n### Explicitly in `@Mapping` annotation\nHas highest priority. Should be avoided in favor of implicit strategies.\n#### Example\n##### Explicit mapping\n```kotlin\ndata class Person  (val key: UUID, userId: UUID)\ndata class Employee(val id: UUID,  cardId: UUID)\n```\n\n```kotlin\ninterface PersonMapper {\n    @Mappings([\n        Mapping(source=\"key\",    target=\"id\"    ),\n        Mapping(source=\"userId\", target=\"cardId\")\n    ])  \n    fun toEmployee(person: Person): Employee\n}\n```\n\nIt is not required to specify all the fields, it is enough just to resolve ambiguity, the rest kewt maps automatically: \n```kotlin\ninterface PersonMapper {\n    @Mappings([\n        Mapping(source=\"key\", target=\"id\")\n    ])  \n    fun toEmployee(person: Person): Employee\n}\n```\n\n##### Explicit converter\n\n```kotlin\ndata class Person  (val id: String)\ndata class Employee(val id: UUID  )\n```\nThere are two functions `(String) -\u003e UUID` and to solve this ambiguity `converter` explicitly:\n```kotlin\n\ninterface PersonMapper {\n\n    fun parseUuid(string: String): UUID = UUID.fromString(string) \n    fun invalidConverter(string: String): UUID = throw RuntimeException()\n\n    @Mappings([\n        Mapping(source=\"id\", target=\"id\", converter=\"parseUuid\")\n    ])  \n    fun toEmployee(person: Person): Employee\n}\n```\n\n### Examples\nWorking examples can be found in `examples` sub-project. To build them run `./gradlew build`\nGenerated code is under `build/generated/source/kapt/main`.\n\n## Field lifting\n```kotlin\ndata class Person(val name: Name)\ndata class Name(val firstName: String)\n\ndata class Employee(val name: String)\n\n@Mapper\ninterface PersonMapper {\n    @Mappings([\n        Mapping(source = \"person.name.firstName\", target = \"name\")\n    ])\n    fun toEmployee(person: Person): Employee\n}\n```\nGenerated code:\n```kotlin\n@Generated\nclass PersonMapperImpl : PersonMapper {\n  override fun toEmployee(person: Person) = Employee(\n      name = person.name.firstName\n  )\n}\n```\n\n## Map collection elements\n```kotlin\n\ndata class Person(val ids: List\u003cUUID\u003e)\ndata class Employee(val ids: List\u003cString\u003e)\n\n@Mapper\ninterface PersonMapper {\n    fun toEmployee(person: Person): Employee\n    fun uuidToString(uuid: UUID): String = uuid.toString()\n}\n```\n\nGenerated code: \n```kotlin\n@Generated\nclass PersonMapperImpl : PersonMapper {\n  override fun toEmployee(person: Person): Employee = Employee(\n      ids = person.ids.map { uuidToString(it) }\n  )\n}\n```\n\n## Protobuf\nKewt can map protobuf classes using their builders.\n\nAdd library with generated protobuf classes to `kapt` configurtion.\n\n## Dependencies\nAll libraries used in mappers should be added to `kapt` configuration:\n```kotlin \ndependencies {\n    kapt(\"com.nowhere:my-library:1.0.0\")\n}\n```\n\n## Configuration\n```kotlin\nkapt {\n    arguments {          \n        arg(\"kewt.generate.spring\", \"true\")\n        arg(\"kewt.log.level\", \"debug\")\n        arg(\"kewt.whitelist\", \"com.github.mfarsikov.kewt.example.proto.ex02\")\n        arg(\"kewt.blacklist\", \"com.github.mfarsikov.kewt.example.proto\")\n    }\n}           \n```\n\n### Spring\n```kotlin\narg(\"kewt.generate.spring\", \"true\") \n```\nAdd a `@org.springframework.stereotype.Component` to the generated class.\n\n### Debugging\n#### Logging\n```kotlin\narg(\"kewt.log.level\", \"debug\")\n```\nValues: `error`, `warn`, `debug`, `trace`. Default: `warn`\n\n#### White/black listing packages\n```kotlin\narg(\"kewt.whitelist\", \"com.github.mfarsikov.kewt.example.proto.ex02\")\narg(\"kewt.blacklist\", \"com.github.mfarsikov.kewt.example.proto\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmfarsikov%2Fkewt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmfarsikov%2Fkewt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmfarsikov%2Fkewt/lists"}