Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/kotlin/extensions/ExtensionsCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ object ExtensionsCollector {
/**
* all extensions
*/
val extensions = listOf<Extension>(
val extensions = listOf(
PropertyPrefixSupport,
PropertySuffixSupport
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JsonToKotlinApplication : ApplicationComponent {

LogUtil.i("init JSON To Kotlin Class version ==$PLUGIN_VERSION")

Thread() {
Thread {
try {
sendConfigInfo()
sendHistoryExceptionInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class KotlinDataClassCodeMaker( private val rootClassName: String, private val j
val kotlinDataClasses = KotlinDataClassMaker(rootClassName = rootClassName, json = json).makeKotlinDataClasses()

val interceptedDataClasses = kotlinDataClasses.map {it.applyInterceptors(interceptors)}
val code = interceptedDataClasses.joinToString("\n\n") {
return interceptedDataClasses.joinToString("\n\n") {
it.getCode()
}
return code
}
}
}
10 changes: 4 additions & 6 deletions src/main/kotlin/wu/seal/jsontokotlin/MakeKotlinClassAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
inputDialog.show()
val className = inputDialog.getClassName()
val inputString = inputDialog.inputString
val json = if (inputString?.startsWith("http") == true) {
val json = if (inputString.startsWith("http")) {
URL(inputString).readText()
} else inputString
if (json == null || json.isEmpty()) {
if (json.isEmpty()) {
return
}
jsonString = json
Expand Down Expand Up @@ -179,9 +179,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {

fun getCleanText(editorText: String): String {
val tempCleanText = editorText.substringBeforeLast("class")
val cleanText =
if (tempCleanText.trim().endsWith("data")) tempCleanText.trim().removeSuffix("data") else tempCleanText
return cleanText
return if (tempCleanText.trim().endsWith("data")) tempCleanText.trim().removeSuffix("data") else tempCleanText
}

fun getCurrentEditFileTemClassName(editorText: String) = editorText.substringAfterLast("class")
Expand Down Expand Up @@ -209,7 +207,7 @@ class MakeKotlinClassAction : AnAction("MakeKotlinClass") {
)
&& removeDocCommentAndPackageDeclareText.indexOf("class") != -1
&& removeDocCommentAndPackageDeclareText.substringAfter("class").substringAfter("(")
.replace(Regex("\\s"), "").let { it.equals(")") || it.equals("){}") })
.replace(Regex("\\s"), "").let { it == ")" || it == "){}" })
) {
couldGetAndReuseClassNameInCurrentEditFileForInsertCode = true
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/wu/seal/jsontokotlin/TargetJsonElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ class TargetJsonElement : ITargetJsonElement {
}

private fun getArrayChildElement(jsonArray: JsonArray): JsonElement {
if (jsonArray.size() >= 1) {
return getFullFieldElementFromArrayElement(jsonArray)
return if (jsonArray.size() >= 1) {
getFullFieldElementFromArrayElement(jsonArray)
} else {
return Gson().toJsonTree(Any())
Gson().toJsonTree(Any())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import wu.seal.jsontokotlin.utils.numberOf
data class Annotation(val annotationTemplate: String, val rawName: String) {

fun getAnnotationString(): String {
if (annotationTemplate.contains("%s")) {
return if (annotationTemplate.contains("%s")) {
val countS = annotationTemplate.numberOf("%s")
val args = Array(countS, { rawName })
return annotationTemplate.format(*args)
val args = Array(countS) { rawName }
annotationTemplate.format(*args)
} else {
return annotationTemplate
annotationTemplate
}
}

Expand All @@ -22,16 +22,16 @@ data class Annotation(val annotationTemplate: String, val rawName: String) {
throw IllegalArgumentException("Only support one line annotation!! current is $annotationString")
}

if (annotationString.contains("\"")) {
return if (annotationString.contains("\"")) {
if (annotationString.numberOf("\"") != 2) {
throw IllegalArgumentException("Only support one line annotation with one couple Double quotes!! current is $annotationString")
}
val rawName = annotationString.substringAfter("\"").substringBefore("\"")
val annotationTemplate =
annotationString.substringBefore("\"") + "\"%s\"" + annotationString.substringAfterLast("\"")
return Annotation(annotationTemplate, rawName)
annotationString.substringBefore("\"") + "\"%s\"" + annotationString.substringAfterLast("\"")
Annotation(annotationTemplate, rawName)
} else {
return Annotation(annotationString, "")
Annotation(annotationString, "")
}

}
Expand Down Expand Up @@ -64,4 +64,4 @@ data class Annotation(val annotationTemplate: String, val rawName: String) {

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class KotlinDataClass(
val indent = getIndent()
val code = buildString {
if (annotations.isNotEmpty()) {
val annotationsCode = annotations.map { it.getAnnotationString() }.joinToString("\n")
val annotationsCode = annotations.joinToString("\n") { it.getAnnotationString() }
if (annotationsCode.isNotBlank()) {
append(annotationsCode).append("\n")
}
Expand Down Expand Up @@ -46,16 +46,16 @@ data class KotlinDataClass(
append("}")
}
}
if (extraIndent.isNotEmpty()) {
return code.split("\n").map {
return if (extraIndent.isNotEmpty()) {
code.split("\n").map {
if (it.isNotBlank()) {
extraIndent + it
} else {
it
}
}.joinToString("\n")
} else {
return code
code
}
}

Expand Down Expand Up @@ -99,4 +99,4 @@ data class KotlinDataClass(

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class Property(

fun getCode(): String {

val code = buildString {
return buildString {
if (annotations.isNotEmpty()) {
val annotationsCode = annotations.map { it.getAnnotationString() }.joinToString("\n")
if (annotationsCode.isNotBlank()) {
Expand All @@ -28,7 +28,6 @@ data class Property(
append(" = ").append(value)
}
}
return code
}

fun toParsedProperty(): ParsedKotlinDataClass.Property {
Expand Down Expand Up @@ -77,4 +76,4 @@ data class Property(
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class LoganSquarePropertyAnnotationTemplate(val rawName: String) : AnnotationTem
}

override fun getAnnotations(): List<Annotation> {
return listOf<Annotation>(annotation)
return listOf(annotation)
}
}
}
29 changes: 10 additions & 19 deletions src/main/kotlin/wu/seal/jsontokotlin/codeelements/DefaultValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,17 @@ import wu.seal.jsontokotlin.utils.*
*/

fun getDefaultValue(propertyType: String): String {

val rawType = getRawType(propertyType)

if (rawType == TYPE_INT) {
return 0.toString()
} else if (rawType == TYPE_LONG) {
return 0L.toString()
} else if (rawType == TYPE_STRING) {
return "\"\""
} else if (rawType == TYPE_DOUBLE) {
return 0.0.toString()
} else if (rawType == TYPE_BOOLEAN) {
return false.toString()
} else if (rawType.contains("List<")) {
return "listOf()"
} else if (rawType.contains("Map<")) {
return "mapOf()"
} else if (rawType == TYPE_ANY) {
return "Any()"
} else {
return "$rawType()"
return when {
rawType == TYPE_INT -> 0.toString()
rawType == TYPE_LONG -> 0L.toString()
rawType == TYPE_STRING -> "\"\""
rawType == TYPE_DOUBLE -> 0.0.toString()
rawType == TYPE_BOOLEAN -> false.toString()
rawType.contains("List<") -> "listOf()"
rawType.contains("Map<") -> "mapOf()"
rawType == TYPE_ANY -> "Any()"
else -> "$rawType()"
}
}
16 changes: 5 additions & 11 deletions src/main/kotlin/wu/seal/jsontokotlin/codeelements/KClassName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ interface IKClassName {
object KClassName : KName(), IKClassName {

override fun getLegalClassName(rawClassName: String): String {

val upperCamelCaseLegalName = getUpperCamelCaseLegalName(rawClassName)
if (upperCamelCaseLegalName.isNotEmpty()) {

return upperCamelCaseLegalName
return if (upperCamelCaseLegalName.isNotEmpty()) {
upperCamelCaseLegalName
} else {
return getUpperCamelCaseLegalName("X-"+rawClassName)
getUpperCamelCaseLegalName("X-$rawClassName")
}
}

Expand All @@ -38,9 +36,7 @@ object KClassName : KName(), IKClassName {

val upperCamelCase =toUpperCamelCase(temp)

val legalName = toBeLegalName(upperCamelCase)

return legalName
return toBeLegalName(upperCamelCase)
}


Expand All @@ -63,9 +59,7 @@ object KClassName : KName(), IKClassName {
}
}

val upperCamelCaseName = stringBuilder.toString()

return upperCamelCaseName
return stringBuilder.toString()

}

Expand Down
13 changes: 6 additions & 7 deletions src/main/kotlin/wu/seal/jsontokotlin/codeelements/KName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ abstract class KName : IKName {

private val suffix = "X"

protected val illegalNameList = listOf<String>(
protected val illegalNameList = listOf(
"as", "break", "class", "continue", "do", "else", "false", "for", "fun", "if", "in", "interface", "is", "null"
, "object", "package", "return", "super", "this", "throw", "true", "try", "typealias", "val", "var", "when", "while"
)


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


protected val nameSeparator = listOf<String>(" ", "_", "-")
protected val nameSeparator = listOf(" ", "_", "-")


/**
Expand All @@ -40,9 +40,9 @@ abstract class KName : IKName {
return@indexOfFirst it in '0'..'9'
} == 0) {

val numberAndIllegalCharacters = listOf<String>(*illegalCharacter.toTypedArray(), "\\d")
val numberAndIllegalCharacters = listOf(*illegalCharacter.toTypedArray(), "\\d")

it.trim().replaceFirst(Regex("${numberAndIllegalCharacters.toString().trim()}{1,}"), "")
it.trim().replaceFirst(Regex("${numberAndIllegalCharacters.toString().trim()}+"), "")
} else {
it
}
Expand All @@ -51,12 +51,11 @@ abstract class KName : IKName {
protected fun toBeLegalName(name: String): String {
val tempName = name.replace(illegalCharacter.toString(), "")

val legalName = if (tempName in illegalNameList) {
return if (tempName in illegalNameList) {
tempName + suffix
} else {
tempName
}
return legalName
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package wu.seal.jsontokotlin.codeelements
import wu.seal.jsontokotlin.ConfigManager
import wu.seal.jsontokotlin.TargetJsonConverter
import wu.seal.jsontokotlin.supporter.*
import wu.seal.jsontokotlin.utils.getIndent

/**
*
Expand Down
Loading