Skip to content

Commit c929954

Browse files
author
seal
committed
Release 1.7.3
1 parent f8b63cf commit c929954

File tree

6 files changed

+204
-6
lines changed

6 files changed

+204
-6
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.intellij.openapi.project.Project
1111
import com.intellij.openapi.ui.Messages
1212
import wu.seal.jsontokotlin.feedback.*
1313
import wu.seal.jsontokotlin.ui.JsonInputDialog
14+
import wu.seal.jsontokotlin.utils.ClassCodeFilter
1415
import wu.seal.jsontokotlin.utils.LogUtil
1516
import wu.seal.jsontokotlin.utils.executeCouldRollBackAction
1617

@@ -131,7 +132,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
131132
} else {
132133
offset = document.textLength - 1
133134
}
134-
document.insertString(Math.max(offset, 0), codeMaker.makeKotlinData())
135+
document.insertString(Math.max(offset, 0), ClassCodeFilter.removeDuplicateClassCode(codeMaker.makeKotlinData()))
135136
}
136137
return true
137138
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package wu.seal.jsontokotlin.utils
2+
3+
import wu.seal.jsontokotlin.ConfigManager
4+
5+
/**
6+
* Class Code Filter
7+
* Created by Seal.Wu on 2018/4/18.
8+
*/
9+
object ClassCodeFilter {
10+
11+
/**
12+
* when not in `innerClassModel` and the class spit with `\n\n` then remove the duplicate class
13+
*/
14+
fun removeDuplicateClassCode(generateClassesString: String): String {
15+
16+
if (ConfigManager.isInnerClassModel.not()) {
17+
18+
val set = mutableSetOf<String>()
19+
set.addAll(generateClassesString.split("\n\n"))
20+
return set.joinToString("\n\n")
21+
22+
} else {
23+
return generateClassesString
24+
}
25+
}
26+
}

