Skip to content

Commit 7464817

Browse files
committed
Feature: Base Class Support
1 parent 2040c99 commit 7464817

File tree

9 files changed

+179
-4
lines changed

9 files changed

+179
-4
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ repositories {
3939
}
4040

4141
dependencies {
42-
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.20")
42+
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.5.30")
4343
testImplementation("com.winterbe:expekt:0.5.0") {
4444
exclude(group = "org.jetbrains.kotlin")
4545
}

src/main/kotlin/extensions/ExtensionsCollector.kt

+1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ object ExtensionsCollector {
3737
NeedNonNullableClassesSupport,
3838
InternalModifierSupport,
3939
AddGsonExposeAnnotationSupport,
40+
BaseClassSupport
4041
)
4142
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package extensions.wu.seal
2+
3+
import extensions.Extension
4+
import wu.seal.jsontokotlin.model.classscodestruct.DataClass
5+
import wu.seal.jsontokotlin.model.classscodestruct.KotlinClass
6+
import wu.seal.jsontokotlin.ui.*
7+
import javax.swing.JPanel
8+
9+
object BaseClassSupport : Extension() {
10+
/**
11+
* Config key can't be private, as it will be accessed from `library` module
12+
*/
13+
14+
@Suppress("MemberVisibilityCanBePrivate")
15+
val baseClassSupportEnabledKey = "azk.zero.baseclass_enabled"
16+
17+
@Suppress("MemberVisibilityCanBePrivate")
18+
const val baseClassImportKey = "azk.zero.baseclass_import"
19+
20+
@Suppress("MemberVisibilityCanBePrivate")
21+
const val baseClassNameKey = "azk.zero.baseclass_name"
22+
23+
@Suppress("MemberVisibilityCanBePrivate")
24+
const val baseClassPropertiesKey = "azk.zero.baseclass_properties"
25+
26+
override fun createUI(): JPanel {
27+
val classImportField = jTextInput(getConfig(baseClassImportKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
28+
addFocusLostListener {
29+
if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
30+
setConfig(baseClassImportKey, text)
31+
}
32+
}
33+
document = ImportConventionDocument()
34+
}
35+
36+
val classNameField = jTextInput(getConfig(baseClassNameKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
37+
addFocusLostListener {
38+
if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
39+
setConfig(baseClassNameKey, text)
40+
}
41+
}
42+
document = SuperClassConventionDocument(100)
43+
}
44+
45+
val classPropertiesField = jTextInput(getConfig(baseClassPropertiesKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
46+
addFocusLostListener {
47+
if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
48+
setConfig(baseClassPropertiesKey, text)
49+
}
50+
}
51+
document = PropertyConventionDocument()
52+
}
53+
54+
return jVerticalLinearLayout {
55+
jHorizontalLinearLayout{
56+
jCheckBox("Base Class Support?", getConfig(baseClassSupportEnabledKey).toBoolean(), { isSelected ->
57+
setConfig(baseClassSupportEnabledKey, isSelected.toString())
58+
classImportField.isEnabled = isSelected
59+
classNameField.isEnabled = isSelected
60+
classPropertiesField.isEnabled = isSelected
61+
})
62+
}
63+
jHorizontalLinearLayout {
64+
jLabel("Base Class Import line")
65+
add(classImportField)
66+
}
67+
jHorizontalLinearLayout {
68+
jLabel("Base Class Name, used as-is")
69+
add(classNameField)
70+
}
71+
jHorizontalLinearLayout {
72+
jLabel("Excluded Properties list, comma-separated")
73+
add(classPropertiesField)
74+
}
75+
}
76+
}
77+
;
78+
override fun intercept(kotlinClass: KotlinClass): KotlinClass {
79+
// val exclusion = listOf("error", "message", "status_code", "status", "statusCode")
80+
return if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
81+
val exclusionNames = getConfig(baseClassPropertiesKey).split(",").map { it.trim() }
82+
val baseClassName = getConfig(baseClassNameKey)
83+
if (kotlinClass is DataClass) {
84+
if (kotlinClass.isTop.not()) return kotlinClass
85+
val newProperties = kotlinClass.properties.mapNotNull { it.takeIf { it.originName !in exclusionNames } }
86+
kotlinClass.copy(properties = newProperties, parentClassTemplate = baseClassName)
87+
} else kotlinClass
88+
} else {
89+
kotlinClass
90+
}
91+
}
92+
93+
override fun intercept(originClassImportDeclaration: String): String {
94+
95+
// val classAnnotationImportClassString = "import com.arena.banglalinkmela.app.data.model.response.base.BaseResponse"
96+
val classAnnotationImportClassString = getConfig(baseClassImportKey)
97+
98+
return if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
99+
originClassImportDeclaration.append("import $classAnnotationImportClassString")
100+
} else {
101+
originClassImportDeclaration
102+
}
103+
}
104+
}

