Skip to content

Commit 07e3748

Browse files
authored
Merge pull request #100 from wuseal/fix/issue89
Fix issue #89
2 parents 0eed657 + 2eea66e commit 07e3748

File tree

7 files changed

+140
-33
lines changed

7 files changed

+140
-33
lines changed

src/main/kotlin/wu/seal/jsontokotlin/codeelements/KClassName.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ object KClassName : KName(), IKClassName {
2828
/**
2929
* keep " " character
3030
*/
31-
val pattern = "$illegalCharacter".replace(Regex(nameSeparator.toString()), "")
31+
val pattern = illegalCharacter.toMutableList().apply { removeAll(nameSeparator) }.toRegex()
3232

33-
val temp = rawClassName.replace(Regex(pattern), "").let {
33+
val temp = rawClassName.replace(pattern, "").let {
3434

3535
return@let removeStartNumberAndIllegalCharacter(it)
3636

@@ -57,7 +57,7 @@ object KClassName : KName(), IKClassName {
5757

5858
val stringBuilder = StringBuilder()
5959

60-
temp.split(Regex(nameSeparator.toString())).forEach {
60+
temp.split(nameSeparator.toRegex()).forEach {
6161
if (it.isNotBlank()) {
6262
stringBuilder.append(it.substring(0, 1).toUpperCase().plus(it.substring(1)))
6363
}

src/main/kotlin/wu/seal/jsontokotlin/codeelements/KName.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,29 @@ abstract class KName : IKName {
2323

2424

2525
protected val illegalCharacter = listOf<String>(
26-
"\\+", "\\-", "\\*", "/", "%", "=", "&", "|", "!", "\\[", "\\]", "\\{", "\\}", "\\(", "\\)", "\\\\", "\"", "_"
27-
, ",", ".", ":", "\\?", "\\>", "\\<", "@", ";", "'", "\\`", "\\~", "\\$", "^", "#", "\\", "/", " ", "\t", "\n"
26+
"\\+", "\\-", "\\*", "/", "%", "=", "&", "\\|", "!", "\\[", "\\]", "\\{", "\\}", "\\(", "\\)", "\\\\", "\"", "_"
27+
, ",", "\\.", ":", "\\?", "\\>", "\\<", "@", ";", "'", "\\`", "\\~", "\\$", "\\^", "#", "\\", "/", " ", "\t", "\n"
2828
)
2929

3030

31-
protected val nameSeparator = listOf<String>(" ", "_", "-")
31+
protected val nameSeparator = listOf<String>(" ", "_", "\\-", ":")
3232

3333

3434
/**
3535
* remove the start number or whiteSpace characters in this string
3636
*/
3737
protected fun removeStartNumberAndIllegalCharacter(it: String): String {
3838

39-
return if (it.replace(Regex(illegalCharacter.toString()), "").indexOfFirst {
40-
return@indexOfFirst it in '0'..'9'
41-
} == 0) {
39+
val numberAndIllegalCharacters = listOf<String>(*illegalCharacter.toTypedArray(), "\\d")
4240

43-
val numberAndIllegalCharacters = listOf<String>(*illegalCharacter.toTypedArray(), "\\d")
41+
val firstNumberAndIllegalCharactersRegex = "^(${numberAndIllegalCharacters.toRegex()})+".toRegex()
42+
43+
return it.trim().replaceFirst(firstNumberAndIllegalCharactersRegex, "")
4444

45-
it.trim().replaceFirst(Regex("${numberAndIllegalCharacters.toString().trim()}{1,}"), "")
46-
} else {
47-
it
48-
}
4945
}
5046

5147
protected fun toBeLegalName(name: String): String {
52-
val tempName = name.replace(illegalCharacter.toString(), "")
48+
val tempName = name.replace(illegalCharacter.toRegex(), "")
5349

5450
val legalName = if (tempName in illegalNameList) {
5551
tempName + suffix
@@ -59,4 +55,10 @@ abstract class KName : IKName {
5955
return legalName
6056
}
6157

58+
/**
59+
* array string into regex match patten that could match any element of the array
60+
*/
61+
protected fun Iterable<String>.toRegex() = joinToString(separator = "|").toRegex()
62+
63+
6264
}

src/main/kotlin/wu/seal/jsontokotlin/codeelements/KPropertyName.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ object KPropertyName : KName(), IPropertyNameMaker {
5454
/**
5555
* keep nameSeparator character
5656
*/
57-
val pattern = "${illegalCharacter}".replace(Regex(nameSeparator.toString()), "")
57+
val pattern = illegalCharacter.toMutableList().apply { removeAll(nameSeparator) }.toRegex()
5858

59-
val temp = rawString.replace(Regex(pattern), "").let {
59+
val temp = rawString.replace(pattern, "").let {
6060

6161
return@let removeStartNumberAndIllegalCharacter(it)
6262

@@ -77,7 +77,7 @@ object KPropertyName : KName(), IPropertyNameMaker {
7777

7878
val stringBuilder = StringBuilder()
7979

80-
temp.split(Regex(nameSeparator.toString())).forEach {
80+
temp.split(nameSeparator.toRegex()).forEach {
8181
if (it.isNotBlank()) {
8282
stringBuilder.append(it.substring(0, 1).toUpperCase().plus(it.substring(1)))
8383
}

src/main/kotlin/wu/seal/jsontokotlin/utils/classblockparse/ClassCodeParser.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,45 +93,45 @@ class ClassCodeParser(private val classBlockString: String) {
9393
}
9494

9595
private fun getPropertyKeyword(propertyLine: String): String {
96-
val stringBeforeColon = propertyLine.substringBefore(":").trim()
96+
val stringBeforeLastColonWithoutComment = propertyLine.substringBefore("//").substringBeforeLast(":").trim()
9797
return when {
98-
stringBeforeColon.contains(")") -> {
99-
val noAnnotationString = stringBeforeColon.substringAfterLast(")").trim()
98+
stringBeforeLastColonWithoutComment.contains(")") -> {
99+
val noAnnotationString = stringBeforeLastColonWithoutComment.substringAfterLast(")").trim()
100100
val keyword = noAnnotationString.split(" ").first()
101101
keyword
102102
}
103-
stringBeforeColon.contains("@") -> {
104-
val keyword = stringBeforeColon.split(" ")[1]
103+
stringBeforeLastColonWithoutComment.contains("@") -> {
104+
val keyword = stringBeforeLastColonWithoutComment.split(" ")[1]
105105
keyword
106106
}
107107
else -> {
108-
val keyword = stringBeforeColon.split(" ").first()
108+
val keyword = stringBeforeLastColonWithoutComment.split(" ").first()
109109
keyword
110110
}
111111
}.trim()
112112
}
113113

114114
private fun getPropertyName(propertyLine: String): String {
115115

116-
val stringBeforeColon = propertyLine.substringBefore(":").trim()
116+
val stringBeforeLastColonWithoutComment = propertyLine.substringBefore("//").substringBeforeLast(":").trim()
117117
return when {
118-
stringBeforeColon.contains(")") -> {
119-
val noAnnotationString = stringBeforeColon.substringAfterLast(")").trim()
118+
stringBeforeLastColonWithoutComment.contains(")") -> {
119+
val noAnnotationString = stringBeforeLastColonWithoutComment.substringAfterLast(")").trim()
120120
val splits = noAnnotationString.split(" ")
121121
val propertyName =
122122
splits.filterIndexed { index, s -> listOf(0).contains(index).not() }
123123
.joinToString(" ")
124124
propertyName
125125
}
126-
stringBeforeColon.contains("@") -> {
127-
val splits = stringBeforeColon.split(" ")
126+
stringBeforeLastColonWithoutComment.contains("@") -> {
127+
val splits = stringBeforeLastColonWithoutComment.split(" ")
128128
val propertyName =
129129
splits.filterIndexed { index, s -> listOf(0, 1).contains(index).not() }
130130
.joinToString(" ")
131131
propertyName
132132
}
133133
else -> {
134-
val splits = stringBeforeColon.split(" ")
134+
val splits = stringBeforeLastColonWithoutComment.split(" ")
135135
val propertyName =
136136
splits.filterIndexed { index, s -> listOf(0).contains(index).not() }
137137
.joinToString(" ")

src/test/kotlin/wu/seal/jsontokotlin/codeelements/KClassNameTest.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package wu.seal.jsontokotlin.codeelements
22

3+
import com.winterbe.expekt.should
34
import org.junit.Assert.assertTrue
45
import org.junit.Test
56

@@ -28,6 +29,9 @@ class KClassNameTest {
2829

2930
}
3031

32+
/**
33+
* test get name logic works well when name contains illegal character
34+
*/
3135
@Test
3236
fun getName() {
3337
val rawClassName = """
@@ -36,13 +40,15 @@ class KClassNameTest {
3640

3741
val legalClassName = KClassName.getName(rawClassName)
3842

39-
assertTrue(legalClassName.startsWith("N"))
40-
assertTrue(legalClassName == "NAM12335E431")
43+
legalClassName.should.startWith("N")
44+
45+
legalClassName.should.be.equal("NAM12335E431")
4146

4247
val rawClassName1 = "341@!$#43214%$#@%34"
48+
4349
val legalClassName1 = KClassName.getName(rawClassName1)
4450
assertTrue(legalClassName1.isNotEmpty())
45-
assertTrue(legalClassName1 == "X3414321434")
51+
legalClassName1.should.be.equal("X3414321434")
4652

4753
}
4854

src/test/kotlin/wu/seal/jsontokotlin/codeelements/KPropertyNameTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ class KPropertyNameTest {
7878
resultCamelCaseNameUnderScore.should.be.equal("abcAbc")
7979
resultCamelCaseNameWithMiddleScore.should.be.equal("abcAbc")
8080
}
81+
82+
/**
83+
* Test Property name camle case format if OK when there two colon inside string
84+
*/
85+
@Test
86+
fun testPropertyNameCameCaseFormatWithTwoColon() {
87+
KPropertyName.getName("hello:baby:come").should.be.equal("helloBabyCome")
88+
}
8189
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package wu.seal.jsontokotlin.regression
2+
3+
import com.winterbe.expekt.should
4+
import org.junit.Before
5+
import org.junit.Test
6+
import wu.seal.jsontokotlin.KotlinDataClassCodeMaker
7+
import wu.seal.jsontokotlin.test.TestConfig
8+
9+
class Issue089Test {
10+
11+
private val json = """{
12+
"mime_type": "image/jpeg",
13+
"file_name": "136149.jpg",
14+
"source_type": "chk",
15+
"source_id": 3000193,
16+
"year_month": "201810",
17+
"day": "31",
18+
"type_document_id": 11,
19+
"usr_id": 55572,
20+
"file_desc": "текст примечания",
21+
"bytes": "/9j/4AAQSkZ...Nd/VJ/Ef",
22+
"description": {
23+
"cmis:objectTypeId": "D:esp:act",
24+
"esp:doc_name": "текст примечания",
25+
"esp:iin_bin": "101040011256",
26+
"esp:create_date": "2018.09.31",
27+
"esp:reg_num": "181000000103012/00022",
28+
"esp:reg_date": "2018.10.01",
29+
"esp:author": "Уалиева Асель Мухаметбековна"
30+
}
31+
}"""
32+
33+
private val expected = """data class Test(
34+
@SerializedName("bytes")
35+
val bytes: String = "", // /9j/4AAQSkZ...Nd/VJ/Ef
36+
@SerializedName("day")
37+
val day: String = "", // 31
38+
@SerializedName("description")
39+
val description: Description = Description(),
40+
@SerializedName("file_desc")
41+
val fileDesc: String = "", // текст примечания
42+
@SerializedName("file_name")
43+
val fileName: String = "", // 136149.jpg
44+
@SerializedName("mime_type")
45+
val mimeType: String = "", // image/jpeg
46+
@SerializedName("source_id")
47+
val sourceId: Int = 0, // 3000193
48+
@SerializedName("source_type")
49+
val sourceType: String = "", // chk
50+
@SerializedName("type_document_id")
51+
val typeDocumentId: Int = 0, // 11
52+
@SerializedName("usr_id")
53+
val usrId: Int = 0, // 55572
54+
@SerializedName("year_month")
55+
val yearMonth: String = "" // 201810
56+
) {
57+
data class Description(
58+
@SerializedName("cmis:objectTypeId")
59+
val cmisObjectTypeId: String = "", // D:esp:act
60+
@SerializedName("esp:author")
61+
val espAuthor: String = "", // Уалиева Асель Мухаметбековна
62+
@SerializedName("esp:create_date")
63+
val espCreateDate: String = "", // 2018.09.31
64+
@SerializedName("esp:doc_name")
65+
val espDocName: String = "", // текст примечания
66+
@SerializedName("esp:iin_bin")
67+
val espIinBin: String = "", // 101040011256
68+
@SerializedName("esp:reg_date")
69+
val espRegDate: String = "", // 2018.10.01
70+
@SerializedName("esp:reg_num")
71+
val espRegNum: String = "" // 181000000103012/00022
72+
)
73+
}"""
74+
75+
/**
76+
* init test environment before test
77+
*/
78+
@Before
79+
fun setUp() {
80+
TestConfig.setToTestInitState()
81+
}
82+
83+
/**
84+
* test issue #89 of Github Project issue
85+
*/
86+
@Test
87+
fun testIssue089() {
88+
val result = KotlinDataClassCodeMaker("Test", json).makeKotlinDataClassCode()
89+
result.trim().should.be.equal(expected)
90+
}
91+
}

0 commit comments

Comments
 (0)