Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit bd2a360

Browse files
authored
Merge pull request #14 from k163377/feature
Add annotation that means "Use Non Null argument".
2 parents a0850b1 + 4cd8887 commit bd2a360

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ val dst: Dst = KMapper(::DataClass) { camelToSnake(it) }.map(src)
7676
### Map param to another class
7777

7878
```kotlin
79-
class CreatorClass @SingleArgCreator constructor(val arg: String) {
79+
class ConverterClass @KConverter constructor(val arg: String) {
8080
companion object {
8181
@KConverter
82-
fun fromInt(arg: Int): CreatorClass {
83-
return CreatorClass(arg.toString)
82+
fun fromInt(arg: Int): ConverterClass {
83+
return ConverterClass(arg.toString)
8484
}
8585
}
8686
}
8787

8888
class Src(val arg1: String, val arg2: Int)
8989

90-
class Dst(val arg1: CreatorClass, val arg2: CreatorClass)
90+
class Dst(val arg1: ConverterClass, val arg2: ConverterClass)
9191

9292
val newInstance = KMapper(::Dst).map(src)
9393
```

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "com.mapk"
9-
version = "0.16"
9+
version = "0.17"
1010

1111
java {
1212
sourceCompatibility = JavaVersion.VERSION_1_8
@@ -30,7 +30,7 @@ repositories {
3030
dependencies {
3131
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
3232
implementation(kotlin("reflect"))
33-
api("com.github.ProjectMapK:Shared:0.7")
33+
api("com.github.ProjectMapK:Shared:0.8")
3434

3535
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
3636
testImplementation(group = "org.junit.jupiter", name = "junit-jupiter", version = "5.6.0") {

src/main/kotlin/com/mapk/kmapper/KMapper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ private fun <T : Any, R : Any> mapObject(param: ParameterForMap<R>, value: T): A
117117
// パラメータに対してvalueが代入可能(同じもしくは親クラス)であればそのまま用いる
118118
if (param.clazz.isSuperclassOf(valueClazz)) return value
119119

120-
val creator: KFunction<*>? = param.getCreator(valueClazz)
120+
val converter: KFunction<*>? = param.getConverter(valueClazz)
121121

122122
return when {
123-
// creatorに一致する組み合わせが有れば設定されていればそれを使う
124-
creator != null -> creator.call(value)
123+
// converterに一致する組み合わせが有れば設定されていればそれを使う
124+
converter != null -> converter.call(value)
125125
// 要求された値がenumかつ元が文字列ならenum mapperでマップ
126126
param.javaClazz.isEnum && value is String -> EnumMapper.getEnum(param.clazz.java, value)
127127
// 要求されているパラメータがStringならtoStringする

src/main/kotlin/com/mapk/kmapper/ParameterForMap.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ internal class ParameterForMap<T : Any> private constructor(val param: KParamete
1616
clazz.java
1717
}
1818
// リストの長さが小さいと期待されるためこの形で実装しているが、理想的にはmap的なものが使いたい
19-
private val creators: Set<Pair<KClass<*>, KFunction<T>>> by lazy {
20-
creatorsFromConstructors(clazz) + creatorsFromStaticMethods(
19+
private val converters: Set<Pair<KClass<*>, KFunction<T>>> by lazy {
20+
convertersFromConstructors(clazz) + convertersFromStaticMethods(
2121
clazz
22-
) + creatorsFromCompanionObject(clazz)
22+
) + convertersFromCompanionObject(clazz)
2323
}
2424

25-
// 引数の型がcreatorに対して入力可能ならcreatorを返す
26-
fun <R : Any> getCreator(input: KClass<out R>): KFunction<T>? =
27-
creators.find { (key, _) -> input.isSubclassOf(key) }?.second
25+
// 引数の型がconverterに対して入力可能ならconverterを返す
26+
fun <R : Any> getConverter(input: KClass<out R>): KFunction<T>? =
27+
converters.find { (key, _) -> input.isSubclassOf(key) }?.second
2828

2929
companion object {
3030
fun newInstance(param: KParameter): ParameterForMap<*> {
@@ -42,19 +42,19 @@ private fun <T> Collection<KFunction<T>>.getConverterMapFromFunctions(): Set<Pai
4242
}.toSet()
4343
}
4444

45-
private fun <T : Any> creatorsFromConstructors(clazz: KClass<T>): Set<Pair<KClass<*>, KFunction<T>>> {
45+
private fun <T : Any> convertersFromConstructors(clazz: KClass<T>): Set<Pair<KClass<*>, KFunction<T>>> {
4646
return clazz.constructors.getConverterMapFromFunctions()
4747
}
4848

4949
@Suppress("UNCHECKED_CAST")
50-
private fun <T : Any> creatorsFromStaticMethods(clazz: KClass<T>): Set<Pair<KClass<*>, KFunction<T>>> {
50+
private fun <T : Any> convertersFromStaticMethods(clazz: KClass<T>): Set<Pair<KClass<*>, KFunction<T>>> {
5151
val staticFunctions: Collection<KFunction<T>> = clazz.staticFunctions as Collection<KFunction<T>>
5252

5353
return staticFunctions.getConverterMapFromFunctions()
5454
}
5555

5656
@Suppress("UNCHECKED_CAST")
57-
private fun <T : Any> creatorsFromCompanionObject(clazz: KClass<T>): Set<Pair<KClass<*>, KFunction<T>>> {
57+
private fun <T : Any> convertersFromCompanionObject(clazz: KClass<T>): Set<Pair<KClass<*>, KFunction<T>>> {
5858
return clazz.companionObjectInstance?.let { companionObject ->
5959
companionObject::class.functions
6060
.filter { it.annotations.any { annotation -> annotation is KConverter } }

0 commit comments

Comments
 (0)