src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/DataClass.kt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ data class DataClass(
1515
val fromJsonSchema: Boolean = false,
1616
val excludedProperties: List<String> = listOf(),
1717
val parentClass: KotlinClass? = null,
18+
val isTop:Boolean=false,
1819
override val codeBuilder: IKotlinDataClassCodeBuilder = KotlinDataClassCodeBuilder
1920
) : ModifiableKotlinClass, NoGenericKotlinClass {
2021

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package wu.seal.jsontokotlin.ui
2+
3+
import javax.swing.text.AttributeSet
4+
import javax.swing.text.PlainDocument
5+
6+
/**
7+
* Created by ted on 2019/8/21 11:08.
8+
*/
9+
class ImportConventionDocument(maxLength: Int) : PlainDocument() {
10+
constructor() : this(252)
11+
12+
private val maxLength: Int = if (maxLength > 252 || maxLength <= 0) 252 else maxLength
13+
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
14+
str ?: return
15+
val take = maxLength - length
16+
if (take <= 0) return
17+
super.insertString(
18+
offs,
19+
str.filter { it.isLetterOrDigit() || it in listOf('_', '.') }.take(take),
20+
a
21+
)
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package wu.seal.jsontokotlin.ui
2+
3+
import javax.swing.text.AttributeSet
4+
import javax.swing.text.PlainDocument
5+
6+
/**
7+
* Created by ted on 2019/8/21 11:08.
8+
*/
9+
class PropertyConventionDocument(maxLength: Int) : PlainDocument() {
10+
constructor() : this(252)
11+
12+
private val maxLength: Int = if (maxLength > 252 || maxLength <= 0) 252 else maxLength
13+
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
14+
str ?: return
15+
val take = maxLength - length
16+
if (take <= 0) return
17+
super.insertString(
18+
offs,
19+
str.filter { it.isLetterOrDigit() || it in listOf('_', ',') }.take(take),
20+
a
21+
)
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package wu.seal.jsontokotlin.ui
2+
3+
import javax.swing.text.AttributeSet
4+
import javax.swing.text.PlainDocument
5+
6+
/**
7+
* Created by ted on 2019/8/21 11:08.
8+
*/
9+
class SuperClassConventionDocument(maxLength: Int) : PlainDocument() {
10+
constructor() : this(252)
11+
12+
private val maxLength: Int = if (maxLength > 252 || maxLength <= 0) 252 else maxLength
13+
override fun insertString(offs: Int, str: String?, a: AttributeSet?) {
14+
str ?: return
15+
val take = maxLength - length
16+
if (take <= 0) return
17+
super.insertString(
18+
offs,
19+
str.filter { it.isLetterOrDigit() || it in listOf('_', '(', ')', '.') }.take(take),
20+
a
21+
)
22+
}
23+
}

src/main/kotlin/wu/seal/jsontokotlin/utils/KotlinClassMaker.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class KotlinClassMaker(private val rootClassName: String, private val json: Stri
1818
DataClassGeneratorByJSONSchema(rootClassName, jsonSchema).generate()
1919
} else {
2020
when {
21-
json.isJSONObject() -> DataClassGeneratorByJSONObject(rootClassName, Gson().fromJson(json, JsonObject::class.java)).generate()
21+
json.isJSONObject() -> DataClassGeneratorByJSONObject(rootClassName, Gson().fromJson(json, JsonObject::class.java)).generate(isTop = true)
2222
json.isJSONArray() -> ListClassGeneratorByJSONArray(rootClassName, json).generate()
2323
else -> throw IllegalStateException("Can't generate Kotlin Data Class from a no JSON Object/JSON Object Array")
2424
}

src/main/kotlin/wu/seal/jsontokotlin/utils/classgenerator/DataClassGeneratorByJSONObject.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import wu.seal.jsontokotlin.utils.*
1414
*/
1515
class DataClassGeneratorByJSONObject(private val className: String, private val jsonObject: JsonObject) {
1616

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

104104
val propertiesAfterConsumeBackStageProperties = properties.consumeBackstageProperties()
105-
return DataClass(name = className, properties = propertiesAfterConsumeBackStageProperties)
105+
return DataClass(name = className, properties = propertiesAfterConsumeBackStageProperties, isTop = isTop)
106106
}
107107

108108
private fun mapValueIsObjectType(mapValueType: String) = (mapValueType == MAP_DEFAULT_OBJECT_VALUE_TYPE

0 commit comments

Comments
 (0)