Skip to content

Commit

Permalink
Release 1.5.1
Browse files Browse the repository at this point in the history
Fix a bug about number only property name
  • Loading branch information
seal committed Dec 18, 2017
1 parent 77557fc commit 22c9039
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 46 deletions.
6 changes: 2 additions & 4 deletions resources/META-INF/plugin.xml
@@ -1,7 +1,7 @@
<idea-plugin>
<id>wu.seal.tool.jsontokotlin</id>
<name>JsonToKotlinClass</name>
<version>1.5</version>
<version>1.5.1</version>
<vendor email="sealkingking@163.com" url="https://www.github.com/wuseal">Seal</vendor>

<description><![CDATA[
Expand All @@ -11,9 +11,7 @@
]]></description>

<change-notes><![CDATA[
1.support for LoganSquare and Moshi.<br>
2.support for cutomize annotation.<br>
3.simply usage steps.
1.Fix a bug about only number json property.<br>
]]>
</change-notes>

Expand Down
4 changes: 2 additions & 2 deletions src/wu/seal/jsontokotlin/JsonToKotlinApplication.kt
Expand Up @@ -10,7 +10,7 @@ import wu.seal.jsontokotlin.statistics.sendHistoryActionInfo
* Created by Seal.wu on 2017/8/21.
*/

const val PLUGIN_VERSION = "1.5"
const val PLUGIN_VERSION = "1.5.1"

class JsonToKotlinApplication : ApplicationComponent {

Expand All @@ -27,6 +27,6 @@ class JsonToKotlinApplication : ApplicationComponent {
override fun disposeComponent() {}

override fun getComponentName(): String {
return "wu.seal.wu.seal.jsontokotlin.JsonToKotlinApplication"
return "wu.seal.jsontokotlin.JsonToKotlinApplication"
}
}
8 changes: 7 additions & 1 deletion src/wu/seal/jsontokotlin/MakeKotlinClassAction.kt
Expand Up @@ -77,9 +77,15 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
Thread {
sendActionInfo(gson.toJson(SuccessCompleteAction()))
}.start()

} catch(e: Exception) {

val yes = Messages.showYesNoDialog("Some thing execute wrong.\nAgree with publishing your JSON text to help us to solve the problem?", "Excuse me", Messages.getQuestionIcon())
if (yes != Messages.YES) {
jsonString = "User keep private about JSON text"
}
getUncaughtExceptionHandler(jsonString) {
Messages.showErrorDialog("I am sorry,JsonToKotlinClass may occur a RuntimeException,\nYou could try again later or recover to the old version,\nOr you could post an issue here:\nhttps://github.com/wuseal/JsonToKotlinClass", "Occur a fatal error")
Messages.showErrorDialog("I am sorry,JsonToKotlinClass may occur a RuntimeException,\nYou could try again later or recover to the old version,\nOr you could post an issue here:\nhttps://github.com/wuseal/JsonToKotlinClass\nWe will fixed it soon!", "Occur a fatal error")
}.uncaughtException(Thread.currentThread(), e)

throw e
Expand Down
31 changes: 15 additions & 16 deletions src/wu/seal/jsontokotlin/codeelements/KClassName.kt
@@ -1,5 +1,6 @@
package wu.seal.jsontokotlin.codeelements


/**
* Transform to legal Class name
* Created by Seal on 2017/9/18.
Expand All @@ -10,44 +11,42 @@ interface IKClassName {

}

fun main(args: Array<String>) {
val name1 = """
!@3214 12#$%n^&*(-a)_+-=m12335_e43{}|[]\\;':1",./<>?/*-+`
"""
println("orginal name is |$name1|")
println("Name1 is : |${wu.seal.jsontokotlin.codeelements.KClassName.getName(name1)}|")
}

object KClassName : KName(), wu.seal.jsontokotlin.codeelements.IKClassName {
object KClassName : KName(), IKClassName {

override fun getLegalClassName(rawClassName: String): String {

return wu.seal.jsontokotlin.codeelements.KClassName.getUpperCamelCaseLegalName(rawClassName)
val upperCamelCaseLegalName = getUpperCamelCaseLegalName(rawClassName)
if (upperCamelCaseLegalName.isNotEmpty()) {

return upperCamelCaseLegalName
} else {
return getUpperCamelCaseLegalName("X-"+rawClassName)
}
}

private fun getUpperCamelCaseLegalName(rawClassName: String): String {
/**
* keep " " character
*/
val pattern = "${wu.seal.jsontokotlin.codeelements.KClassName.illegalCharacter}".replace(Regex(wu.seal.jsontokotlin.codeelements.KClassName.nameSeparator.toString()), "")
val pattern = "$illegalCharacter".replace(Regex(nameSeparator.toString()), "")

val temp = rawClassName.replace(Regex(pattern), "").let {

return@let wu.seal.jsontokotlin.codeelements.KClassName.removeStartNumberAndIllegalCharacter(it)
return@let removeStartNumberAndIllegalCharacter(it)

}

val upperCamelCase = wu.seal.jsontokotlin.codeelements.KClassName.toUpperCamelCase(temp)
val upperCamelCase =toUpperCamelCase(temp)

val legalName = wu.seal.jsontokotlin.codeelements.KClassName.toBeLegalName(upperCamelCase)
val legalName = toBeLegalName(upperCamelCase)

return legalName
}


