Skip to content

Commit 9d2940a

Browse files
committed
feat: all tests passed
1 parent df2ea2f commit 9d2940a

5 files changed

Lines changed: 48 additions & 108 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.gradle
22
.idea
33
build
4+
out

core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ dependencies {
88
implementation(kotlin("stdlib"))
99

1010
kaptTest(project(":processor"))
11-
testImplementation(project(":gsonkeepdefault-processor"))
11+
testImplementation(project(":processor"))
1212
testImplementation("junit", "junit", "4.12")
1313
}

core/src/test/kotlin/me/mozidev/keepdefault/KeepDefaultTest.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package me.mozidev.keepdefault
22

3+
import com.google.gson.stream.JsonReader
34
import org.intellij.lang.annotations.Language
45
import org.junit.Test
6+
import java.io.BufferedReader
7+
import java.io.StringReader
58

69
/**
710
* created by zijing on 2019/1/21
@@ -87,4 +90,34 @@ class KeepDefaultProcessorTest {
8790
val al = KGson.fromJson<List<AValue>>(json)
8891
assert(al == listOf(AValue(), AValue()))
8992
}
93+
94+
@Test
95+
fun testJsonReader() {
96+
@Language("JSON")
97+
val json = """{"a":"aaa"}"""
98+
99+
val jsonReader = JsonReader(StringReader(json))
100+
val a1 = KGson.fromJson<AValue>(jsonReader, AValue::class.java)
101+
assert(a1.a == "aaa")
102+
}
103+
104+
@Test
105+
fun testBufferReader() {
106+
@Language("JSON")
107+
val json = """{"a":"aaa"}"""
108+
109+
val bufferReader = BufferedReader(StringReader(json))
110+
val a = KGson.fromJson<AValue>(bufferReader)
111+
assert(a.a == "aaa")
112+
}
113+
114+
@Test
115+
fun testToJson() {
116+
@Language("JSON")
117+
val targetJson = """{"a":"aaa"}"""
118+
119+
val a = AValue("aaa")
120+
val json = KGson.toJson(a)
121+
assert(json == targetJson)
122+
}
90123
}

processor/src/main/kotlin/me/mozidev/keepdefault/KeepDefaultProcessor.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
package me.mozidev.keepdefault
22

33
import com.google.auto.service.AutoService
4-
import com.squareup.kotlinpoet.ClassName
5-
import com.squareup.kotlinpoet.FileSpec
6-
import com.squareup.kotlinpoet.FunSpec
7-
import com.squareup.kotlinpoet.KModifier
8-
import com.squareup.kotlinpoet.ParameterSpec
4+
import com.squareup.kotlinpoet.*
95
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
10-
import com.squareup.kotlinpoet.TypeName
11-
import com.squareup.kotlinpoet.TypeSpec
12-
import com.squareup.kotlinpoet.asClassName
13-
import com.squareup.kotlinpoet.asTypeName
14-
import me.eugeniomarletti.kotlin.metadata.KotlinClassMetadata
15-
import me.eugeniomarletti.kotlin.metadata.KotlinMetadataUtils
16-
import me.eugeniomarletti.kotlin.metadata.declaresDefaultValue
17-
import me.eugeniomarletti.kotlin.metadata.isDataClass
18-
import me.eugeniomarletti.kotlin.metadata.isPrimary
19-
import me.eugeniomarletti.kotlin.metadata.kotlinMetadata
6+
import me.eugeniomarletti.kotlin.metadata.*
207
import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf
218
import me.eugeniomarletti.kotlin.metadata.shadow.metadata.deserialization.NameResolver
229
import me.eugeniomarletti.kotlin.metadata.shadow.serialization.deserialization.getName
@@ -91,7 +78,7 @@ class KeepDefaultProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils {
9178
.addFunction(generateTryKeepDefault(typeName, kotlinClassMetadata))
9279
.build())
9380

94-
result.build().writeTo(File(fileDir, helperClassName))
81+
result.build().writeTo(File(fileDir, helperClassName).also { if (!it.exists()) it.mkdirs() })
9582

9683
return helperClassName
9784
}
@@ -100,16 +87,20 @@ class KeepDefaultProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils {
10087

10188
val (nameAllocator, classProto) = metadata.data
10289

90+
// the Gson-parsed data object
10391
val originParam = ParameterSpec
10492
.builder("originData", typeName.asNullable())
10593
.build()
10694

95+
// retrieve all the fields from DataClass' constructor
10796
val propList = classProto.constructorList.single { it.isPrimary }.valueParameterList
10897

10998
val noDefaultMarked = propList.count { it.declaresDefaultValue } == 0
11099
if (showErrorIfNoDefault && noDefaultMarked) {
111100
messager.printMessage(Diagnostic.Kind.ERROR, "There is no default value for any property")
112101
}
102+
103+
// if non field is marked with "default", return the Gson-pared data object
113104
val statements = if (noDefaultMarked) {
114105
generateNoDefaultSpecified()
115106
} else {
@@ -131,6 +122,12 @@ class KeepDefaultProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils {
131122
return listOf("return originData")
132123
}
133124

125+
/**
126+
* do the real work,
127+
* 1. check all the fields set with "default" modifier
128+
* 2. check if it is "nullable"
129+
* 3. override the null value of "NonNull-Default" field
130+
*/
134131
private fun generateCheckDefault(
135132
typeName: TypeName,
136133
nameAllocator: NameResolver,

processor/src/test/kotlin/KeepDefaultTest.kt

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)