Skip to content

Commit

Permalink
Feature: Base Class Support
Browse files Browse the repository at this point in the history
  • Loading branch information
AZKZero committed Dec 4, 2023
1 parent 2040c99 commit 7464817
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repositories {
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.20")
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.5.30")
testImplementation("com.winterbe:expekt:0.5.0") {
exclude(group = "org.jetbrains.kotlin")
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/extensions/ExtensionsCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ object ExtensionsCollector {
NeedNonNullableClassesSupport,
InternalModifierSupport,
AddGsonExposeAnnotationSupport,
BaseClassSupport
)
}
104 changes: 104 additions & 0 deletions src/main/kotlin/extensions/wu/seal/BaseClassSupport.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package extensions.wu.seal

import extensions.Extension
import wu.seal.jsontokotlin.model.classscodestruct.DataClass
import wu.seal.jsontokotlin.model.classscodestruct.KotlinClass
import wu.seal.jsontokotlin.ui.*
import javax.swing.JPanel

object BaseClassSupport : Extension() {
/**
* Config key can't be private, as it will be accessed from `library` module
*/

@Suppress("MemberVisibilityCanBePrivate")
val baseClassSupportEnabledKey = "azk.zero.baseclass_enabled"

@Suppress("MemberVisibilityCanBePrivate")
const val baseClassImportKey = "azk.zero.baseclass_import"

@Suppress("MemberVisibilityCanBePrivate")
const val baseClassNameKey = "azk.zero.baseclass_name"

@Suppress("MemberVisibilityCanBePrivate")
const val baseClassPropertiesKey = "azk.zero.baseclass_properties"

override fun createUI(): JPanel {
val classImportField = jTextInput(getConfig(baseClassImportKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
addFocusLostListener {
if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
setConfig(baseClassImportKey, text)
}
}
document = ImportConventionDocument()
}

val classNameField = jTextInput(getConfig(baseClassNameKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
addFocusLostListener {
if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
setConfig(baseClassNameKey, text)
}
}
document = SuperClassConventionDocument(100)
}

val classPropertiesField = jTextInput(getConfig(baseClassPropertiesKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
addFocusLostListener {
if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
setConfig(baseClassPropertiesKey, text)
}
}
document = PropertyConventionDocument()
}

return jVerticalLinearLayout {
jHorizontalLinearLayout{
jCheckBox("Base Class Support?", getConfig(baseClassSupportEnabledKey).toBoolean(), { isSelected ->
setConfig(baseClassSupportEnabledKey, isSelected.toString())
classImportField.isEnabled = isSelected
classNameField.isEnabled = isSelected
classPropertiesField.isEnabled = isSelected
})
}
jHorizontalLinearLayout {
jLabel("Base Class Import line")
add(classImportField)
}
jHorizontalLinearLayout {
jLabel("Base Class Name, used as-is")
add(classNameField)
}
jHorizontalLinearLayout {
jLabel("Excluded Properties list, comma-separated")
add(classPropertiesField)
}
}
}
;
override fun intercept(kotlinClass: KotlinClass): KotlinClass {
// val exclusion = listOf("error", "message", "status_code", "status", "statusCode")
return if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
val exclusionNames = getConfig(baseClassPropertiesKey).split(",").map { it.trim() }
val baseClassName = getConfig(baseClassNameKey)
if (kotlinClass is DataClass) {
if (kotlinClass.isTop.not()) return kotlinClass
val newProperties = kotlinClass.properties.mapNotNull { it.takeIf { it.originName !in exclusionNames } }
kotlinClass.copy(properties = newProperties, parentClassTemplate = baseClassName)
} else kotlinClass
} else {
kotlinClass
}
}

override fun intercept(originClassImportDeclaration: String): String {

// val classAnnotationImportClassString = "import com.arena.banglalinkmela.app.data.model.response.base.BaseResponse"
val classAnnotationImportClassString = getConfig(baseClassImportKey)

return if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
originClassImportDeclaration.append("import $classAnnotationImportClassString")
} else {
originClassImportDeclaration
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ data class DataClass(
val fromJsonSchema: Boolean = false,
val excludedProperties: List<String> = listOf(),
val parentClass: KotlinClass? = null,
val isTop:Boolean=false,
override val codeBuilder: IKotlinDataClassCodeBuilder = KotlinDataClassCodeBuilder
) : ModifiableKotlinClass, NoGenericKotlinClass {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package wu.seal.jsontokotlin.ui

import javax.swing.text.AttributeSet
import javax.swing.text.PlainDocument

/**
* Created by ted on 2019/8/21 11:08.
*/
class ImportConventionDocument(maxLength: Int) : PlainDocument() {
constructor() : this(252)

private val maxLength: Int = if (maxLength > 252 || maxLength <= 0) 252 else maxLength
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
str ?: return
val take = maxLength - length
if (take <= 0) return
super.insertString(
offs,
str.filter { it.isLetterOrDigit() || it in listOf('_', '.') }.take(take),
a
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package wu.seal.jsontokotlin.ui

import javax.swing.text.AttributeSet
import javax.swing.text.PlainDocument

/**
* Created by ted on 2019/8/21 11:08.
*/
class PropertyConventionDocument(maxLength: Int) : PlainDocument() {
constructor() : this(252)

private val maxLength: Int = if (maxLength > 252 || maxLength <= 0) 252 else maxLength
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
str ?: return
val take = maxLength - length
if (take <= 0) return
super.insertString(
offs,
str.filter { it.isLetterOrDigit() || it in listOf('_', ',') }.take(take),
a
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package wu.seal.jsontokotlin.ui

import javax.swing.text.AttributeSet
import javax.swing.text.PlainDocument

/**
* Created by ted on 2019/8/21 11:08.
*/
class SuperClassConventionDocument(maxLength: Int) : PlainDocument() {
constructor() : this(252)

private val maxLength: Int = if (maxLength > 252 || maxLength <= 0) 252 else maxLength
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
str ?: return
val take = maxLength - length
if (take <= 0) return
super.insertString(
offs,
str.filter { it.isLetterOrDigit() || it in listOf('_', '(', ')', '.') }.take(take),
a
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class KotlinClassMaker(private val rootClassName: String, private val json: Stri
DataClassGeneratorByJSONSchema(rootClassName, jsonSchema).generate()
} else {
when {
json.isJSONObject() -> DataClassGeneratorByJSONObject(rootClassName, Gson().fromJson(json, JsonObject::class.java)).generate()
json.isJSONObject() -> DataClassGeneratorByJSONObject(rootClassName, Gson().fromJson(json, JsonObject::class.java)).generate(isTop = true)
json.isJSONArray() -> ListClassGeneratorByJSONArray(rootClassName, json).generate()
else -> throw IllegalStateException("Can't generate Kotlin Data Class from a no JSON Object/JSON Object Array")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import wu.seal.jsontokotlin.utils.*
*/
class DataClassGeneratorByJSONObject(private val className: String, private val jsonObject: JsonObject) {

fun generate(): DataClass {
fun generate(isTop:Boolean=false): DataClass {
if (maybeJsonObjectBeMapType(jsonObject) && ConfigManager.enableMapType) {
throw IllegalArgumentException("Can't generate data class from a Map type JSONObjcet when enable Map Type : $jsonObject")
}
Expand Down Expand Up @@ -102,7 +102,7 @@ class DataClassGeneratorByJSONObject(private val className: String, private val
}

val propertiesAfterConsumeBackStageProperties = properties.consumeBackstageProperties()
return DataClass(name = className, properties = propertiesAfterConsumeBackStageProperties)
return DataClass(name = className, properties = propertiesAfterConsumeBackStageProperties, isTop = isTop)
}

private fun mapValueIsObjectType(mapValueType: String) = (mapValueType == MAP_DEFAULT_OBJECT_VALUE_TYPE
Expand Down

0 comments on commit 7464817

Please sign in to comment.