Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.
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
2 changes: 2 additions & 0 deletions .idea/KMapper.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![CircleCI](https://circleci.com/gh/k163377/MapK.svg?style=svg)](https://circleci.com/gh/k163377/MapK)
[![](https://jitci.com/gh/k163377/MapK/svg)](https://jitci.com/gh/k163377/MapK)
[![CircleCI](https://circleci.com/gh/ProjectMapK/KMapper.svg?style=svg)](https://circleci.com/gh/ProjectMapK/KMapper)
[![](https://jitci.com/gh/ProjectMapK/KMapper/svg)](https://jitci.com/gh/ProjectMapK/KMapper)

MapK
KMapper
====
This is a Mapper Libraly like a `ModelMapper` for `Kotlin`.
You can call `KFunction`(e.g. `constructor`) from `Object`.
Expand All @@ -19,11 +19,11 @@ val dst = Dst(
)

// after
val newInstance = KMapper(Dst::class.primaryConstructor!!).map(src)
val newInstance = KMapper(::Dst).map(src)
```
## How to use
Published on JitPack.
Please see [here](https://jitpack.io/#k163377/MapK) for the introduction method.
Please see [here](https://jitpack.io/#ProjectMapK/KMapper/) for the introduction method.

## Usages
### From multiple resources
Expand Down
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ plugins {
id("org.jlleitschuh.gradle.ktlint") version "9.2.1"
}

group = "com.wrongwrong"
version = "0.1"
group = "com.mapk"
version = "0.10"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -24,11 +24,13 @@ buildscript {

repositories {
mavenCentral()
maven { setUrl("https://jitpack.io") }
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation(kotlin("reflect"))
implementation("com.github.ProjectMapK:Shared:0.1")

// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
testImplementation(group = "org.junit.jupiter", name = "junit-jupiter", version = "5.6.0") {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = "MapK"
rootProject.name = "KMapper"
18 changes: 0 additions & 18 deletions src/main/java/com/wrongwrong/mapk/core/EnumMapper.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wrongwrong.mapk.annotations
package com.mapk.annotations

@Target(AnnotationTarget.CONSTRUCTOR, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wrongwrong.mapk.annotations
package com.mapk.annotations

@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wrongwrong.mapk.annotations
package com.mapk.annotations

@Target(AnnotationTarget.PROPERTY)
@Retention(AnnotationRetention.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.wrongwrong.mapk.core

import com.wrongwrong.mapk.annotations.KConstructor
import com.wrongwrong.mapk.annotations.KGetterAlias
import com.wrongwrong.mapk.annotations.KPropertyAlias
import com.wrongwrong.mapk.annotations.KPropertyIgnore
package com.mapk.kmapper

import com.mapk.annotations.KConstructor
import com.mapk.annotations.KGetterAlias
import com.mapk.annotations.KPropertyAlias
import com.mapk.annotations.KPropertyIgnore
import com.mapk.core.ArgumentBucket
import com.mapk.core.EnumMapper
import com.mapk.core.KFunctionForCall
import java.lang.reflect.Method
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
Expand Down Expand Up @@ -58,7 +61,12 @@ class KMapper<T : Any> private constructor(
parameterMap[property.findAnnotation<KGetterAlias>()?.value ?: property.name]?.let {
// javaGetterを呼び出す方が高速
javaGetter.isAccessible = true
argumentBucket.setArgument(javaGetter.invoke(src)?.let { value -> mapObject(it, value) }, it.index)
argumentBucket.setArgument(javaGetter.invoke(src)?.let { value ->
mapObject(
it,
value
)
}, it.index)
// 終了判定
if (argumentBucket.isInitialized) return
}
Expand All @@ -79,7 +87,12 @@ class KMapper<T : Any> private constructor(

private fun bindArguments(argumentBucket: ArgumentBucket, srcPair: Pair<*, *>) {
parameterMap[srcPair.first.toString()]?.let {
argumentBucket.setArgument(srcPair.second?.let { value -> mapObject(it, value) }, it.index)
argumentBucket.setArgument(srcPair.second?.let { value ->
mapObject(
it,
value
)
}, it.index)
}
}

Expand Down Expand Up @@ -133,7 +146,11 @@ internal fun <T : Any> getTarget(clazz: KClass<T>): KFunctionForCall<T> {
clazz.companionObjectInstance?.let { companionObject ->
companionObject::class.functions
.filter { it.annotations.any { annotation -> annotation is KConstructor } }
.map { KFunctionForCall(it, companionObject) as KFunctionForCall<T> }
.map { KFunctionForCall(
it,
companionObject
) as KFunctionForCall<T>
}
} ?: emptyList()

val constructors: List<KFunctionForCall<T>> = factoryConstructor + clazz.constructors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wrongwrong.mapk.core
package com.mapk.kmapper

import com.wrongwrong.mapk.annotations.KConverter
import com.mapk.annotations.KConverter
import com.mapk.core.KFunctionWithInstance
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
Expand All @@ -16,7 +17,9 @@ internal class ParameterForMap<T : Any> private constructor(val index: Int, val
}
// リストの長さが小さいと期待されるためこの形で実装しているが、理想的にはmap的なものが使いたい
private val creators: Set<Pair<KClass<*>, KFunction<T>>> by lazy {
creatorsFromConstructors(clazz) + creatorsFromStaticMethods(clazz) + creatorsFromCompanionObject(clazz)
creatorsFromConstructors(clazz) + creatorsFromStaticMethods(
clazz
) + creatorsFromCompanionObject(clazz)
}

// 引数の型がcreatorに対して入力可能ならcreatorを返す
Expand Down Expand Up @@ -56,7 +59,10 @@ private fun <T : Any> creatorsFromCompanionObject(clazz: KClass<T>): Set<Pair<KC
companionObject::class.functions
.filter { it.annotations.any { annotation -> annotation is KConverter } }
.map { function ->
val func: KFunction<T> = KFunctionWithInstance(function, companionObject) as KFunction<T>
val func: KFunction<T> = KFunctionWithInstance(
function,
companionObject
) as KFunction<T>

(func.parameters.single().type.classifier as KClass<*>) to func
}.toSet()
Expand Down
6 changes: 0 additions & 6 deletions src/main/kotlin/com/wrongwrong/mapk/annotations/KConverter.kt

This file was deleted.

This file was deleted.

26 changes: 0 additions & 26 deletions src/main/kotlin/com/wrongwrong/mapk/core/ArgumentBucket.kt

This file was deleted.

38 changes: 0 additions & 38 deletions src/main/kotlin/com/wrongwrong/mapk/core/KFunctionForCall.kt

This file was deleted.

26 changes: 0 additions & 26 deletions src/main/kotlin/com/wrongwrong/mapk/core/KFunctionWithInstance.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mapk.core;
package com.mapk.kmapper;

import com.wrongwrong.mapk.annotations.KConverter;
import com.mapk.annotations.KConverter;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
@file:Suppress("unused")

package mapk.core
package com.mapk.kmapper

import com.wrongwrong.mapk.annotations.KConverter
import com.wrongwrong.mapk.core.KMapper
import com.mapk.annotations.KConverter
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.DisplayName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@file:Suppress("unused")

package mapk.core
package com.mapk.kmapper

import com.wrongwrong.mapk.core.KMapper
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.params.ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
@file:Suppress("unused")

package mapk.core
package com.mapk.kmapper

import com.wrongwrong.mapk.annotations.KConstructor
import com.wrongwrong.mapk.core.KFunctionForCall
import com.wrongwrong.mapk.core.getTarget
import com.mapk.annotations.KConstructor
import com.mapk.core.KFunctionForCall
import kotlin.reflect.KFunction
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package mapk.core
package com.mapk.kmapper

import com.wrongwrong.mapk.annotations.KGetterAlias
import com.wrongwrong.mapk.annotations.KPropertyAlias
import com.wrongwrong.mapk.core.KMapper
import com.mapk.annotations.KGetterAlias
import com.mapk.annotations.KPropertyAlias
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mapk.core
package com.mapk.kmapper

import com.google.common.base.CaseFormat
import com.wrongwrong.mapk.core.KMapper
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand All @@ -16,7 +15,12 @@ class PropertyNameConverterTest {
val expected = "snakeCase"
val src = mapOf("camel_case" to expected)

val mapper = KMapper(CamelCaseDst::class) { CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, it) }
val mapper = KMapper(CamelCaseDst::class) {
CaseFormat.LOWER_CAMEL.to(
CaseFormat.LOWER_UNDERSCORE,
it
)
}
val result = mapper.map(src)

assertEquals(expected, result.camelCase)
Expand Down
Loading