override fun getName(rawName: String): String {

return wu.seal.jsontokotlin.codeelements.KClassName.getLegalClassName(rawName)
return getLegalClassName(rawName)
}


Expand All @@ -58,7 +57,7 @@ object KClassName : KName(), wu.seal.jsontokotlin.codeelements.IKClassName {

val stringBuilder = StringBuilder()

temp.split(Regex(wu.seal.jsontokotlin.codeelements.KClassName.nameSeparator.toString())).forEach {
temp.split(Regex(nameSeparator.toString())).forEach {
if (it.isNotBlank()) {
stringBuilder.append(it.substring(0, 1).toUpperCase().plus(it.substring(1)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/wu/seal/jsontokotlin/codeelements/KName.kt
Expand Up @@ -12,7 +12,7 @@ interface IKName {

}

abstract class KName : wu.seal.jsontokotlin.codeelements.IKName {
abstract class KName : IKName {

private val suffix = "X"

Expand Down
42 changes: 21 additions & 21 deletions src/wu/seal/jsontokotlin/codeelements/KPropertyName.kt
Expand Up @@ -21,22 +21,12 @@ interface IPropertyNameMaker {

}

fun main(args: Array<String>) {
val name1 = """
!@#$ -_32322 3 32%N^&*(-a)_+-=m123-35 e43{}|[]\\;':1",./<>?/*-+`
"""

println("orginal name is |$name1|")
println("Name1 is : |${wu.seal.jsontokotlin.codeelements.KPropertyName.getName(name1)}|")

}

object KPropertyName : KName(), wu.seal.jsontokotlin.codeelements.IPropertyNameMaker {
object KPropertyName : KName(), IPropertyNameMaker {


override fun getName(rawName: String): String {

return wu.seal.jsontokotlin.codeelements.KPropertyName.makePropertyName(rawName, true)
return makePropertyName(rawName, true)
}

override fun makePropertyName(rawString: String): String {
Expand All @@ -48,29 +38,33 @@ object KPropertyName : KName(), wu.seal.jsontokotlin.codeelements.IPropertyNameM

if (needTransformToLegalName) {

return wu.seal.jsontokotlin.codeelements.KPropertyName.makeCamelCaseLegalName(rawString)
val camelCaseLegalName = makeCamelCaseLegalName(rawString)
return if (camelCaseLegalName.isEmpty()) KPropertyName.makeCamelCaseLegalName("x-" + rawString) else camelCaseLegalName

} else {
return rawString
}

}

/**
* this function may return empty string when the raw string is only make of number and illegal character
*/
private fun makeCamelCaseLegalName(rawString: String): String {
/**
* keep nameSeparator character
*/
val pattern = "${wu.seal.jsontokotlin.codeelements.KPropertyName.illegalCharacter}".replace(Regex(wu.seal.jsontokotlin.codeelements.KPropertyName.nameSeparator.toString()), "")
val pattern = "${illegalCharacter}".replace(Regex(nameSeparator.toString()), "")

val temp = rawString.replace(Regex(pattern), "").let {

return@let wu.seal.jsontokotlin.codeelements.KPropertyName.removeStartNumberAndIllegalCharacter(it)
return@let removeStartNumberAndIllegalCharacter(it)

}

val lowerCamelCaseName = wu.seal.jsontokotlin.codeelements.KPropertyName.toLowerCamelCase(temp)
val lowerCamelCaseName = toLowerCamelCase(temp)

val legalName = wu.seal.jsontokotlin.codeelements.KPropertyName.toBeLegalName(lowerCamelCaseName)
val legalName = toBeLegalName(lowerCamelCaseName)

return legalName
}
Expand All @@ -83,20 +77,26 @@ object KPropertyName : KName(), wu.seal.jsontokotlin.codeelements.IPropertyNameM

val stringBuilder = StringBuilder()

temp.split(Regex(wu.seal.jsontokotlin.codeelements.KPropertyName.nameSeparator.toString())).forEach {
temp.split(Regex(nameSeparator.toString())).forEach {
if (it.isNotBlank()) {
stringBuilder.append(it.substring(0, 1).toUpperCase().plus(it.substring(1)))
}
}

val camelCaseName = stringBuilder.toString()

val lowerCamelCaseName = camelCaseName.substring(0, 1).toLowerCase().plus(camelCaseName.substring(1))
if (camelCaseName.isNotEmpty()) {

return lowerCamelCaseName
val lowerCamelCaseName = camelCaseName.substring(0, 1).toLowerCase().plus(camelCaseName.substring(1))

return lowerCamelCaseName
} else {

return camelCaseName
}

}

}


}
5 changes: 4 additions & 1 deletion src/wu/seal/jsontokotlin/statistics/ExceptionHandler.kt
Expand Up @@ -32,9 +32,12 @@ fun getUncaughtExceptionHandler(jsonString: String, callBack: () -> Unit): Threa
}
printWriter.close()
logBuilder.append(stringWriter.toString())

logBuilder.append("Error Json String:\n")
logBuilder.append(jsonString)
sendExceptionLog(logBuilder.toString())
Thread {
sendExceptionLog(logBuilder.toString())
}.start()

callBack.invoke()
}
50 changes: 50 additions & 0 deletions test/wu/seal/jsontokotlin/codeelements/KClassNameTest.kt
@@ -0,0 +1,50 @@
package wu.seal.jsontokotlin.codeelements

import org.junit.*
import org.junit.Test

import org.junit.Assert.*

/**
* Created by Seal.Wu on 2017/12/18.
*/
class KClassNameTest {

@org.junit.Test
fun getLegalClassName() {

val rawClassName = """
!@3214 12#$%n^&*(-a)_+-=m12335_e43{}|[]\\;':1",./<>?/*-+`
"""

val legalClassName = KClassName.getLegalClassName(rawClassName)

assertTrue(legalClassName.startsWith("N"))
assertTrue(legalClassName == "NAM12335E431")

val rawClassName1 = "341@!$#43214%$#@%34"
val legalClassName1 = KClassName.getLegalClassName(rawClassName1)
assertTrue(legalClassName1.isNotEmpty())
assertTrue(legalClassName1 == "X3414321434")

}

@Test
fun getName() {
val rawClassName = """
!@3214 12#$%n^&*(-a)_+-=m12335_e43{}|[]\\;':1",./<>?/*-+`
"""

val legalClassName = KClassName.getName(rawClassName)

assertTrue(legalClassName.startsWith("N"))
assertTrue(legalClassName == "NAM12335E431")

val rawClassName1 = "341@!$#43214%$#@%34"
val legalClassName1 = KClassName.getName(rawClassName1)
assertTrue(legalClassName1.isNotEmpty())
assertTrue(legalClassName1 == "X3414321434")

}

}
69 changes: 69 additions & 0 deletions test/wu/seal/jsontokotlin/codeelements/KPropertyNameTest.kt
@@ -0,0 +1,69 @@
package wu.seal.jsontokotlin.codeelements

import org.junit.*
import org.junit.Test

import org.junit.Assert.*

/**
* Created by Seal.Wu on 2017/12/18.
*/
class KPropertyNameTest {
@org.junit.Test
fun getName() {
val originNmae = """
!@#$ -_32322 3 32%N^&*(-a)_+-=m123-35 e43{}|[]\\;':1",./<>?/*-+`
"""
val resultName = KPropertyName.getName(originNmae)

assertTrue(resultName.startsWith("n"))
assertTrue(resultName.endsWith("1"))
assertTrue(resultName.contains("A"))
assertTrue(resultName.contains("M"))
assertTrue(resultName.contains("E"))


val rawName = "#$@#4324324$$#@324"
val legalName = KPropertyName.getName(rawName)
assertTrue(legalName.isNotEmpty())
assertTrue(legalName.startsWith("x"))

}

@Test
fun makePropertyName() {
val originNmae = """
!@#$ -_32322 3 32%N^&*(-a)_+-=m123-35 e43{}|[]\\;':1",./<>?/*-+`
"""
val resultName = KPropertyName.makePropertyName(originNmae)

assertTrue(originNmae == resultName)


}

@Test
fun makePropertyName1() {
val originNmae = """
!@#$ -_32322 3 32%N^&*(-a)_+-=m123-35 e43{}|[]\\;':1",./<>?/*-+`
"""
val resultNameNotToBeLegal = KPropertyName.makePropertyName(originNmae, false)

assertTrue(originNmae == resultNameNotToBeLegal)

val resultNameToBeLegal = KPropertyName.makePropertyName(originNmae, true)

assertTrue(originNmae != resultNameToBeLegal)
assertTrue(resultNameToBeLegal.startsWith("n"))
assertTrue(resultNameToBeLegal.endsWith("1"))
assertTrue(resultNameToBeLegal.contains("A"))
assertTrue(resultNameToBeLegal.contains("M"))
assertTrue(resultNameToBeLegal.contains("E"))

val rawName = "#$@#4324324$$#@324"
val legalName = KPropertyName.getName(rawName)
assertTrue(legalName.isNotEmpty())
assertTrue(legalName.startsWith("x"))
}

}

0 comments on commit 22c9039

Please sign in to comment.