Skip to content

Commit

Permalink
Release 1.7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
seal committed Apr 18, 2018
1 parent f8b63cf commit c929954
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

3 changes: 2 additions & 1 deletion src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import wu.seal.jsontokotlin.feedback.*
import wu.seal.jsontokotlin.ui.JsonInputDialog
import wu.seal.jsontokotlin.utils.ClassCodeFilter
import wu.seal.jsontokotlin.utils.LogUtil
import wu.seal.jsontokotlin.utils.executeCouldRollBackAction

Expand Down Expand Up @@ -131,7 +132,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
} else {
offset = document.textLength - 1
}
document.insertString(Math.max(offset, 0), codeMaker.makeKotlinData())
document.insertString(Math.max(offset, 0), ClassCodeFilter.removeDuplicateClassCode(codeMaker.makeKotlinData()))
}
return true
}
Expand Down
26 changes: 26 additions & 0 deletions src/wu/seal/jsontokotlin/utils/ClassCodeFilter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package wu.seal.jsontokotlin.utils

import wu.seal.jsontokotlin.ConfigManager

/**
* Class Code Filter
* Created by Seal.Wu on 2018/4/18.
*/
object ClassCodeFilter {

/**
* when not in `innerClassModel` and the class spit with `\n\n` then remove the duplicate class
*/
fun removeDuplicateClassCode(generateClassesString: String): String {

if (ConfigManager.isInnerClassModel.not()) {

val set = mutableSetOf<String>()
set.addAll(generateClassesString.split("\n\n"))
return set.joinToString("\n\n")

} else {
return generateClassesString
}
}
}
8 changes: 4 additions & 4 deletions src/wu/seal/jsontokotlin/utils/TypeHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fun getJsonObjectType(type: String): String {
*/
fun getChildType(arrayType: String): String {

val tempType = arrayType.replace(Regex("<|>|List"), "")
val tempType = arrayType.replace(Regex("List<|>"), "")

return tempType
}
Expand Down Expand Up @@ -109,12 +109,12 @@ fun isExpectedJsonObjArrayType(jsonElementArray: JsonArray): Boolean {

/**
* when get the child type in an array
* ,we need to modify the property name to make it's type name looks like a child type.
* ,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.
* filter the sequence as 'list' ,"List'
* and remove the last character 's' to make it like a child rather than a list
*/
fun adjustPropertyNameForGettingArrayChildType(property: String): String {
var innerProperty = property
internal fun adjustPropertyNameForGettingArrayChildType(property: String): String {
var innerProperty = KClassName.getLegalClassName(property)
if (innerProperty.endsWith("ies")) {
innerProperty = innerProperty.substring(0, innerProperty.length - 3) + "y"
} else if (innerProperty.contains("List")) {
Expand Down
87 changes: 87 additions & 0 deletions test/wu/seal/jsontokotlin/utils/ClassCodeFilterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package wu.seal.jsontokotlin.utils

import com.winterbe.expekt.should
import org.junit.Test
import wu.seal.jsontokotlin.test.TestConfig

/**
* Created by Seal.Wu on 2018/4/18.
*/
class ClassCodeFilterTest {

private val withDuplicateClassString = """data class Test(
val user_id: String = "",
val password: String = "",
val gender: Int = 0,
val birthday: String = "",
val phone: String = "",
val name: String = "",
val cons: Int = 0,
val gps_info: String = "",
val activity_time: String = "",
val sign: String = "",
val vip: String = "",
val being_liked_num: Int = 0,
val info: Info = Info(),
val tag: List<String> = listOf(),
val interest: Interest = Interest(),
val answer: List<Answer> = listOf(),
val media: List<Media> = listOf(),
val friend_show: FriendShow = FriendShow()
)
data class Info(
val industry: String = "",
val work: String = "",
val comp: String = "",
val city: String = "",
val hauntedly: String = ""
)
data class Interest(
val sports: List<String> = listOf(),
val music: List<String> = listOf(),
val food: List<String> = listOf(),
val movie: List<String> = listOf(),
val books_and_comic: List<String> = listOf(),
val footprint: List<String> = listOf()
)
data class FriendShow(
val cover: String = "",
val show_list: List<Show> = listOf()
)
data class Show(
val media: List<Media> = listOf(),
val time: String = "",
val title: String = ""
)
data class Media(
val type: Int = 0,
val url: String = "",
val order: Int = 0
)
data class Media(
val type: Int = 0,
val url: String = "",
val order: Int = 0
)
data class Answer(
val a: String = "",
val q: String = ""
)"""

@Test
fun removeDuplicateClassCode() {
TestConfig.isInnerClassModel = false
withDuplicateClassString.indexOf("data class Media(").should.not.be.equal(withDuplicateClassString.lastIndexOf("data class Media("))
val removeDuplicateClassCode = ClassCodeFilter.removeDuplicateClassCode(withDuplicateClassString)
removeDuplicateClassCode.indexOf("data class Media(").should.be.equal(removeDuplicateClassCode.lastIndexOf("data class Media("))

println(removeDuplicateClassCode)
}
}
84 changes: 84 additions & 0 deletions test/wu/seal/jsontokotlin/utils/TypeHelperKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package wu.seal.jsontokotlin.utils

import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.winterbe.expekt.should
import org.junit.Test

import org.junit.Assert.*

/**
* Created by Seal.Wu on 2018/4/18.
*/
class TypeHelperKtTest {

@Test
fun getPrimitiveTypeTest() {
}

@Test
fun getJsonObjectTypeTest() {
}

@Test
fun getChildTypeTest() {
getChildType("List<List<List<Boy>>>").should.be.equal("Boy")
}

@Test
fun getOutTypeTest() {
}

@Test
fun getRawTypeTest() {
}

@Test
fun getArrayTypeTest() {
val jsonArrayBool = JsonArray()
jsonArrayBool.add(true)
getArrayType("good_friends", jsonArrayBool).should.be.equal("List<Boolean>")

val jsonArrayInt = JsonArray()
jsonArrayInt.add(1)
getArrayType("good_friends", jsonArrayInt).should.be.equal("List<Int>")

val jsonArrayObj = JsonArray()
val jsonObject = JsonObject()
jsonObject.add("name", JsonParser().parse("seal"))
jsonArrayObj.add(jsonObject)
getArrayType("good_friends", jsonArrayObj).should.be.equal("List<GoodFriend>")

val jsonArrayObj1 = JsonArray()
val jsonObject1 = JsonObject()
jsonObject1.add("name", JsonParser().parse("seal"))
jsonArrayObj1.add(jsonObject1)
getArrayType("show_list", jsonArrayObj1).should.be.equal("List<Show>")
}

@Test
fun isExpectedJsonObjArrayTypeTest() {
val jsonArray = JsonArray()
jsonArray.add(true)
isExpectedJsonObjArrayType(jsonArray).should.be.`false`
jsonArray.removeAll { true }
jsonArray.add(1)
isExpectedJsonObjArrayType(jsonArray).should.be.`false`
jsonArray.removeAll { true }
jsonArray.add(JsonObject())
isExpectedJsonObjArrayType(jsonArray).should.be.`true`
}

@Test
fun adjustPropertyNameForGettingArrayChildTypeTest() {
adjustPropertyNameForGettingArrayChildType("").should.be.equal("X")
adjustPropertyNameForGettingArrayChildType("List").should.be.equal("")
adjustPropertyNameForGettingArrayChildType("arrayList").should.be.equal("Array")
adjustPropertyNameForGettingArrayChildType("Apples").should.be.equal("Apple")
adjustPropertyNameForGettingArrayChildType("Activities").should.be.equal("Activity")
adjustPropertyNameForGettingArrayChildType("Book_List").should.be.equal("Book")
adjustPropertyNameForGettingArrayChildType("show_list").should.be.equal("Show")
}
}

0 comments on commit c929954

Please sign in to comment.