src/wu/seal/jsontokotlin/utils/TypeHelper.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fun getJsonObjectType(type: String): String {
5454
*/
5555
fun getChildType(arrayType: String): String {
5656

57-
val tempType = arrayType.replace(Regex("<|>|List"), "")
57+
val tempType = arrayType.replace(Regex("List<|>"), "")
5858

5959
return tempType
6060
}
@@ -109,12 +109,12 @@ fun isExpectedJsonObjArrayType(jsonElementArray: JsonArray): Boolean {
109109

110110
/**
111111
* when get the child type in an array
112-
* ,we need to modify the property name to make it's type name looks like a child type.
112+
* ,we need to make the property name be legal and then modify the property name to make it's type name looks like a child type.
113113
* filter the sequence as 'list' ,"List'
114114
* and remove the last character 's' to make it like a child rather than a list
115115
*/
116-
fun adjustPropertyNameForGettingArrayChildType(property: String): String {
117-
var innerProperty = property
116+
internal fun adjustPropertyNameForGettingArrayChildType(property: String): String {
117+
var innerProperty = KClassName.getLegalClassName(property)
118118
if (innerProperty.endsWith("ies")) {
119119
innerProperty = innerProperty.substring(0, innerProperty.length - 3) + "y"
120120
} else if (innerProperty.contains("List")) {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package wu.seal.jsontokotlin.utils
2+
3+
import com.winterbe.expekt.should
4+
import org.junit.Test
5+
import wu.seal.jsontokotlin.test.TestConfig
6+
7+
/**
8+
* Created by Seal.Wu on 2018/4/18.
9+
*/
10+
class ClassCodeFilterTest {
11+
12+
private val withDuplicateClassString = """data class Test(
13+
val user_id: String = "",
14+
val password: String = "",
15+
val gender: Int = 0,
16+
val birthday: String = "",
17+
val phone: String = "",
18+
val name: String = "",
19+
val cons: Int = 0,
20+
val gps_info: String = "",
21+
val activity_time: String = "",
22+
val sign: String = "",
23+
val vip: String = "",
24+
val being_liked_num: Int = 0,
25+
val info: Info = Info(),
26+
val tag: List<String> = listOf(),
27+
val interest: Interest = Interest(),
28+
val answer: List<Answer> = listOf(),
29+
val media: List<Media> = listOf(),
30+
val friend_show: FriendShow = FriendShow()
31+
)
32+
33+
data class Info(
34+
val industry: String = "",
35+
val work: String = "",
36+
val comp: String = "",
37+
val city: String = "",
38+
val hauntedly: String = ""
39+
)
40+
41+
data class Interest(
42+
val sports: List<String> = listOf(),
43+
val music: List<String> = listOf(),
44+
val food: List<String> = listOf(),
45+
val movie: List<String> = listOf(),
46+
val books_and_comic: List<String> = listOf(),
47+
val footprint: List<String> = listOf()
48+
)
49+
50+
data class FriendShow(
51+
val cover: String = "",
52+
val show_list: List<Show> = listOf()
53+
)
54+
55+
data class Show(
56+
val media: List<Media> = listOf(),
57+
val time: String = "",
58+
val title: String = ""
59+
)
60+
61+
data class Media(
62+
val type: Int = 0,
63+
val url: String = "",
64+
val order: Int = 0
65+
)
66+
67+
data class Media(
68+
val type: Int = 0,
69+
val url: String = "",
70+
val order: Int = 0
71+
)
72+
73+
data class Answer(
74+
val a: String = "",
75+
val q: String = ""
76+
)"""
77+
78+
@Test
79+
fun removeDuplicateClassCode() {
80+
TestConfig.isInnerClassModel = false
81+
withDuplicateClassString.indexOf("data class Media(").should.not.be.equal(withDuplicateClassString.lastIndexOf("data class Media("))
82+
val removeDuplicateClassCode = ClassCodeFilter.removeDuplicateClassCode(withDuplicateClassString)
83+
removeDuplicateClassCode.indexOf("data class Media(").should.be.equal(removeDuplicateClassCode.lastIndexOf("data class Media("))
84+
85+
println(removeDuplicateClassCode)
86+
}
87+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package wu.seal.jsontokotlin.utils
2+
3+
import com.google.gson.JsonArray
4+
import com.google.gson.JsonElement
5+
import com.google.gson.JsonObject
6+
import com.google.gson.JsonParser
7+
import com.winterbe.expekt.should
8+
import org.junit.Test
9+
10+
import org.junit.Assert.*
11+
12+
/**
13+
* Created by Seal.Wu on 2018/4/18.
14+
*/
15+
class TypeHelperKtTest {
16+
17+
@Test
18+
fun getPrimitiveTypeTest() {
19+
}
20+
21+
@Test
22+
fun getJsonObjectTypeTest() {
23+
}
24+
25+
@Test
26+
fun getChildTypeTest() {
27+
getChildType("List<List<List<Boy>>>").should.be.equal("Boy")
28+
}
29+
30+
@Test
31+
fun getOutTypeTest() {
32+
}
33+
34+
@Test
35+
fun getRawTypeTest() {
36+
}
37+
38+
@Test
39+
fun getArrayTypeTest() {
40+
val jsonArrayBool = JsonArray()
41+
jsonArrayBool.add(true)
42+
getArrayType("good_friends", jsonArrayBool).should.be.equal("List<Boolean>")
43+
44+
val jsonArrayInt = JsonArray()
45+
jsonArrayInt.add(1)
46+
getArrayType("good_friends", jsonArrayInt).should.be.equal("List<Int>")
47+
48+
val jsonArrayObj = JsonArray()
49+
val jsonObject = JsonObject()
50+
jsonObject.add("name", JsonParser().parse("seal"))
51+
jsonArrayObj.add(jsonObject)
52+
getArrayType("good_friends", jsonArrayObj).should.be.equal("List<GoodFriend>")
53+
54+
val jsonArrayObj1 = JsonArray()
55+
val jsonObject1 = JsonObject()
56+
jsonObject1.add("name", JsonParser().parse("seal"))
57+
jsonArrayObj1.add(jsonObject1)
58+
getArrayType("show_list", jsonArrayObj1).should.be.equal("List<Show>")
59+
}
60+
61+
@Test
62+
fun isExpectedJsonObjArrayTypeTest() {
63+
val jsonArray = JsonArray()
64+
jsonArray.add(true)
65+
isExpectedJsonObjArrayType(jsonArray).should.be.`false`
66+
jsonArray.removeAll { true }
67+
jsonArray.add(1)
68+
isExpectedJsonObjArrayType(jsonArray).should.be.`false`
69+
jsonArray.removeAll { true }
70+
jsonArray.add(JsonObject())
71+
isExpectedJsonObjArrayType(jsonArray).should.be.`true`
72+
}
73+
74+
@Test
75+
fun adjustPropertyNameForGettingArrayChildTypeTest() {
76+
adjustPropertyNameForGettingArrayChildType("").should.be.equal("X")
77+
adjustPropertyNameForGettingArrayChildType("List").should.be.equal("")
78+
adjustPropertyNameForGettingArrayChildType("arrayList").should.be.equal("Array")
79+
adjustPropertyNameForGettingArrayChildType("Apples").should.be.equal("Apple")
80+
adjustPropertyNameForGettingArrayChildType("Activities").should.be.equal("Activity")
81+
adjustPropertyNameForGettingArrayChildType("Book_List").should.be.equal("Book")
82+
adjustPropertyNameForGettingArrayChildType("show_list").should.be.equal("Show")
83+
}
84+
}

0 commit comments

Comments
 (0)